Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,26 @@ jobs:
- name: Package USB / serial partition upgrade
run: python3 tools/serial_upgrade.py package --build-dir build --output "build/tianshanos-serial-v$(cat version.txt)"

- name: Normalize public firmware filename
run: |
python3 - <<'PYTHON'
import json
from pathlib import Path
root = Path('build')
(root / 'TianShanOS.bin').rename(root / 'TianshanOS.bin')
args = root / 'flasher_args.json'
data = json.loads(args.read_text())
data['app']['file'] = 'TianshanOS.bin'
data['flash_files'] = {address: 'TianshanOS.bin' if name == 'TianShanOS.bin' else name for address, name in data['flash_files'].items()}
args.write_text(json.dumps(data, indent=2) + '\n')
evidence = root / 'security-artifact.json'
artifact = json.loads(evidence.read_text())
if artifact['binary'] != 'TianShanOS.bin':
raise ValueError('Unexpected sealed firmware filename')
artifact['binary'] = 'TianshanOS.bin'
evidence.write_text(json.dumps(artifact, indent=2) + '\n')
PYTHON

- name: Upload firmware artifacts
uses: actions/upload-artifact@v4
with:
Expand Down Expand Up @@ -342,20 +362,6 @@ jobs:
GH_TOKEN: ${{ github.token }}
BUILD_VERSION: ${{ needs.build.outputs.version }}
BUILD_COMMIT: ${{ needs.build.outputs.commit }}
- name: Normalize public firmware filename
run: |
python3 - <<'PYTHON'
import json
from pathlib import Path
root = Path('firmware')
(root / 'TianShanOS.bin').rename(root / 'TianshanOS.bin')
args = root / 'flasher_args.json'
data = json.loads(args.read_text())
data['app']['file'] = 'TianshanOS.bin'
data['flash_files'] = {address: 'TianshanOS.bin' if name == 'TianShanOS.bin' else name for address, name in data['flash_files'].items()}
args.write_text(json.dumps(data, indent=2) + '\n')
PYTHON

- name: Update existing release assets
if: steps.preflight.outputs.existing_release == 'true'
run: |
Expand Down
25 changes: 22 additions & 3 deletions tests/release/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,41 @@ def test_existing_release_upload_only_replaces_matching_assets(self):
self.assertEqual(args[-3:],['--clobber','--repo','RMinte/TianshanOS'])

def test_public_asset_name_and_flash_mapping_match(self):
import json, subprocess
import json, struct, subprocess
workflow = (ROOT / '.github/workflows/build.yml').read_text()
step = workflow.split(' - name: Normalize public firmware filename\n', 1)[1].split(' - name:', 1)[0]
body = step.split("python3 - <<'PYTHON'\n", 1)[1].split(' PYTHON', 1)[0]
code = '\n'.join(line[10:] for line in body.splitlines())
with tempfile.TemporaryDirectory() as folder:
root = Path(folder) / 'firmware'; root.mkdir()
original = b'synthetic firmware bytes'
root = Path(folder) / 'build'; root.mkdir()
original = bytearray(512)
original[0] = 0xE9
struct.pack_into('<I', original, 32, 0xABCD5432)
original[48:53] = b'0.6.5'
original[80:90] = b'TianShanOS'
(root / 'security-build.json').write_text(json.dumps(dict(
schema=2, development=False, recovery=False, webui_enabled=True,
auth_required=True, security_enabled=True, webui_tls=False)))
validator = ROOT / 'tools/check_release.py'
(root / 'TianShanOS.bin').write_bytes(original)
(root / 'www.bin').write_bytes(b'synthetic web')
args = {'app': {'file': 'TianShanOS.bin'}, 'flash_files': {'0x20000': 'TianShanOS.bin', '0x6a0000': 'www.bin'}}
(root / 'flasher_args.json').write_text(json.dumps(args))
subprocess.run([sys.executable, validator, '--seal-build', root], check=True, capture_output=True)
subprocess.run([sys.executable, '-c', code], cwd=folder, check=True)
result = subprocess.run([sys.executable, validator, '--check-security', root],
check=True, capture_output=True, text=True)
self.assertIn('TianShanOS', result.stdout)
self.assertNotIn('TianShanOS.bin', {p.name for p in root.iterdir()})
self.assertIn('TianshanOS.bin', {p.name for p in root.iterdir()})
self.assertEqual((root / 'TianshanOS.bin').read_bytes(), original)
evidence = json.loads((root / 'security-artifact.json').read_text())
self.assertEqual(evidence['binary'], 'TianshanOS.bin')
(root / 'TianshanOS.bin').write_bytes(original + b'changed')
tampered = subprocess.run([sys.executable, validator, '--check-security', root],
capture_output=True, text=True)
self.assertNotEqual(tampered.returncode, 0)
self.assertIn('does not match the actual firmware', tampered.stderr)
actual = json.loads((root / 'flasher_args.json').read_text())
self.assertEqual(actual['app']['file'], 'TianshanOS.bin')
self.assertEqual(actual['flash_files'], {'0x20000': 'TianshanOS.bin', '0x6a0000': 'www.bin'})
Expand Down
5 changes: 3 additions & 2 deletions tools/check_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def seal_security_artifact(build_dir):
def validate_security_artifacts(build_dir, *, recovery=False):
build_dir = Path(build_dir)
artifact = json.loads((build_dir/'security-artifact.json').read_text())
if type(artifact.get('schema')) is not int or artifact.get('schema') != 1 or artifact.get('binary') != 'TianShanOS.bin':
binary = artifact.get('binary')
if type(artifact.get('schema')) is not int or artifact.get('schema') != 1 or binary not in ('TianShanOS.bin', 'TianshanOS.bin'):
raise ValueError('Only standard firmware is eligible for release')
profile = artifact.get('profile', {})
wanted = {'schema': 2, 'development': False, 'recovery': False,
Expand All @@ -67,7 +68,7 @@ def validate_security_artifacts(build_dir, *, recovery=False):
wanted.update(recovery=True, webui_enabled=False, auth_required=False, security_enabled=False, webui_tls=False)
if set(profile) != set(wanted) or any(type(profile[k]) is not type(v) or profile[k] != v for k, v in wanted.items()):
raise ValueError('Release requires standard WebUI authentication/security with the supported HTTP transport; dedicated recovery policy is separate')
identity = firmware_identity(build_dir/'TianShanOS.bin')
identity = firmware_identity(build_dir / binary)
if identity['project_name'] != 'TianShanOS' or any(artifact.get(k) != v for k, v in identity.items()):
raise ValueError('Release security evidence does not match the actual firmware')
return identity
Expand Down
Loading