@@ -244,19 +244,28 @@ def _compile_prompt_patterns(
244244# Shared grammar cores: the year-range + holder cap and the SPDX
245245# expression each appear in both a license-line form and a metadata
246246# value form. One core per shape so the next cap change edits one place.
247- # Name-like tokens carry letters, digits, and header punctuation only
248- # (no colon, slash, or tilde) with at most one inner hyphen, so a
249- # hyphen-, colon-, or slash-joined sentence never counts as tokens.
250- _P2_NAME_TOKEN = r"[A-Za-z0-9&.,'()+]+(?:-[A-Za-z0-9&.,'()+]+)?"
247+ # Name-like tokens carry letters, digits, underscore, and header
248+ # punctuation only (no colon, slash, tilde, or inner dot — the path
249+ # grammar needs the dot for the file extension) with at most one
250+ # inner separator ([-,_]) and an optional trailing dot or comma
251+ # (Corp., Acme,), so a dot-, comma-, hyphen-, colon-, or
252+ # slash-joined sentence never counts as tokens.
253+ _P2_NAME_TOKEN = r"[A-Za-z0-9&'()+_]+(?:[-,_][A-Za-z0-9&'()+_]+)?[.,]?"
251254_P2_COPYRIGHT_CORE = (
252255 r"(?=.{1,60}\Z)(?:\(c\)\s+|©\s+)?\d{1,4}(?:\s*-\s*\d{1,4})?"
253256 r"\s+(?:" + _P2_NAME_TOKEN + r"\s+){0,5}" + _P2_NAME_TOKEN + r"\.?"
254257)
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.
258+ # SPDX ids allow at most two hyphens, plus a third only in a
259+ # digit-bearing id (versioned compounds such as GPL-3.0-or-later and
260+ # CC-BY-SA-4.0; all-alpha 3-hyphen runs stay refused). Dots sit only
261+ # between digits, so dot-joined prose never fits. Length is scoped to
262+ # the id run so chains and trailing periods never shrink it; chains
263+ # cap at three ids.
257264_P2_SPDX_ID = (
258265 r"(?=[A-Za-z0-9.+\-]{1,24}(?![A-Za-z0-9.+\-]))"
259- r"[A-Za-z0-9.+]+(?:-[A-Za-z0-9.+]+){0,2}"
266+ r"(?:[A-Za-z0-9+]+(?:\.[0-9]+)?(?:-[A-Za-z0-9+]+(?:\.[0-9]+)?){0,2}"
267+ r"|(?=[A-Za-z0-9.+\-]*[0-9])[A-Za-z0-9+]+(?:\.[0-9]+)?"
268+ r"(?:-[A-Za-z0-9+]+(?:\.[0-9]+)?){0,3})"
260269)
261270_P2_SPDX_ATOM = _P2_SPDX_ID + r"(?:\s+(?:OR|AND|WITH)\s+" + _P2_SPDX_ID + r"){0,2}"
262271_P2_LICENSE_LINE_RES = (
@@ -270,17 +279,15 @@ def _compile_prompt_patterns(
270279 re .IGNORECASE ,
271280 ),
272281 re .compile (
273- r"\ALicensed under the\s+(?:"
274- + _P2_NAME_TOKEN
275- + r"\s+){0,2}"
276- + _P2_NAME_TOKEN
277- + r"\s+License"
282+ r"\ALicensed under the\s+(?=.{1,40}\s+License)"
283+ r"(?:" + _P2_NAME_TOKEN + r"\s+){0,2}" + _P2_NAME_TOKEN + r"\s+License"
278284 r"(?:, Version \d+\.\d+)?\.?\Z" ,
279285 re .IGNORECASE ,
280286 ),
281287)
282288_P2_COPYRIGHT_LINE_INDEX = 0
283289_P2_SPDX_LINE_INDEX = 2
290+ _P2_LICENSED_LINE_INDEX = 3
284291_P2_OVERRIDE_EXTRA = re .compile (
285292 r"system\s+prompt|respond\s+as|override\s+instructions?|you\s+must" ,
286293 re .IGNORECASE ,
@@ -294,23 +301,27 @@ def _compile_prompt_patterns(
294301 re .IGNORECASE ,
295302)
296303_P2_EXFIL_STANDALONE = re .compile (r"\bexfiltrat\w*\b" , re .IGNORECASE )
297- # Standalone instruction-ish words refuse the benign exemption when they
298- # appear in a value or license fragment. Keys are never scanned (system
299- # requirements, get started). Load-bearing for Title-Case prose, which
300- # no name grammar can stop. Fail-closed FP shapes: send feedback, blog
301- # post, operating system in values.
302- _P2_TRIGGER_STANDALONE = re .compile (
303- r"\b(system|instructions?|ignore|post|get|send|transmit)\b" , re .IGNORECASE
304+ # Instruction-ish words refuse the benign exemption when they appear
305+ # anywhere in a value or license fragment, even inside a longer word —
306+ # that is how main's P2 regex matches. Keys are never scanned (system
307+ # requirements, get started). "get" is deliberately excluded: as a
308+ # substring it fires on ordinary prose (getting_started, target,
309+ # forget), while GET-plus-URL exfiltration still trips the danger
310+ # scans. This costs one plain-prose shape (PostgreSQL carries "post"):
311+ # it reports P2, as it does on main, as does Systems ("system").
312+ # Applied to values and license fragments only, never keys.
313+ _P2_TRIGGER_SUBSTRING = re .compile (
314+ r"(system|instructions?|ignore|post|send|transmit)" , re .IGNORECASE
304315)
305316
306317
307318def _p2_trigger_scan_text (text : str ) -> str :
308- """Return a de-obfuscated copy of ``text`` for trigger/ danger scans.
319+ """Return a de-obfuscated copy of ``text`` for the danger scans.
309320
310321 camelCase joints, underscores, and digits become spaces, so
311322 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 .
323+ Only the danger scans use the copy; the exemption trigger check
324+ is a raw substring, which needs no de-obfuscation .
314325 """
315326 text = re .sub (r"(?<=[a-z0-9])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])" , " " , text )
316327 text = re .sub (r"[0-9_]+" , " " , text )
@@ -348,20 +359,24 @@ def _p2_trigger_scan_text(text: str) -> str:
348359# are 1-4 capitalized tokens or an email, requires entries each carry
349360# a version unless the value is one bare name (operators may follow
350361# the name directly: "python>=3.10"), free-text keys take one short
351- # hyphen-free word, and get-started paths need a dotted final segment
352- # so slash-joined prose no longer fits. A standalone trigger word
353- # (system, instruction(s), ignore, post, get, send, transmit) in any
354- # value refuses exemption; keys are excluded since "system
355- # requirements" and "get started" are keys. Known residuals: a bare
356- # triggerless package name ("requires: numpy") parses as a package;
357- # short triggerless SPDX OR-chains fit the atom cap.
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+ # token with at most two inner dots, and get-started paths need a
363+ # dotted final segment so slash-joined prose no longer fits. A
364+ # trigger word anywhere inside a value (system, instruction(s),
365+ # ignore, post, send, transmit — never "get", see above) refuses
366+ # exemption; keys are excluded since "system requirements" and "get
367+ # started" are keys. Known residuals: a bare triggerless package name
368+ # ("requires: numpy") parses as a package; short triggerless SPDX
369+ # OR-chains fit the atom cap; a trigger broken inside itself
370+ # ("ig_nore") carries no contiguous trigger run, so neither this
371+ # check nor main's regex sees it.
372+ # Requirement names take a package-name shape: optional @scope/
373+ # (hyphens allowed in the scope), at most 40 characters, at most two
374+ # -/_/. separators — a separator-joined sentence never counts as one
375+ # bare name. Length is scoped to the name run so comma-separated
376+ # lists never shrink it.
362377_P2_REQ_NAME = (
363378 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}"
379+ r"(?:@[A-Za-z0-9_.\- ]+/)?[A-Za-z0-9 ]+(?:[-_./ ][A-Za-z0-9 ]+){0,2}"
365380)
366381_P2_REQ_VER = r"v?\d+(?:\.\d+){0,3}(?:[-+][0-9A-Za-z.]{1,20})?[+*]?"
367382_P2_REQ_OP = r"(?:>=|<=|==|!=|~=|\^|>|<|=|~)"
@@ -384,7 +399,7 @@ def _p2_trigger_scan_text(text: str) -> str:
384399 r"\A(?=[\w\-./#:?]{1,80}\Z)(?:https?://[\w\-.~]+(?::\d+)?/)?"
385400 r"(?:" + _P2_NAME_TOKEN + r"/){0,3}" + _P2_NAME_TOKEN + r"\.[\w]{1,10}(?:[#?]\S*)?\Z"
386401)
387- _P2_SINGLE_TOKEN_RE = re .compile (r"\A[A-Za-z0-9.]{1,24}\Z" )
402+ _P2_SINGLE_TOKEN_RE = re .compile (r"\A(?= [A-Za-z0-9.]{1,24}\Z)[A-Za-z0-9]+(?:\.[A-Za-z0-9]+){0,2 }\Z" )
388403_P2_COPYRIGHT_VALUE_RE = re .compile (
389404 r"\A" + _P2_COPYRIGHT_CORE + r"\Z" ,
390405 re .IGNORECASE ,
@@ -470,7 +485,7 @@ def _is_frontmatter_adjacent(content: str, match_start: int) -> bool:
470485
471486def _is_license_only_fragment (fragment : str ) -> bool :
472487 """Return True when the fragment fully matches an anchored license-line form."""
473- if _P2_TRIGGER_STANDALONE .search (_p2_trigger_scan_text ( fragment ) ) is not None :
488+ if _P2_TRIGGER_SUBSTRING .search (fragment ) is not None :
474489 return False
475490 return any (pattern .match (fragment ) is not None for pattern in _P2_LICENSE_LINE_RES )
476491
@@ -491,7 +506,7 @@ def _is_allowlisted_metadata_fragment(fragment: str) -> bool:
491506 value = match .group (2 ).strip ()
492507 if len (value ) > 1 and value .endswith ("." ):
493508 value = value [:- 1 ]
494- if _P2_TRIGGER_STANDALONE .search (_p2_trigger_scan_text ( value ) ) is not None :
509+ if _P2_TRIGGER_SUBSTRING .search (value ) is not None :
495510 return False
496511 if key in _P2_FREE_TEXT_KEYS :
497512 return _P2_SINGLE_TOKEN_RE .match (value ) is not None
@@ -516,6 +531,7 @@ def _is_benign_license_or_metadata_body(inner: str) -> bool:
516531 seen_keys : set [str ] = set ()
517532 copyright_lines = 0
518533 spdx_lines = 0
534+ licensed_lines = 0
519535 for fragment in re .split (r"[.!?]+\s+|\n|;" , body ):
520536 fragment = fragment .strip ()
521537 if not fragment :
@@ -529,6 +545,10 @@ def _is_benign_license_or_metadata_body(inner: str) -> bool:
529545 spdx_lines += 1
530546 if spdx_lines > 1 :
531547 return False
548+ elif _P2_LICENSE_LINE_RES [_P2_LICENSED_LINE_INDEX ].match (fragment ) is not None :
549+ licensed_lines += 1
550+ if licensed_lines > 1 :
551+ return False
532552 continue
533553 key_match = _P2_METADATA_LINE .match (fragment )
534554 if key_match is not None :
@@ -537,6 +557,10 @@ def _is_benign_license_or_metadata_body(inner: str) -> bool:
537557 if key in seen_keys :
538558 return False
539559 seen_keys .add (key )
560+ if key == "copyright" :
561+ copyright_lines += 1
562+ if copyright_lines > 2 :
563+ return False
540564 if _is_allowlisted_metadata_fragment (fragment ):
541565 continue
542566 return False
0 commit comments