|
8 | 8 | import re |
9 | 9 | import time |
10 | 10 | import unicodedata |
| 11 | +from bisect import bisect_right |
11 | 12 | from collections import deque |
12 | 13 | from collections.abc import Iterator |
13 | 14 | from dataclasses import dataclass, field |
14 | 15 |
|
15 | 16 | from skillspector.artifacts import ( |
16 | 17 | ContentKind, |
| 18 | + SecurityTextView, |
17 | 19 | _concealed_instruction_run_spans, |
18 | 20 | _contextual_default_ignorable_boundary_spans, |
19 | 21 | _obfuscated_instruction_matches, |
|
87 | 89 | } |
88 | 90 | ) |
89 | 91 | _MAX_AMBIGUOUS_PROMPT_PHRASE = 512 |
| 92 | +_MAX_IRREGULAR_SPACING_FRAGMENT_GAP = 2 |
90 | 93 | _IDENTIFIER_RELAXATION = str.maketrans({"_": " ", **{str(value): " " for value in range(10)}}) |
91 | 94 | _PROJECTED_PROMPT_PATTERNS = tuple( |
92 | 95 | pattern for pattern, _confidence in (*COMPILED_P3_PATTERNS, *COMPILED_P4_PATTERNS) |
@@ -667,9 +670,76 @@ def _projected_prompt_injection_line( |
667 | 670 | source_offset = reconstructed_gaps[0][0] |
668 | 671 | if first_offset is None or source_offset < first_offset: |
669 | 672 | first_offset = source_offset |
| 673 | + |
| 674 | + irregular_projection = _irregular_spacing_prompt_projection(view) |
| 675 | + if irregular_projection is not None: |
| 676 | + irregular_text, join_points = irregular_projection |
| 677 | + relaxed_irregular_text = irregular_text.translate(_IDENTIFIER_RELAXATION) |
| 678 | + irregular_texts = ( |
| 679 | + (irregular_text, relaxed_irregular_text) |
| 680 | + if relaxed_irregular_text != irregular_text |
| 681 | + else (irregular_text,) |
| 682 | + ) |
| 683 | + candidate_offsets = tuple(point[0] for point in join_points) |
| 684 | + for projected_text in irregular_texts: |
| 685 | + for pattern in _PROJECTED_PROMPT_PATTERNS: |
| 686 | + budget.check_runtime() |
| 687 | + for match in pattern.finditer(projected_text): |
| 688 | + budget.check_runtime() |
| 689 | + point_index = bisect_right(candidate_offsets, match.start()) |
| 690 | + if ( |
| 691 | + point_index >= len(join_points) |
| 692 | + or join_points[point_index][0] >= match.end() |
| 693 | + ): |
| 694 | + continue |
| 695 | + source_offset = join_points[point_index][1] |
| 696 | + if first_offset is None or source_offset < first_offset: |
| 697 | + first_offset = source_offset |
670 | 698 | return get_line_number(content, first_offset) if first_offset is not None else None |
671 | 699 |
|
672 | 700 |
|
| 701 | +def _irregular_spacing_prompt_projection( |
| 702 | + view: SecurityTextView, |
| 703 | +) -> tuple[str, tuple[tuple[int, int], ...]] | None: |
| 704 | + """Join only short gaps that split adjacent reconstructed fragments. |
| 705 | +
|
| 706 | + The primary projection preserves a width change because fully letter-spaced |
| 707 | + phrases use wider gaps as explicit word boundaries. An alternating one/two |
| 708 | + character gap can therefore split a short action into two reconstructed |
| 709 | + fragments. Joining only short gaps *between* those proven fragments creates |
| 710 | + a fail-closed AE6 view without erasing the wider observed word boundaries. |
| 711 | + """ |
| 712 | + if view.source_offsets is None or len(view.reconstructions) < 2: |
| 713 | + return None |
| 714 | + |
| 715 | + removable_gaps: list[tuple[int, int]] = [] |
| 716 | + for left, right in zip(view.reconstructions, view.reconstructions[1:], strict=False): |
| 717 | + gap_start = left.derived_end |
| 718 | + gap_end = right.derived_start |
| 719 | + gap = view.text[gap_start:gap_end] |
| 720 | + if ( |
| 721 | + 0 < len(gap) <= _MAX_IRREGULAR_SPACING_FRAGMENT_GAP |
| 722 | + and gap.isspace() |
| 723 | + and not any(character in LINE_BREAK_CHARS for character in gap) |
| 724 | + ): |
| 725 | + removable_gaps.append((gap_start, gap_end)) |
| 726 | + if not removable_gaps: |
| 727 | + return None |
| 728 | + |
| 729 | + parts: list[str] = [] |
| 730 | + join_points: list[tuple[int, int]] = [] |
| 731 | + cursor = 0 |
| 732 | + projected_length = 0 |
| 733 | + for gap_start, gap_end in removable_gaps: |
| 734 | + part = view.text[cursor:gap_start] |
| 735 | + parts.append(part) |
| 736 | + projected_length += len(part) |
| 737 | + join_points.append((projected_length, view.source_offset(gap_start))) |
| 738 | + cursor = gap_end |
| 739 | + parts.append(view.text[cursor:]) |
| 740 | + return "".join(parts), tuple(join_points) |
| 741 | + |
| 742 | + |
673 | 743 | def _text_signals( |
674 | 744 | content: str, |
675 | 745 | budget: _ArtifactIntegrityBudget, |
|
0 commit comments