| 1 | #!/usr/bin/env python3 |
| 2 | """Deploy guard: expand $timftproot, refuse a deploy that would hit the FTP root.""" |
| 3 | import posixpath |
| 4 | import re |
| 5 | import sys |
| 6 | |
| 7 | ROOT_VAR = 'timftproot' |
| 8 | PROJECT_VAR = 'projectftp' |
| 9 | ALLOWED_LEAF = 'meteors' |
| 10 | |
| 11 | |
| 12 | class DeployGuardError(ValueError): |
| 13 | pass |
| 14 | |
| 15 | |
| 16 | def _expand(value, environ): |
| 17 | """Expand $timftproot / ${timftproot} (BlueGit secrets do not).""" |
| 18 | root = environ.get(ROOT_VAR) or '' |
| 19 | return re.sub(r'\$\{?' + ROOT_VAR + r'\}?', lambda _m: root, value) |
| 20 | |
| 21 | |
| 22 | def _norm(path): |
| 23 | path = (path or '').strip() |
| 24 | if not path: |
| 25 | return '' |
| 26 | # Collapse duplicate slashes before and after normpath. |
| 27 | path = re.sub(r'/+', '/', path.replace('\\', '/')) |
| 28 | norm = posixpath.normpath(path) |
| 29 | return '/' if norm in ('/', '//', '.') else norm.rstrip('/') or '/' |
| 30 | |
| 31 | |
| 32 | def project_dir(environ): |
| 33 | raw = environ.get(PROJECT_VAR) |
| 34 | if raw is None: |
| 35 | raise DeployGuardError( |
| 36 | f'DEPLOY GEBLOKKEERD: CI-variabele {PROJECT_VAR} is niet gezet. ' |
| 37 | f'Zet de projectmap als project-secret, bijvoorbeeld $timftproot/{ALLOWED_LEAF}.' |
| 38 | ) |
| 39 | if any(c in raw for c in '\r\n\x00'): |
| 40 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} bevat een regeleinde of NUL-byte.') |
| 41 | leftover_dollar = raw.replace('$' + ROOT_VAR, '').replace('${' + ROOT_VAR + '}', '') |
| 42 | if '$' in leftover_dollar: |
| 43 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} bevat een onbekende $-verwijzing.') |
| 44 | if (('$' + ROOT_VAR) in raw or ('${' + ROOT_VAR + '}') in raw) and not (environ.get(ROOT_VAR) or '').strip(): |
| 45 | raise DeployGuardError( |
| 46 | f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} verwijst naar {ROOT_VAR}, maar die is niet gezet.' |
| 47 | ) |
| 48 | value = _expand(raw, environ).strip() |
| 49 | if not value: |
| 50 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: CI-variabele {PROJECT_VAR} is leeg.') |
| 51 | parts = [p for p in value.replace('\\', '/').split('/') if p] |
| 52 | if '..' in parts: |
| 53 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} mag geen ".." bevatten.') |
| 54 | if value.startswith('~'): |
| 55 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} mag niet de thuismap (~) zijn.') |
| 56 | norm = _norm(value) |
| 57 | if norm in ('/', '.', ''): |
| 58 | raise DeployGuardError( |
| 59 | f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} wijst naar de root ("/" of "."); ' |
| 60 | 'dat zou de hele website overschrijven.' |
| 61 | ) |
| 62 | root = _norm(environ.get(ROOT_VAR) or '') |
| 63 | if root and norm == root: |
| 64 | raise DeployGuardError( |
| 65 | f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} is gelijk aan {ROOT_VAR} (de FTP-root van de site); ' |
| 66 | 'dat zou de hele website overschrijven.' |
| 67 | ) |
| 68 | if root and root not in ('/', '.') and norm.startswith('/') and not norm.startswith(root + '/'): |
| 69 | raise DeployGuardError(f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} ligt niet onder {ROOT_VAR}.') |
| 70 | leaf = parts[-1].lower() if parts else '' |
| 71 | if leaf != ALLOWED_LEAF: |
| 72 | raise DeployGuardError( |
| 73 | f'DEPLOY GEBLOKKEERD: {PROJECT_VAR} moet op "{ALLOWED_LEAF}" eindigen (leaf={leaf!r}).' |
| 74 | ) |
| 75 | return norm |
| 76 | |
| 77 | |
| 78 | def main(environ=None): |
| 79 | import os |
| 80 | try: |
| 81 | path = project_dir(os.environ if environ is None else environ) |
| 82 | except DeployGuardError as error: |
| 83 | print(str(error), file=sys.stderr) |
| 84 | return 1 |
| 85 | print('Deploy guard: projectmap ok.') |
| 86 | # Path itself is not printed (secrets). Length only. |
| 87 | print('projectftp length=%d' % len(path)) |
| 88 | return 0 |
| 89 | |
| 90 | |
| 91 | if __name__ == '__main__': |
| 92 | sys.exit(main()) |
projects-timfalken-com meteors