| 1 | #!/usr/bin/env python3 |
| 2 | """Deploy guard for projects.timfalken.com repositories. |
| 3 | |
| 4 | Shared snippet: copy this file unchanged into ci/ of every repository that |
| 5 | deploys to projects.timfalken.com. It refuses any project directory that would |
| 6 | make a mirror --delete overwrite the whole site (or another project). |
| 7 | |
| 8 | Usage in Python: from deploy_guard import project_dir; path = project_dir(os.environ) |
| 9 | Usage in a shell: python3 ci/deploy_guard.py (exit 1 with a clear message on failure) |
| 10 | |
| 11 | Values are never printed; only variable names appear in messages. |
| 12 | """ |
| 13 | import posixpath |
| 14 | import re |
| 15 | import sys |
| 16 | |
| 17 | ROOT_VAR = 'timftproot' |
| 18 | PROJECT_VAR = 'projectftp' |
| 19 | |
| 20 | |
| 21 | class DeployGuardError(ValueError): |
| 22 | pass |
| 23 | |
| 24 | |
| 25 | def _expand(value, environ): |
| 26 | """Expand $timftproot / ${timftproot} (GitLab expanded these; BlueGit secrets do not).""" |
| 27 | root = environ.get(ROOT_VAR) or '' |
| 28 | return re.sub(r'\$\{?' + ROOT_VAR + r'\}?', lambda _m: root, value) |
| 29 | |
| 30 | |
| 31 | def _norm(path): |
| 32 | path = path.strip() |
| 33 | if not path: |
| 34 | return '' |
| 35 | norm = posixpath.normpath(path) |
| 36 | return '/' if norm in ('/', '//') else norm.rstrip('/') or '/' |
| 37 | |
| 38 | |
| 39 | def project_dir(environ): |
| 40 | raw = environ.get(PROJECT_VAR) |
| 41 | if raw is None: |
| 42 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: CI-variabele {PROJECT_VAR} is niet gezet. ' |
| 43 | f'Zet de projectmap als project-secret, bijvoorbeeld $timftproot/<project>.') |
| 44 | if any(c in raw for c in '\r\n\x00'): |
| 45 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} bevat een regeleinde of NUL-byte.') |
| 46 | if '$' in raw.replace('$' + ROOT_VAR, '').replace('${' + ROOT_VAR + '}', ''): |
| 47 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} bevat een onbekende $-verwijzing.') |
| 48 | if (('$' + ROOT_VAR) in raw or ('${' + ROOT_VAR + '}') in raw) and not (environ.get(ROOT_VAR) or '').strip(): |
| 49 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} verwijst naar {ROOT_VAR}, maar die is niet gezet.') |
| 50 | value = _expand(raw, environ).strip() |
| 51 | if not value: |
| 52 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: CI-variabele {PROJECT_VAR} is leeg.') |
| 53 | parts = [p for p in value.replace('\\', '/').split('/') if p] |
| 54 | if '..' in parts: |
| 55 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} mag geen ".." bevatten.') |
| 56 | if value.startswith('~'): |
| 57 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} mag niet de thuismap (~) zijn.') |
| 58 | norm = _norm(value) |
| 59 | if norm in ('/', '.', ''): |
| 60 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} wijst naar de root ("/" of "."); ' |
| 61 | f'dat zou de hele website overschrijven.') |
| 62 | root = _norm(environ.get(ROOT_VAR) or '') |
| 63 | if root and norm == root: |
| 64 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} is gelijk aan {ROOT_VAR} (de FTP-root van de site); ' |
| 65 | f'dat zou de hele website overschrijven.') |
| 66 | if root and root not in ('/', '.') and norm.startswith('/') and not norm.startswith(root + '/'): |
| 67 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} ligt niet onder {ROOT_VAR}.') |
| 68 | return norm |
| 69 | |
| 70 | |
| 71 | def main(environ=None): |
| 72 | import os |
| 73 | try: |
| 74 | project_dir(os.environ if environ is None else environ) |
| 75 | except DeployGuardError as error: |
| 76 | print(str(error), file=sys.stderr) |
| 77 | return 1 |
| 78 | print('Deploy guard: projectmap ok.') |
| 79 | return 0 |
| 80 | |
| 81 | |
| 82 | if __name__ == '__main__': |
| 83 | sys.exit(main()) |
projects-timfalken-com verhalenboek