forked from ChelseaKR/habitable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_verify_path_security.py
More file actions
456 lines (380 loc) · 15.3 KB
/
Copy pathtest_verify_path_security.py
File metadata and controls
456 lines (380 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright 2026 Chelsea Kelly-Reif
"""Hostile packet file references stay inside regular packet files."""
from __future__ import annotations
import json
import os
import shutil
from collections.abc import Callable
from pathlib import Path
from typing import cast
import pytest
import habitable.verify as verifier
from habitable.canonical import JSONValue, canonical_json, sha256_bytes
from habitable.capture import capture
from habitable.errors import VerificationError
from habitable.packet import _write_signature, build_packet
from habitable.tsa import LocalRfc3161TSA
from habitable.vault import Vault
def _make_packet(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
*,
include_originals: bool = False,
) -> tuple[Vault, Path]:
vault = make_vault()
issue_id = vault.document.add_issue(category="mold", title="Mold", issue_id="i1")
capture(vault, make_jpeg("reference.jpg"), issue_id=issue_id, tsa=local_tsa)
packet = tmp_path / "packet"
build_packet(
vault,
packet,
include_originals=include_originals,
generated_at="2026-01-02T00:10:00Z",
make_pdf=False,
)
return vault, packet
def _rewrite_item(
vault: Vault,
packet: Path,
fields: dict[str, JSONValue],
*,
resign: bool,
) -> None:
bundle = cast("dict[str, JSONValue]", json.loads((packet / "bundle.json").read_text()))
items = cast("list[JSONValue]", bundle["items"])
item = cast("dict[str, JSONValue]", items[0])
item.update(fields)
bundle_bytes = canonical_json(bundle)
(packet / "bundle.json").write_bytes(bundle_bytes)
if resign:
_write_signature(vault, packet, bundle_bytes)
def _only_item(packet: Path, local_tsa: LocalRfc3161TSA) -> verifier.ItemVerdict:
report = verifier.verify_packet(packet, trusted_certs=[local_tsa.certificate])
assert len(report.items) == 1
return report.items[0]
@pytest.mark.parametrize(
"reference",
[
"/etc/hosts",
"../outside.jpg",
"nested/outside.jpg",
r"nested\outside.jpg",
"..",
r"C:\Windows\system.ini",
],
)
def test_signed_packet_rejects_non_basename_shared_references(
reference: str,
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
vault, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
_rewrite_item(vault, packet, {"shared_name": reference}, resign=True)
report = verifier.verify_packet(packet, trusted_certs=[local_tsa.certificate])
assert report.signature_ok
assert not report.items[0].shared_media_ok
assert any("reference must be one basename" in note for note in report.items[0].notes)
def test_signed_packet_rejects_absolute_poster_reference(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
vault, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
_rewrite_item(
vault,
packet,
{"poster_name": "/etc/hosts", "poster_hash": "0" * 64},
resign=True,
)
item = _only_item(packet, local_tsa)
assert not item.shared_media_ok
assert any("poster frame reference must be one basename" in note for note in item.notes)
@pytest.mark.parametrize(
("poster_hash_from_media", "expected_note"),
[
(False, "poster frame does not match its recorded hash"),
(True, "no signed custody entry binds the poster frame to the original"),
],
)
def test_signed_packet_checks_poster_hash_and_custody_binding(
poster_hash_from_media: bool,
expected_note: str,
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
vault, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
media = next((packet / "media").iterdir())
media_hash = sha256_bytes(media.read_bytes())
_rewrite_item(
vault,
packet,
{
"poster_name": media.name,
"poster_hash": media_hash if poster_hash_from_media else "0" * 64,
},
resign=True,
)
item = _only_item(packet, local_tsa)
assert not item.structurally_intact
assert expected_note in item.notes
def test_signed_audio_without_transcript_or_poster_reports_accessibility_gap(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
vault, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
_rewrite_item(vault, packet, {"media_type": "audio/mpeg"}, resign=True)
item = _only_item(packet, local_tsa)
assert "no transcript or poster frame recorded for this item (accessibility gap)" in item.notes
@pytest.mark.parametrize("capture_id", ["/etc/hosts", "../outside", r"nested\outside"])
def test_signed_packet_rejects_unsafe_original_reference(
capture_id: str,
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
vault, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path, include_originals=True)
_rewrite_item(vault, packet, {"capture_id": capture_id}, resign=True)
item = _only_item(packet, local_tsa)
assert item.original_fixity_ok is False
assert any("embedded original reference must be one basename" in note for note in item.notes)
def test_rejects_symlinked_media_file_without_following_it(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
media = next((packet / "media").iterdir())
outside = tmp_path / "outside.jpg"
shutil.copyfile(media, outside)
media.unlink()
media.symlink_to(outside)
item = _only_item(packet, local_tsa)
assert not item.shared_media_ok
assert any("must not be a symlink" in note for note in item.notes)
def test_rejects_symlinked_designated_directory(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
outside = tmp_path / "outside-media"
(packet / "media").rename(outside)
(packet / "media").symlink_to(outside, target_is_directory=True)
item = _only_item(packet, local_tsa)
assert not item.shared_media_ok
assert any("directory must not be a symlink" in note for note in item.notes)
@pytest.mark.parametrize("replacement", ["missing", "regular-file"])
def test_rejects_missing_or_non_directory_media_directory(
replacement: str,
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
media_dir = packet / "media"
shutil.rmtree(media_dir)
if replacement == "regular-file":
media_dir.write_bytes(b"not a directory")
item = _only_item(packet, local_tsa)
assert not item.shared_media_ok
expected = "shared media directory missing"
if replacement == "regular-file":
expected = "shared media directory is not a regular directory"
assert expected in item.notes
def test_rejects_packet_directory_symlink(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
alias = tmp_path / "packet-alias"
alias.symlink_to(packet, target_is_directory=True)
with pytest.raises(VerificationError, match="packet directory must not be a symlink"):
verifier.verify_packet(alias, trusted_certs=[local_tsa.certificate])
def test_control_file_reader_rejects_missing_and_non_directory_packet_roots(
tmp_path: Path,
) -> None:
missing = tmp_path / "missing-packet"
with pytest.raises(VerificationError, match="packet directory could not be safely inspected"):
verifier.verify_packet(missing)
regular_file = tmp_path / "not-a-packet"
regular_file.write_bytes(b"not a directory")
with pytest.raises(VerificationError, match="packet path is not a directory"):
verifier.verify_packet(regular_file)
def test_reference_hasher_rejects_unsafe_packet_roots(tmp_path: Path) -> None:
missing = tmp_path / "missing"
assert verifier._hash_packet_reference(missing, "media", "item.jpg", label="shared media") == (
None,
"packet directory could not be safely inspected",
)
regular_file = tmp_path / "regular-file"
regular_file.write_bytes(b"not a packet")
assert verifier._hash_packet_reference(
regular_file, "media", "item.jpg", label="shared media"
) == (None, "packet path is not a directory")
packet = tmp_path / "packet"
packet.mkdir()
alias = tmp_path / "packet-alias"
alias.symlink_to(packet, target_is_directory=True)
assert verifier._hash_packet_reference(alias, "media", "item.jpg", label="shared media") == (
None,
"packet directory must not be a symlink",
)
def test_rejects_symlinked_bundle_before_parsing(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
bundle = packet / "bundle.json"
outside = tmp_path / "outside-bundle.json"
bundle.replace(outside)
bundle.symlink_to(outside)
with pytest.raises(VerificationError, match=r"bundle\.json must not be a symlink"):
verifier.verify_packet(packet, trusted_certs=[local_tsa.certificate])
def test_symlinked_signature_fails_without_following_it(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
signature = packet / "bundle.sig.json"
outside = tmp_path / "outside-signature.json"
signature.replace(outside)
signature.symlink_to(outside)
report = verifier.verify_packet(packet, trusted_certs=[local_tsa.certificate])
assert not report.signature_ok
assert not report.items[0].shared_media_ok
@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="FIFO creation is not portable")
def test_rejects_fifo_bundle_before_reading(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
bundle = packet / "bundle.json"
bundle.unlink()
os.mkfifo(bundle)
with pytest.raises(VerificationError, match=r"bundle\.json is not a regular file"):
verifier.verify_packet(packet, trusted_certs=[local_tsa.certificate])
def test_rejects_oversized_bundle_before_parsing(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
bundle = packet / "bundle.json"
monkeypatch.setattr(verifier, "_MAX_BUNDLE_BYTES", bundle.stat().st_size - 1)
with pytest.raises(VerificationError, match=r"bundle\.json exceeds"):
verifier.verify_packet(packet, trusted_certs=[local_tsa.certificate])
def test_rejects_directory_in_place_of_media_file(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
media = next((packet / "media").iterdir())
media.unlink()
media.mkdir()
item = _only_item(packet, local_tsa)
assert not item.shared_media_ok
assert any("not a regular file" in note for note in item.notes)
@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="FIFO creation is not portable")
def test_rejects_fifo_before_reading(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
media = next((packet / "media").iterdir())
media.unlink()
os.mkfifo(media)
item = _only_item(packet, local_tsa)
assert not item.shared_media_ok
assert any("not a regular file" in note for note in item.notes)
def test_rejects_file_over_verification_ceiling_before_hashing(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path)
media = next((packet / "media").iterdir())
monkeypatch.setattr(verifier, "_MAX_REFERENCED_FILE_BYTES", media.stat().st_size - 1)
item = _only_item(packet, local_tsa)
assert not item.shared_media_ok
assert any("verification limit" in note for note in item.notes)
def test_invalid_signature_never_inspects_bundle_file_references(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
vault, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path, include_originals=True)
_rewrite_item(
vault,
packet,
{
"capture_id": "/etc/hosts",
"shared_name": "/etc/hosts",
"poster_name": "/dev/null",
"poster_hash": "0" * 64,
"has_original": True,
},
resign=False,
)
def forbidden_read(*args: object, **kwargs: object) -> tuple[str | None, str | None]:
raise AssertionError(f"referenced-file inspection was called: {args!r} {kwargs!r}")
monkeypatch.setattr(verifier, "_hash_packet_reference", forbidden_read)
report = verifier.verify_packet(packet, trusted_certs=[local_tsa.certificate])
assert not report.signature_ok
[item] = report.items
assert not item.shared_media_ok
assert item.original_fixity_ok is False
assert "bundle signature invalid; referenced packet files were not read" in item.notes
def test_declared_original_must_exist_as_regular_file(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path, include_originals=True)
next((packet / "originals").iterdir()).unlink()
item = _only_item(packet, local_tsa)
assert item.original_fixity_ok is False
assert "embedded original file missing" in item.notes
def test_ordinary_packet_filenames_still_verify(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
) -> None:
_, packet = _make_packet(make_vault, make_jpeg, local_tsa, tmp_path, include_originals=True)
report = verifier.verify_packet(packet, trusted_certs=[local_tsa.certificate])
assert report.evidence_ready
assert report.items[0].shared_media_ok
assert report.items[0].original_fixity_ok is True