@@ -252,8 +252,13 @@ def _compile_prompt_patterns(
252252 r"(?=.{1,60}\Z)(?:\(c\)\s+|©\s+)?\d{1,4}(?:\s*-\s*\d{1,4})?"
253253 r"\s+(?:" + _P2_NAME_TOKEN + r"\s+){0,5}" + _P2_NAME_TOKEN + r"\.?"
254254)
255- _P2_SPDX_ID = r"[A-Za-z0-9.+\-]{1,24}"
256- _P2_SPDX_ATOM = _P2_SPDX_ID + r"(?:\s+(?:OR|AND|WITH)\s+" + _P2_SPDX_ID + r")*"
255+ # SPDX ids allow at most two hyphens (length is scoped to the id run so
256+ # chains and trailing periods never shrink it); chains cap at three ids.
257+ _P2_SPDX_ID = (
258+ r"(?=[A-Za-z0-9.+\-]{1,24}(?![A-Za-z0-9.+\-]))"
259+ r"[A-Za-z0-9.+]+(?:-[A-Za-z0-9.+]+){0,2}"
260+ )
261+ _P2_SPDX_ATOM = _P2_SPDX_ID + r"(?:\s+(?:OR|AND|WITH)\s+" + _P2_SPDX_ID + r"){0,2}"
257262_P2_LICENSE_LINE_RES = (
258263 re .compile (
259264 r"\ACopyright\s+" + _P2_COPYRIGHT_CORE + r"\Z" ,
@@ -274,6 +279,8 @@ def _compile_prompt_patterns(
274279 re .IGNORECASE ,
275280 ),
276281)
282+ _P2_COPYRIGHT_LINE_INDEX = 0
283+ _P2_SPDX_LINE_INDEX = 2
277284_P2_OVERRIDE_EXTRA = re .compile (
278285 r"system\s+prompt|respond\s+as|override\s+instructions?|you\s+must" ,
279286 re .IGNORECASE ,
@@ -295,6 +302,21 @@ def _compile_prompt_patterns(
295302_P2_TRIGGER_STANDALONE = re .compile (
296303 r"\b(system|instructions?|ignore|post|get|send|transmit)\b" , re .IGNORECASE
297304)
305+
306+
307+ def _p2_trigger_scan_text (text : str ) -> str :
308+ """Return a de-obfuscated copy of ``text`` for trigger/danger scans.
309+
310+ camelCase joints, underscores, and digits become spaces, so
311+ IgnorePriorInstructions scans as Ignore Prior Instructions.
312+ Grammars always match the raw text; only the scans use the copy.
313+ Single benign words (PostgreSQL, Systems) stay whole-word clean.
314+ """
315+ text = re .sub (r"(?<=[a-z0-9])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])" , " " , text )
316+ text = re .sub (r"[0-9_]+" , " " , text )
317+ return re .sub (r"\s+" , " " , text )
318+
319+
298320# Metadata keys observed in benign skill headers plus obvious header keys.
299321# Matching is exact (case-insensitive): bare instruction words such as
300322# system, instructions, or ignore are never here.
@@ -333,7 +355,14 @@ def _compile_prompt_patterns(
333355# requirements" and "get started" are keys. Known residuals: a bare
334356# triggerless package name ("requires: numpy") parses as a package;
335357# short triggerless SPDX OR-chains fit the atom cap.
336- _P2_REQ_NAME = r"[A-Za-z0-9_.\-@/]+"
358+ # Requirement names take a package-name shape: optional @scope/, at most
359+ # 40 characters, at most two -/_/. separators — a hyphen-joined sentence
360+ # never counts as one bare name. Length is scoped to the name run so
361+ # comma-separated lists never shrink it.
362+ _P2_REQ_NAME = (
363+ r"(?=[A-Za-z0-9_.@/\-]{1,40}(?![A-Za-z0-9_.@/\-]))"
364+ r"(?:@[A-Za-z0-9_.]+/)?[A-Za-z0-9_.]+(?:[-/.][A-Za-z0-9_.]+){0,2}"
365+ )
337366_P2_REQ_VER = r"v?\d+(?:\.\d+){0,3}(?:[-+][0-9A-Za-z.]{1,20})?[+*]?"
338367_P2_REQ_OP = r"(?:>=|<=|==|!=|~=|\^|>|<|=|~)"
339368_P2_REQ_VER_SUFFIX = r"(?:\s*" + _P2_REQ_OP + r"\s*" + _P2_REQ_VER + r"|\s+" + _P2_REQ_VER + r")"
@@ -353,7 +382,7 @@ def _compile_prompt_patterns(
353382)
354383_P2_PATH_VALUE_RE = re .compile (
355384 r"\A(?=[\w\-./#:?]{1,80}\Z)(?:https?://[\w\-.~]+(?::\d+)?/)?"
356- r"(?:[\w\-]+ /){0,3}[\w\-]+ \.[\w]{1,10}(?:[#?]\S*)?\Z"
385+ r"(?:" + _P2_NAME_TOKEN + r" /){0,3}" + _P2_NAME_TOKEN + r" \.[\w]{1,10}(?:[#?]\S*)?\Z"
357386)
358387_P2_SINGLE_TOKEN_RE = re .compile (r"\A[A-Za-z0-9.]{1,24}\Z" )
359388_P2_COPYRIGHT_VALUE_RE = re .compile (
@@ -400,16 +429,22 @@ def _p2_comment_inner(matched_text: str) -> str:
400429
401430
402431def _p2_has_danger_signal (inner : str ) -> bool :
403- """Return True when a comment body carries override or exfiltration intent."""
404- for pattern_source , _confidence in P1_PATTERNS :
405- if re .search (pattern_source , inner , re .IGNORECASE ):
432+ """Return True when a comment body carries override or exfiltration intent.
433+
434+ Both the raw body and a de-obfuscated copy are scanned: the copy
435+ catches camelCase/underscore-joined intent, the raw body keeps
436+ digit-bearing patterns exact. Either firing refuses the exemption.
437+ """
438+ for text in {inner , _p2_trigger_scan_text (inner )}:
439+ for pattern_source , _confidence in P1_PATTERNS :
440+ if re .search (pattern_source , text , re .IGNORECASE ):
441+ return True
442+ if _P2_OVERRIDE_EXTRA .search (text ):
443+ return True
444+ if _P2_EXFIL_STANDALONE .search (text ):
445+ return True
446+ if _P2_EXFIL_KEYWORD .search (text ) and _P2_EXTERNAL_DEST .search (text ):
406447 return True
407- if _P2_OVERRIDE_EXTRA .search (inner ):
408- return True
409- if _P2_EXFIL_STANDALONE .search (inner ):
410- return True
411- if _P2_EXFIL_KEYWORD .search (inner ) and _P2_EXTERNAL_DEST .search (inner ):
412- return True
413448 return False
414449
415450
@@ -435,7 +470,7 @@ def _is_frontmatter_adjacent(content: str, match_start: int) -> bool:
435470
436471def _is_license_only_fragment (fragment : str ) -> bool :
437472 """Return True when the fragment fully matches an anchored license-line form."""
438- if _P2_TRIGGER_STANDALONE .search (fragment ) is not None :
473+ if _P2_TRIGGER_STANDALONE .search (_p2_trigger_scan_text ( fragment ) ) is not None :
439474 return False
440475 return any (pattern .match (fragment ) is not None for pattern in _P2_LICENSE_LINE_RES )
441476
@@ -456,7 +491,7 @@ def _is_allowlisted_metadata_fragment(fragment: str) -> bool:
456491 value = match .group (2 ).strip ()
457492 if len (value ) > 1 and value .endswith ("." ):
458493 value = value [:- 1 ]
459- if _P2_TRIGGER_STANDALONE .search (value ) is not None :
494+ if _P2_TRIGGER_STANDALONE .search (_p2_trigger_scan_text ( value ) ) is not None :
460495 return False
461496 if key in _P2_FREE_TEXT_KEYS :
462497 return _P2_SINGLE_TOKEN_RE .match (value ) is not None
@@ -479,11 +514,21 @@ def _is_benign_license_or_metadata_body(inner: str) -> bool:
479514 if not body :
480515 return False
481516 seen_keys : set [str ] = set ()
517+ copyright_lines = 0
518+ spdx_lines = 0
482519 for fragment in re .split (r"[.!?]+\s+|\n|;" , body ):
483520 fragment = fragment .strip ()
484521 if not fragment :
485522 continue
486523 if _is_license_only_fragment (fragment ):
524+ if _P2_LICENSE_LINE_RES [_P2_COPYRIGHT_LINE_INDEX ].match (fragment ) is not None :
525+ copyright_lines += 1
526+ if copyright_lines > 2 :
527+ return False
528+ elif _P2_LICENSE_LINE_RES [_P2_SPDX_LINE_INDEX ].match (fragment ) is not None :
529+ spdx_lines += 1
530+ if spdx_lines > 1 :
531+ return False
487532 continue
488533 key_match = _P2_METADATA_LINE .match (fragment )
489534 if key_match is not None :
0 commit comments