mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 06:17:25 +00:00
Masker: publication holds a per-folder flock so a parallel rerun never unlinks another masker's temp file; a failed output relay closes its pipe and ends the run UNSETTLED instead of returning 0 with cut output; early scrub refusals name the container (no-follow read of .cid). Helper: a short value holding a control character is refused by its key; the secret-file check requires a regular file via lstat; an escape past U+10FFFF stays literal text. Tasks: a deploy pass removes the top-level outputs an earlier run left unarchived (never a variant-* slot), so a stale JUnit cannot pass as the current run's. rerun-spec.sh refuses any TEST_E2E_PLAYWRIGHT_IMAGE override before the npm install container. README documents age-only pruning and the image refusal. Tests: vacuous assertions tightened (late CID exit status, declared APP_BASE_URL, started-but-unnamed container, expected report content); the external masked test compares 8-byte windows, skips only on identified network or registry failures and fails on a timeout. 🎯 Fix: 93 % 💥 Break: 3 % 💸 Tokens: 100 % ⏱️ Velocity: 100 % 🛡️ Security: 100 % 🏆 Score: 90 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
422 lines
15 KiB
Python
422 lines
15 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from ._spec_secrets_fakes import RAW, VALUE, WINDOW, load_masker
|
|
from ._spec_secrets_forms import (
|
|
BASIC,
|
|
BASIC_USERS,
|
|
ENCODER_TRIGGERS,
|
|
MARK_TRIGGERS,
|
|
NORMALIZED_TRIGGERS,
|
|
SNAPSHOT_TRIGGERS,
|
|
SPECIALS,
|
|
SPLIT_TRIGGERS,
|
|
TRIGGERS,
|
|
basic_header,
|
|
basic_leaks,
|
|
chunked,
|
|
form_leaks,
|
|
literal_leaks,
|
|
spaced_leaks,
|
|
)
|
|
|
|
UNDECODED_VALUES = (
|
|
" for data, _starts, _ends in views(form)\n",
|
|
" for data in [form]\n",
|
|
)
|
|
NO_ENCODINGS = (
|
|
" texts += [data for value in values for data in encoded_forms(value)]\n",
|
|
"",
|
|
)
|
|
HELD = 'held = max(len(buffer) - HOLD, buffer.rfind(b"\\n") + 1, 0)'
|
|
HOLD = 64 * WINDOW
|
|
SAMPLE = "q\"w'z\\yk&j<n>m%r+s t/u.v:x(é\U0001f600)"
|
|
UNSTRIPPED = ("if MARK.search(buffer) else", "if False else")
|
|
UNMARKED = ("for start, end in MARKS.values()", "for start, end in ()")
|
|
UNQUOTED = (" quoted_text,\n", "")
|
|
LT_ESCAPE = ('.replace("<", ', '.replace(">", ')
|
|
REGEX_ESCAPE = ('r"[.*+?^${}()|[', 'r"[')
|
|
NO_REPR = (
|
|
UNQUOTED,
|
|
("repr(text)[1:-1]", "text"),
|
|
("ascii(text)[1:-1]", "text"),
|
|
('repr(text.encode("utf-8"))[2:-1]', "text"),
|
|
)
|
|
DECODERS = {
|
|
"ansi": [' rb"(?P<ansi>\\x1b\\[[0-?]*[ -/]*[@-~])",\n', ["ansi_inverse_diff"]],
|
|
"sgr": [' rb"(?P<sgr>\\[[0-9;]*m)",\n', ["junit_sgr_remnant"]],
|
|
"hex": [
|
|
' rb"%(?P<hex>[0-9A-Fa-f]{2})",\n',
|
|
[
|
|
"percent_encoded",
|
|
"form_encoded",
|
|
"double_percent",
|
|
"form_then_percent",
|
|
"encode_uri_component",
|
|
"encode_uri",
|
|
"url_search_params",
|
|
],
|
|
],
|
|
"entity": [
|
|
' rb"&(?P<entity>#[0-9]{1,7}|#[xX][0-9A-Fa-f]{1,6}|[A-Za-z][A-Za-z0-9]{1,31});?",\n',
|
|
["xml", "json_in_xml_attribute", "html_decimal", "html_hex", "html_named"],
|
|
],
|
|
"unit": [
|
|
' rb"\\\\u(?P<unit>[0-9A-Fa-f]{4})",\n',
|
|
["json", "json_in_xml_attribute", "json_in_json", "json_unicode_escapes"],
|
|
],
|
|
"pair": [
|
|
' rb"\\\\u(?P<high>[dD][89abAB][0-9A-Fa-f]{2})\\\\u(?P<low>[dD][c-fC-F][0-9A-Fa-f]{2})",\n',
|
|
["json", "json_in_xml_attribute", "json_in_json", "json_unicode_escapes"],
|
|
],
|
|
"wide": [
|
|
' rb"\\\\(?:U|u\\{)(?P<wide>[0-9A-Fa-f]{1,8})\\}?",\n',
|
|
["js_code_point_escapes", "ascii_repr"],
|
|
],
|
|
"byte": [
|
|
' rb"\\\\x(?P<byte>[0-9A-Fa-f]{2})",\n',
|
|
["ascii_repr", "python_bytes_repr"],
|
|
],
|
|
"char": [
|
|
' rb"\\\\(?P<char>[ -/:-@\\[-`{-~bft])",\n',
|
|
[
|
|
"json",
|
|
"json_unicode",
|
|
"json_in_xml_attribute",
|
|
"single_quoted_inspect",
|
|
"json_in_json",
|
|
"ascii_repr",
|
|
"python_bytes_repr",
|
|
],
|
|
],
|
|
"css": [' rb"\\\\(?P<css>[0-9A-Fa-f]{1,6}) ?",\n', ["css_hex"]],
|
|
"plus": [
|
|
' rb"(?P<plus>\\+)",\n',
|
|
["form_encoded", "form_then_percent", "url_search_params"],
|
|
],
|
|
"quote": [" rb\"(?P<quote>'')\",\n", []],
|
|
}
|
|
NEW_RULE = (DECODERS["plus"][0], DECODERS["plus"][0] + ' rb"(?P<tilde>~)",\n')
|
|
NO_QUOTE = (
|
|
(DECODERS["quote"][0], ""),
|
|
(' "quote": (lambda text: text.replace("\'", "\'\'"),),\n', ""),
|
|
)
|
|
UNNORMALIZED = (
|
|
" values = list(dict.fromkeys([*values, *map(normalized, values)]))\n",
|
|
"",
|
|
)
|
|
CUTS = {
|
|
"start": ("\U0001f600ABCDEFG", "x\U0001f640ABCDEFG y\n"),
|
|
"end": ("ABCDEFG\U0001f600", "xABCDEFG\U0001f601 y\n"),
|
|
}
|
|
UNWIDENED = {
|
|
"start": (
|
|
(
|
|
" while start and buffer[start] & 0xC0 == 0x80:\n"
|
|
" start -= 1\n"
|
|
),
|
|
"",
|
|
),
|
|
"end": (
|
|
(
|
|
" while end < len(buffer) and buffer[end] & 0xC0 == 0x80:\n"
|
|
" end += 1\n"
|
|
),
|
|
"",
|
|
),
|
|
}
|
|
CUT_SHOWN = {
|
|
"start": "x\U0001f640".encode()[:4] + b"*** y\n",
|
|
"end": b"x***" + "\U0001f601".encode()[3:] + b" y\n",
|
|
}
|
|
BASIC_VALUE = "0123456789"
|
|
BASIC_SHOWN = {"a": "YT", "ab": "YWI6", "abc": "YWJj"}
|
|
BASIC_FORMS = '((b"", 0), (b":", 0), (b"\\0:", 2))'
|
|
COLONLESS = (BASIC_FORMS, '((b"", 0), (b"\\0", 4), (b"\\0\\0", 4))')
|
|
UNPADDED = ("value)[cut:]", "value)[cut : 4 * ((len(prefix) + len(value)) // 3)]")
|
|
SNAPSHOT_LINES = {
|
|
"ab'cdefgh": (
|
|
"- 'heading \"Token: ab''cdefgh\"'\n",
|
|
"- 'heading \"Token: ***\"'\n",
|
|
),
|
|
"abcd efgh": ('- heading "Token: abcd efgh"\n', '- heading "Token: ***"\n'),
|
|
"ab\N{ZERO WIDTH SPACE}cd\N{SOFT HYPHEN}efgh": (
|
|
'- heading "Token: abcdefgh"\n',
|
|
'- heading "Token: ***"\n',
|
|
),
|
|
}
|
|
|
|
|
|
def cut_line(module, side: str) -> bytes:
|
|
"""Mask a line whose character at one end of a match shares only some bytes with the value.
|
|
|
|
Args:
|
|
module: the loaded masker.
|
|
side: ``start`` or ``end`` of ``CUTS``, the end the match cuts.
|
|
|
|
Returns:
|
|
The masked line.
|
|
"""
|
|
value, line = CUTS[side]
|
|
stream = module.Stream(module.windows([value.encode()]))
|
|
return stream.feed(line.encode(), final=True)
|
|
|
|
|
|
def basic_line(module, user: str) -> bytes:
|
|
"""Mask the ``Basic`` header of a user and ``BASIC_VALUE``.
|
|
|
|
Args:
|
|
module: the loaded masker.
|
|
user: the user; behind ``abc:`` the last group is padded.
|
|
|
|
Returns:
|
|
The masked header line.
|
|
"""
|
|
_encoded, line = basic_header(user, BASIC_VALUE.encode())
|
|
stream = module.Stream(module.windows([BASIC_VALUE.encode()]))
|
|
return stream.feed(line, final=True)
|
|
|
|
|
|
def snapshot_leaks(module) -> list[str]:
|
|
"""Mask the ARIA snapshot line a ``toMatchAriaSnapshot`` failure prints.
|
|
|
|
Args:
|
|
module: the loaded masker.
|
|
|
|
Returns:
|
|
The values of ``SNAPSHOT_LINES`` whose line is not masked as expected.
|
|
"""
|
|
found = []
|
|
for value, (line, expected) in SNAPSHOT_LINES.items():
|
|
stream = module.Stream(module.windows([value.encode()]))
|
|
if stream.feed(line.encode(), final=True) != expected.encode():
|
|
found.append(value)
|
|
return found
|
|
|
|
|
|
def mapping_gaps(module) -> list[str]:
|
|
"""Hold every decoder rule against the encoders that write its escape.
|
|
|
|
Args:
|
|
module: the loaded masker.
|
|
|
|
Returns:
|
|
The rule names of ``ESCAPES`` without an entry in ``ENCODERS`` or
|
|
``MARKS`` and the entries without a rule, else each encoder whose
|
|
form of ``SAMPLE`` holds no escape of its rule or that no view
|
|
decodes back to ``SAMPLE``.
|
|
"""
|
|
names = module.windows.__globals__
|
|
escape, encoders, marks = names["ESCAPE"], names["ENCODERS"], names["MARKS"]
|
|
kinds, mirrored = set(escape.groupindex) - {"high"}, set(encoders) | set(marks)
|
|
if kinds != mirrored:
|
|
return sorted(kinds ^ mirrored)
|
|
forms = [
|
|
(kind, encode(SAMPLE)) for kind, found in encoders.items() for encode in found
|
|
]
|
|
forms += [
|
|
(kind, f"{start}{SAMPLE[0]}{end}{SAMPLE[1:]}")
|
|
for kind, (start, end) in marks.items()
|
|
]
|
|
gaps = []
|
|
for kind, form in forms:
|
|
data = form.encode()
|
|
found = {match.lastgroup for match in escape.finditer(data)}
|
|
decoded = {view for view, _starts, _ends in names["views"](data)}
|
|
if kind not in found or SAMPLE.encode() not in decoded:
|
|
gaps.append(f"{kind}: {form!r}")
|
|
return gaps
|
|
|
|
|
|
class TestSpecSecretsEncoding(unittest.TestCase):
|
|
def test_every_form_a_value_reaches_the_output_in_is_masked(self) -> None:
|
|
module = load_masker()
|
|
self.assertEqual(form_leaks(module, VALUE), [])
|
|
self.assertEqual(basic_leaks(module), [])
|
|
self.assertFalse(spaced_leaks(module))
|
|
|
|
def test_a_value_that_holds_escapes_itself_is_masked_in_every_form(self) -> None:
|
|
self.assertEqual(literal_leaks(load_masker()), [])
|
|
|
|
def test_a_nine_byte_value_of_every_escaped_class_is_masked_in_every_form(
|
|
self,
|
|
) -> None:
|
|
self.assertEqual(len(SPECIALS.encode()), 9)
|
|
self.assertEqual(form_leaks(load_masker(), SPECIALS), [])
|
|
|
|
def test_each_decoder_rule_has_an_encoder_whose_form_it_decodes(self) -> None:
|
|
self.assertEqual(mapping_gaps(load_masker()), [])
|
|
|
|
def test_an_escape_past_unicode_is_left_as_text_and_stops_no_scan(self) -> None:
|
|
names = load_masker().windows.__globals__
|
|
for escape in (
|
|
b"\\UFFFFFFFF",
|
|
b"\\U80000000",
|
|
b"\\U00110000",
|
|
b"\\u{FFFFFFFF}",
|
|
):
|
|
with self.subTest(escape=escape):
|
|
data = b"secret-" + escape + b"-tail"
|
|
self.assertIn(data, {view for view, _s, _e in names["views"](data)})
|
|
self.assertTrue(names["windows"]([data]))
|
|
|
|
def test_an_aria_snapshot_masks_a_doubled_quote_and_a_normalised_name(
|
|
self,
|
|
) -> None:
|
|
self.assertEqual(snapshot_leaks(load_masker()), [])
|
|
|
|
def test_a_value_across_feeds_is_masked_and_held_back_bytes_stay_few(self) -> None:
|
|
module = load_masker()
|
|
for size in (1, 5, 4096):
|
|
with self.subTest(size=size):
|
|
self.assertTrue(chunked(module, size))
|
|
stream = module.Stream(module.windows([RAW]))
|
|
for _ in range(HOLD):
|
|
stream.feed(RAW[:13])
|
|
self.assertLessEqual(len(stream.pending), HOLD)
|
|
stream = module.Stream(module.windows([RAW]))
|
|
self.assertEqual(stream.feed(b"started\n"), b"started\n")
|
|
|
|
def test_a_mask_never_cuts_a_utf8_character_in_two(self) -> None:
|
|
module = load_masker()
|
|
for side in CUTS:
|
|
with self.subTest(side=side):
|
|
self.assertEqual(cut_line(module, side).decode(), "x*** y\n")
|
|
|
|
def test_a_basic_header_shows_no_value_byte_behind_a_user_of_any_length(
|
|
self,
|
|
) -> None:
|
|
module = load_masker()
|
|
for user, shown in BASIC_SHOWN.items():
|
|
with self.subTest(user=user):
|
|
self.assertEqual(
|
|
basic_line(module, user),
|
|
f"{BASIC}{shown}***\n".encode(),
|
|
)
|
|
|
|
|
|
class TestSpecSecretsEncodingNegativeControls(unittest.TestCase):
|
|
def test_a_mask_that_starts_or_ends_inside_a_character_is_caught(self) -> None:
|
|
for side, unwidened in UNWIDENED.items():
|
|
with self.subTest(side=side):
|
|
self.assertEqual(
|
|
cut_line(load_masker(unwidened), side), CUT_SHOWN[side]
|
|
)
|
|
|
|
def test_base64_forms_cut_before_the_padded_last_group_are_caught(self) -> None:
|
|
module = load_masker(UNPADDED)
|
|
self.assertEqual(basic_line(module, "abc"), f"{BASIC}YWJj***ODk=\n".encode())
|
|
self.assertEqual(basic_leaks(module), list(BASIC_USERS))
|
|
|
|
def test_base64_forms_without_the_colon_of_a_basic_header_are_caught(
|
|
self,
|
|
) -> None:
|
|
module = load_masker(COLONLESS)
|
|
self.assertEqual(basic_line(module, "abc"), f"{BASIC}YWJjOjAx***\n".encode())
|
|
self.assertEqual(basic_leaks(module), ["abc", "a", "user", "admin:x"])
|
|
|
|
def test_each_form_leaks_without_its_decoder(self) -> None:
|
|
for name, (line, leaking) in DECODERS.items():
|
|
with self.subTest(decoder=name):
|
|
decoderless = load_masker((line, ""), NO_ENCODINGS)
|
|
self.assertEqual(form_leaks(decoderless, VALUE), leaking)
|
|
decoderless = load_masker((DECODERS["hex"][0], ""), NO_ENCODINGS)
|
|
self.assertTrue(spaced_leaks(decoderless))
|
|
for old, new, leaking in (
|
|
(
|
|
"DEPTH = 2",
|
|
"DEPTH = 1",
|
|
[
|
|
"json_in_xml_attribute",
|
|
"double_percent",
|
|
"form_then_percent",
|
|
"json_in_json",
|
|
],
|
|
),
|
|
(
|
|
"for form in {value} | base64_forms(value)",
|
|
"for form in {value}",
|
|
["base64", "base64_urlsafe_unpadded"],
|
|
),
|
|
(" | hex_forms(value)", "", ["node_buffer", "hex_unspaced"]),
|
|
(
|
|
'if kind == "hex" or (kind == "byte" and octets):',
|
|
'if kind == "hex":',
|
|
["python_bytes_repr"],
|
|
),
|
|
):
|
|
with self.subTest(old=old):
|
|
changed = load_masker((old, new), NO_ENCODINGS)
|
|
self.assertEqual(form_leaks(changed, VALUE), leaking)
|
|
|
|
def test_a_rule_without_an_encoder_or_an_encoder_without_a_rule_is_caught(
|
|
self,
|
|
) -> None:
|
|
self.assertEqual(mapping_gaps(load_masker(NEW_RULE)), ["tilde"])
|
|
renamed = load_masker(('"css": (', '"cssx": ('))
|
|
self.assertEqual(mapping_gaps(renamed), ["css", "cssx"])
|
|
|
|
def test_each_trigger_leaks_once_its_encoder_is_dropped(self) -> None:
|
|
for replacements, leaking in (
|
|
((UNQUOTED,), "nine_bytes_quote_in_a_locator"),
|
|
(NO_REPR, "nine_bytes_quotes_in_a_python_repr"),
|
|
((LT_ESCAPE,), "nine_bytes_lt_in_html_safe_json"),
|
|
((REGEX_ESCAPE,), "nine_bytes_dot_in_a_regex"),
|
|
):
|
|
with self.subTest(trigger=leaking):
|
|
self.assertIn(leaking, literal_leaks(load_masker(*replacements)))
|
|
|
|
def test_a_doubled_quote_leaks_without_its_rule_and_encoder(self) -> None:
|
|
unquoted = load_masker(*NO_QUOTE)
|
|
self.assertLessEqual(set(SNAPSHOT_TRIGGERS), set(literal_leaks(unquoted)))
|
|
self.assertEqual(snapshot_leaks(unquoted), ["ab'cdefgh"])
|
|
ruleless = load_masker(NO_QUOTE[0], NO_ENCODINGS)
|
|
self.assertEqual(snapshot_leaks(ruleless), ["ab'cdefgh"])
|
|
|
|
def test_a_normalised_name_leaks_without_the_normalised_windows(self) -> None:
|
|
unnormalized = load_masker(UNNORMALIZED)
|
|
self.assertLessEqual(set(NORMALIZED_TRIGGERS), set(literal_leaks(unnormalized)))
|
|
self.assertEqual(snapshot_leaks(unnormalized), list(SNAPSHOT_LINES)[1:])
|
|
|
|
def test_a_highlight_leaks_without_the_stripped_view_or_the_marked_forms(
|
|
self,
|
|
) -> None:
|
|
unstripped = literal_leaks(load_masker(UNSTRIPPED))
|
|
self.assertIn("nine_bytes_two_letters_highlighted", unstripped)
|
|
self.assertNotIn("nine_bytes_one_letter_highlighted", unstripped)
|
|
bare = set(literal_leaks(load_masker(UNSTRIPPED, UNMARKED)))
|
|
self.assertLessEqual(set(MARK_TRIGGERS), bare)
|
|
|
|
def test_windows_cut_from_the_undecoded_value_alone_are_caught(self) -> None:
|
|
undecoded = load_masker(UNDECODED_VALUES, NO_ENCODINGS)
|
|
self.assertLessEqual(set(TRIGGERS), set(literal_leaks(undecoded)))
|
|
|
|
def test_windows_only_from_decoded_output_are_caught(self) -> None:
|
|
decoded_only = load_masker(NO_ENCODINGS)
|
|
self.assertLessEqual(
|
|
set(SPLIT_TRIGGERS) | set(ENCODER_TRIGGERS),
|
|
set(literal_leaks(decoded_only)),
|
|
)
|
|
|
|
def test_a_basic_header_behind_an_unaligned_user_is_caught(self) -> None:
|
|
module = load_masker((BASIC_FORMS, '((b"", 0),)'))
|
|
self.assertEqual(basic_leaks(module), ["abc", "a", "user", "admin:x"])
|
|
|
|
def test_a_stream_without_hold_back_carry_or_line_release_is_caught(
|
|
self,
|
|
) -> None:
|
|
held = load_masker((HELD, "held = len(buffer)"))
|
|
self.assertFalse(chunked(held, 5))
|
|
carried = load_masker(
|
|
("done, carry = min(end, cut), max(end - cut, 0)", "done = min(end, cut)")
|
|
)
|
|
self.assertFalse(chunked(carried, 1))
|
|
lagging = load_masker(('buffer.rfind(b"\\n") + 1, ', ""))
|
|
stream = lagging.Stream(lagging.windows([RAW]))
|
|
self.assertNotEqual(stream.feed(b"started\n"), b"started\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|