Skip to content

Commit 4d65792

Browse files
yashrajp22github-actions[bot]rng1995
authored
fix(analyzer): enforce deadlines for projected prompt matching (#742)
* fix(security): enforce deadlines for projected prompt matching Signed-off-by: yashrajbasav <[email protected]> * fix(analyzers): isolate projected matching deadlines from caller CPU Signed-off-by: yashrajbasav <[email protected]> * test(analyzers): model per-search CPU and exhausted iterators Signed-off-by: yashrajbasav <[email protected]> * style(tests): format projected matcher regression cases Signed-off-by: yashrajbasav <[email protected]> --------- Signed-off-by: yashrajbasav <[email protected]> Signed-off-by: Narendran Raghavan <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Narendran Raghavan <[email protected]>
1 parent 415d507 commit 4d65792

2 files changed

Lines changed: 187 additions & 52 deletions

File tree

‎src/skillspector/nodes/analyzers/artifact_integrity.py‎

Lines changed: 78 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@
9898
_PROJECTED_PROMPT_PATTERNS = tuple(
9999
pattern for pattern, _confidence in (*COMPILED_P3_PATTERNS, *COMPILED_P4_PATTERNS)
100100
)
101-
# Removing line breaks can give the existing wildcard patterns a much longer
102-
# search space. Interrupt the regex itself, not just work between matches.
101+
# Reconstructed projections can give wildcard patterns a much longer search
102+
# space. Interrupt the regex itself, not just work between matches.
103103
_MULTILINE_PROMPT_PATTERN_SECONDS = 0.25
104104
_MULTILINE_PROMPT_PATTERNS = tuple(
105105
regex.compile(pattern.pattern, regex.ASCII | regex.IGNORECASE | regex.MULTILINE)
@@ -682,9 +682,9 @@ def _projected_prompt_injection_line(
682682
else (view.text,)
683683
)
684684
for projected_text in projected_texts:
685-
for pattern in _PROJECTED_PROMPT_PATTERNS:
686-
budget.check_runtime()
687-
match = pattern.search(projected_text)
685+
matching_text = _multiline_prompt_matching_text(projected_text, budget)
686+
for pattern in _MULTILINE_PROMPT_PATTERNS:
687+
match = next(_timed_prompt_matches(pattern, matching_text, budget), None)
688688
if match is None:
689689
continue
690690
reconstructed_gaps = view.reconstructed_source_spans(match.start(), match.end())
@@ -705,9 +705,9 @@ def _projected_prompt_injection_line(
705705
)
706706
candidate_offsets = tuple(point[0] for point in join_points)
707707
for projected_text in irregular_texts:
708-
for pattern in _PROJECTED_PROMPT_PATTERNS:
709-
budget.check_runtime()
710-
for match in pattern.finditer(projected_text):
708+
matching_text = _multiline_prompt_matching_text(projected_text, budget)
709+
for pattern in _MULTILINE_PROMPT_PATTERNS:
710+
for match in _timed_prompt_matches(pattern, matching_text, budget):
711711
budget.check_runtime()
712712
point_index = bisect_right(candidate_offsets, match.start())
713713
if (
@@ -759,6 +759,57 @@ def _multiline_prompt_matching_text(text: str, budget: _ArtifactIntegrityBudget)
759759
return "".join(parts)
760760

761761

762+
def _timed_prompt_matches(
763+
pattern: regex.Pattern[str],
764+
text: str,
765+
budget: _ArtifactIntegrityBudget,
766+
) -> Iterator[regex.Match[str]]:
767+
"""Apply the same interruptible matching budget to every prompt projection."""
768+
matching_seconds = 0.0
769+
matching_limit = _MULTILINE_PROMPT_PATTERN_SECONDS
770+
terminal_timeout = False
771+
start = 0
772+
skip_empty = False
773+
while True:
774+
budget.check_runtime()
775+
remaining = transitive_remaining_seconds(budget.state)
776+
if remaining is not None:
777+
workflow_limit = matching_seconds + max(0.0, remaining)
778+
# A per-pattern cap ends this artifact; workflow exhaustion remains terminal.
779+
if workflow_limit <= matching_limit:
780+
matching_limit = workflow_limit
781+
terminal_timeout = True
782+
timeout = matching_limit - matching_seconds
783+
if timeout <= 0:
784+
raise _ArtifactIntegrityResourceLimitError(
785+
LedgerReason.RUNTIME_LIMIT,
786+
{"observed_seconds": matching_seconds, "limit_seconds": matching_limit},
787+
terminal=terminal_timeout,
788+
)
789+
started_at = time.thread_time()
790+
expired = False
791+
try:
792+
# Restart across yields so consumer work never uses the match budget.
793+
matches = pattern.finditer(text, pos=start, timeout=timeout, concurrent=False)
794+
if skip_empty:
795+
next(matches, None)
796+
match = next(matches, None)
797+
except TimeoutError:
798+
expired = True
799+
finally:
800+
matching_seconds += max(0.0, time.thread_time() - started_at)
801+
budget.check_runtime()
802+
if expired:
803+
# regex counts process CPU, including native work in other threads.
804+
# Retry only the same search; the thread and workflow budgets still bound it.
805+
continue
806+
if match is None:
807+
return
808+
start = match.end()
809+
skip_empty = match.start() == start
810+
yield match
811+
812+
762813
def _multiline_prompt_injection_line(
763814
content: str,
764815
budget: _ArtifactIntegrityBudget,
@@ -770,49 +821,27 @@ def _multiline_prompt_injection_line(
770821
matching_text = _multiline_prompt_matching_text(view.text, budget)
771822
first_offset: int | None = None
772823
for pattern in _MULTILINE_PROMPT_PATTERNS:
773-
budget.check_runtime()
774-
remaining = transitive_remaining_seconds(budget.state)
775-
timeout = _MULTILINE_PROMPT_PATTERN_SECONDS
776-
terminal_timeout = False
777-
if remaining is not None:
778-
terminal_timeout = remaining <= timeout
779-
timeout = min(timeout, max(0.0, remaining))
780-
started_at = time.monotonic()
781824
reconstruction_index = 0
782-
try:
783-
# Keep this short, interruptible search on the current thread.
784-
# Releasing the GIL lets another analyzer consume its wall-clock
785-
# allowance and turn ordinary prose into a false timeout.
786-
for match in pattern.finditer(matching_text, timeout=timeout, concurrent=False):
825+
for match in _timed_prompt_matches(pattern, matching_text, budget):
826+
# Matches and reconstruction spans are both ordered. Advance
827+
# once per span, including ordinary matches before a spaced
828+
# instruction, instead of rescanning all provenance per match.
829+
while (
830+
reconstruction_index < len(view.reconstructions)
831+
and view.reconstructions[reconstruction_index].derived_end <= match.start() + 1
832+
):
787833
budget.check_runtime()
788-
# Matches and reconstruction spans are both ordered. Advance
789-
# once per span, including ordinary matches before a spaced
790-
# instruction, instead of rescanning all provenance per match.
791-
while (
792-
reconstruction_index < len(view.reconstructions)
793-
and view.reconstructions[reconstruction_index].derived_end <= match.start() + 1
794-
):
795-
budget.check_runtime()
796-
reconstruction_index += 1
797-
if reconstruction_index == len(view.reconstructions):
798-
break
799-
reconstruction = view.reconstructions[reconstruction_index]
800-
right = max(match.start() + 1, reconstruction.derived_start + 1)
801-
if right < min(match.end(), reconstruction.derived_end):
802-
source_offset = view.source_offset(right - 1) + 1
803-
if first_offset is None or source_offset < first_offset:
804-
first_offset = source_offset
805-
# Later matches cannot precede this pattern's first gap.
806-
break
807-
except TimeoutError as exc:
808-
raise _ArtifactIntegrityResourceLimitError(
809-
LedgerReason.RUNTIME_LIMIT,
810-
{
811-
"observed_seconds": max(0.0, time.monotonic() - started_at),
812-
"limit_seconds": timeout,
813-
},
814-
terminal=terminal_timeout,
815-
) from exc
834+
reconstruction_index += 1
835+
if reconstruction_index == len(view.reconstructions):
836+
break
837+
reconstruction = view.reconstructions[reconstruction_index]
838+
right = max(match.start() + 1, reconstruction.derived_start + 1)
839+
if right < min(match.end(), reconstruction.derived_end):
840+
source_offset = view.source_offset(right - 1) + 1
841+
if first_offset is None or source_offset < first_offset:
842+
first_offset = source_offset
843+
# Later matches cannot precede this pattern's first gap.
844+
break
816845
return get_line_number(content, first_offset) if first_offset is not None else None
817846

818847

‎tests/nodes/analyzers/test_multiline_prompt_spacing.py‎

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ def test_multiline_regex_timeout_is_scoped_to_current_artifact(
196196
monkeypatch: pytest.MonkeyPatch,
197197
) -> None:
198198
timeouts = []
199+
clock = iter((0.0, artifact_integrity._MULTILINE_PROMPT_PATTERN_SECONDS + 0.02))
200+
monkeypatch.setattr(artifact_integrity.time, "thread_time", lambda: next(clock))
199201

200202
class ExpiredPattern:
201-
def finditer(self, _text, *, timeout, concurrent):
203+
def finditer(self, _text, *, pos, timeout, concurrent):
202204
timeouts.append(timeout)
203205
raise TimeoutError("regex timed out")
204206

@@ -232,8 +234,11 @@ def finditer(self, _text, *, timeout, concurrent):
232234
def test_multiline_regex_timeout_preserves_global_terminal_limit(
233235
monkeypatch: pytest.MonkeyPatch,
234236
) -> None:
237+
clock = iter((0.0, 0.02))
238+
monkeypatch.setattr(artifact_integrity.time, "thread_time", lambda: next(clock))
239+
235240
class ExpiredPattern:
236-
def finditer(self, _text, *, timeout, concurrent):
241+
def finditer(self, _text, *, pos, timeout, concurrent):
237242
raise TimeoutError("global deadline reached")
238243

239244
monkeypatch.setattr(artifact_integrity, "_MULTILINE_PROMPT_PATTERNS", (ExpiredPattern(),))
@@ -413,7 +418,9 @@ def compete_for_gil() -> None:
413418
class ContendedPattern:
414419
def finditer(self, text, **kwargs):
415420
matches = original.finditer(text, **kwargs)
416-
first = next(matches)
421+
first = next(matches, None)
422+
if first is None:
423+
return
417424
start_work.set()
418425
yield first
419426
yield from matches
@@ -460,3 +467,102 @@ async def test_parallel_scan_of_contractions_remains_complete(tmp_path: Path) ->
460467
assert result["analysis_completeness"]["is_complete"] is True
461468
mcp = await run_scan(str(tmp_path), use_llm=False)
462469
assert mcp["safe_to_install"] is True
470+
471+
472+
@pytest.mark.parametrize("irregular", [False, True])
473+
def test_projected_prompt_regex_timeout_records_incomplete_coverage(
474+
monkeypatch: pytest.MonkeyPatch, irregular: bool
475+
) -> None:
476+
calls = []
477+
clock = iter((0.0, 0.02, 0.02, 0.04))
478+
monkeypatch.setattr(artifact_integrity.time, "thread_time", lambda: next(clock))
479+
monkeypatch.setattr(
480+
artifact_integrity,
481+
"_multiline_prompt_injection_line",
482+
lambda *_args: pytest.fail("ordinary projection reached the multiline fallback"),
483+
)
484+
485+
class ExpiredPattern:
486+
def finditer(self, text, *, pos, timeout, concurrent):
487+
calls.append((timeout, concurrent))
488+
# Exercise the irregular projection after the normal search.
489+
if irregular and len(calls) == 1:
490+
return iter(())
491+
raise TimeoutError("regex timed out")
492+
493+
monkeypatch.setattr(artifact_integrity, "_MULTILINE_PROMPT_PATTERNS", (ExpiredPattern(),))
494+
monkeypatch.setattr(artifact_integrity, "transitive_remaining_seconds", lambda _state: 0.01)
495+
if irregular:
496+
monkeypatch.setattr(
497+
artifact_integrity,
498+
"_irregular_spacing_prompt_projection",
499+
lambda _view: ("without telling the user", ((1, 0),)),
500+
)
501+
content = "w i t h o u t t e l l i n g u s e r"
502+
result = node({"components": ["SKILL.md"], "file_cache": {"SKILL.md": content}})
503+
assert len(calls) == (2 if irregular else 1)
504+
assert all(call == (0.01, False) for call in calls)
505+
assert all(event["outcome"] == "partial" for event in result["inspection_ledger"])
506+
assert all(
507+
event["reason_code"] == LedgerReason.RUNTIME_LIMIT for event in result["inspection_ledger"]
508+
)
509+
510+
511+
def test_repeated_projected_prompt_prefix_has_engine_deadline(
512+
monkeypatch: pytest.MonkeyPatch,
513+
) -> None:
514+
monkeypatch.setattr(artifact_integrity, "_MULTILINE_PROMPT_PATTERN_SECONDS", 0.000001)
515+
monkeypatch.setattr(
516+
artifact_integrity,
517+
"_multiline_prompt_injection_line",
518+
lambda *_args: pytest.fail("ordinary projection reached the multiline fallback"),
519+
)
520+
content = "w i t h o u t t e l l i n g u s e r " * 1000
521+
with pytest.raises(artifact_integrity._ArtifactIntegrityResourceLimitError) as caught:
522+
artifact_integrity._projected_prompt_injection_line(
523+
content, artifact_integrity._ArtifactIntegrityBudget({})
524+
)
525+
assert caught.value.reason == LedgerReason.RUNTIME_LIMIT
526+
527+
528+
def test_timed_prompt_matches_excludes_consumer_cpu(monkeypatch: pytest.MonkeyPatch) -> None:
529+
import time
530+
531+
import regex
532+
533+
monkeypatch.setattr(artifact_integrity, "_MULTILINE_PROMPT_PATTERN_SECONDS", 0.01)
534+
matches = artifact_integrity._timed_prompt_matches(
535+
regex.compile(r".*?"), "ab", artifact_integrity._ArtifactIntegrityBudget({})
536+
)
537+
first = next(matches)
538+
until = time.thread_time() + 0.03
539+
while time.thread_time() < until:
540+
pass
541+
assert [first.span(), *(match.span() for match in matches)] == [
542+
(0, 0),
543+
(0, 1),
544+
(1, 1),
545+
(1, 2),
546+
(2, 2),
547+
]
548+
549+
550+
def test_timed_prompt_matches_retries_other_thread_cpu(monkeypatch: pytest.MonkeyPatch) -> None:
551+
import regex
552+
553+
calls = 0
554+
original = regex.compile("x")
555+
556+
class ContendedPattern:
557+
def finditer(self, text, **kwargs):
558+
nonlocal calls
559+
calls += 1
560+
if calls == 1:
561+
raise TimeoutError("another thread used process CPU")
562+
return original.finditer(text, **kwargs)
563+
564+
matches = artifact_integrity._timed_prompt_matches(
565+
ContendedPattern(), "x", artifact_integrity._ArtifactIntegrityBudget({})
566+
)
567+
assert [match.span() for match in matches] == [(0, 1)]
568+
assert calls == 3

0 commit comments

Comments
 (0)