"""Public bootstrap path/expiry regressions, without server/VM/network access.""" import datetime as dt from pathlib import Path import sys import unittest sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'deploy/windows')) from download_service import select_file class DownloadGuards(unittest.TestCase): def setUp(self): self.now=dt.datetime(2026,11,13,12,tzinfo=dt.timezone.utc) self.config=dict(url_path='/windows/fixed/windows-dev-access.zip',expires_at='2026-11-14T12:00:00+00:00') def test_only_fixed_public_file_is_selected(self): self.assertEqual(select_file('download',self.config['url_path'],self.config,self.now), Path('/srv/rmm-bootstrap-download/windows-dev-access.zip')) for path in ('/','/etc/passwd','/windows/../windows-dev-access.zip',self.config['url_path']+'?path=/etc/passwd', '/windows/%2e%2e/windows-dev-access.zip','/windows/fixed/../../domain.key'): with self.subTest(path=path):self.assertIsNone(select_file('download',path,self.config,self.now)) def test_expiry_has_no_grace_window(self): for delta in (dt.timedelta(days=1),dt.timedelta(days=1,seconds=1)): self.assertIsNone(select_file('download',self.config['url_path'],self.config,self.now+delta)) def test_explicit_repair_allowlist_cannot_expose_another_file(self): self.config['public_files']={'/repair.zip':'windows-dev-repair.zip','/bad':'../../domain.key'} self.assertEqual(select_file('download','/repair.zip',self.config,self.now),Path('/srv/rmm-bootstrap-download/windows-dev-repair.zip')) self.assertIsNone(select_file('download','/bad',self.config,self.now)) def test_acme_has_no_arbitrary_file_path(self): token='a'*43 self.assertEqual(select_file('acme','/.well-known/acme-challenge/'+token,{},self.now),Path('/srv/rmm-bootstrap-acme')/token) for path in ('/','/.well-known/acme-challenge/../../domain.key','/.well-known/acme-challenge/%2e%2e', '/.well-known/acme-challenge/'+token+'?x=y'): with self.subTest(path=path):self.assertIsNone(select_file('acme',path,{},self.now)) if __name__=='__main__':unittest.main()