@@ -233,21 +233,27 @@ def _compile_prompt_patterns(
233233# Each license fragment must FULLY match one anchored form below. The
234234# substring check these replace let space-joined payloads through
235235# ("Copyright ... <arbitrary instruction>" with no separators). The
236- # holder is capped at six tokens ("NVIDIA CORPORATION & AFFILIATES" is
237- # three); years are \d{1,4} because digit-masking collapses "2026" to
238- # "0". One trailing period is allowed ("All rights reserved." is the
239- # residual: a very short instruction fits the holder cap
240- # ("Copyright 2026 Acme delete everything") — accepted per the
241- # anchored-forms spec; the surrounding gates (danger signal first,
242- # top-of-file, complete comment, 300 chars) still apply.
236+ # holder is capped at six name-like tokens and about 60 characters
237+ # ("NVIDIA CORPORATION & AFFILIATES" is three); fragments carrying a
238+ # standalone trigger word never exempt. One trailing period is
239+ # allowed ("All rights reserved."). Residual: a very short
240+ # triggerless instruction fits the holder cap ("Copyright 2026 Acme
241+ # delete everything") — accepted per the anchored-forms spec; the
242+ # surrounding gates (danger signal first, top-of-file, complete
243+ # comment, 300 chars) still apply.
243244# Shared grammar cores: the year-range + holder cap and the SPDX
244245# expression each appear in both a license-line form and a metadata
245246# 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&.,'()+]+)?"
246251_P2_COPYRIGHT_CORE = (
247- r"(?:\(c\)\s+|©\s+)?\d{1,4}(?:\s*-\s*\d{1,4})?"
248- r"\s+(?:\S+ \s+){0,5}\S+ \.?"
252+ r"(?=.{1,60}\Z)(? :\(c\)\s+|©\s+)?\d{1,4}(?:\s*-\s*\d{1,4})?"
253+ r"\s+(?:" + _P2_NAME_TOKEN + r" \s+){0,5}" + _P2_NAME_TOKEN + r" \.?"
249254)
250- _P2_SPDX_ATOM = r"[A-Za-z0-9.+\-]+(?:\s+(?:OR|AND|WITH)\s+[A-Za-z0-9.+\-]+)*"
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")*"
251257_P2_LICENSE_LINE_RES = (
252258 re .compile (
253259 r"\ACopyright\s+" + _P2_COPYRIGHT_CORE + r"\Z" ,
@@ -259,7 +265,11 @@ def _compile_prompt_patterns(
259265 re .IGNORECASE ,
260266 ),
261267 re .compile (
262- r"\ALicensed under the\s+(?:\S+\s+){0,3}\S+\s+License"
268+ r"\ALicensed under the\s+(?:"
269+ + _P2_NAME_TOKEN
270+ + r"\s+){0,2}"
271+ + _P2_NAME_TOKEN
272+ + r"\s+License"
263273 r"(?:, Version \d+\.\d+)?\.?\Z" ,
264274 re .IGNORECASE ,
265275 ),
@@ -277,6 +287,14 @@ def _compile_prompt_patterns(
277287 re .IGNORECASE ,
278288)
279289_P2_EXFIL_STANDALONE = re .compile (r"\bexfiltrat\w*\b" , re .IGNORECASE )
290+ # Standalone instruction-ish words refuse the benign exemption when they
291+ # appear in a value or license fragment. Keys are never scanned (system
292+ # requirements, get started). Load-bearing for Title-Case prose, which
293+ # no name grammar can stop. Fail-closed FP shapes: send feedback, blog
294+ # post, operating system in values.
295+ _P2_TRIGGER_STANDALONE = re .compile (
296+ r"\b(system|instructions?|ignore|post|get|send|transmit)\b" , re .IGNORECASE
297+ )
280298# Metadata keys observed in benign skill headers plus obvious header keys.
281299# Matching is exact (case-insensitive): bare instruction words such as
282300# system, instructions, or ignore are never here.
@@ -302,32 +320,42 @@ def _compile_prompt_patterns(
302320 }
303321)
304322_P2_METADATA_LINE = re .compile (r"\A([A-Za-z][\w\- ]{0,40}):\s+(\S.*)\Z" )
305- _P2_NUMERIC_MASK = re .compile (r"\d+" )
306- # Per-key metadata value grammars. The single generic value shape they
307- # replace let any imperative instruction through once it contained a
308- # digit, dot, slash, @, or hyphen. Each key now fullmatches its own
309- # narrow form (years/dates survive digit-masking: "2026" becomes "0",
310- # still matched by \d classes). A single sentence-final period is
311- # stripped before validation ("contact: [email protected] ."), anything else 312- # fail-closed. Free-text keys (description/title/status/tags) accept a
313- # single token only. Known residual: a bare package name ("requires:
314- # send") parses as a package; single-word values carry no payload.
315- _P2_VERSION_RE = re .compile (r"\Av?\d[\w.+\-]*\Z" )
323+ # Per-key metadata value grammars. Token content is constrained as
324+ # well as token count: versions are semver-like (with a trailing + or *
325+ # for "3.10+"-style floors), SPDX atoms are bounded-length ids, names
326+ # are 1-4 capitalized tokens or an email, requires entries each carry
327+ # a version unless the value is one bare name (operators may follow
328+ # the name directly: "python>=3.10"), free-text keys take one short
329+ # hyphen-free word, and get-started paths need a dotted final segment
330+ # so slash-joined prose no longer fits. A standalone trigger word
331+ # (system, instruction(s), ignore, post, get, send, transmit) in any
332+ # value refuses exemption; keys are excluded since "system
333+ # requirements" and "get started" are keys. Known residuals: a bare
334+ # triggerless package name ("requires: numpy") parses as a package;
335+ # short triggerless SPDX OR-chains fit the atom cap.
336+ _P2_REQ_NAME = r"[A-Za-z0-9_.\-@/]+"
337+ _P2_REQ_VER = r"v?\d+(?:\.\d+){0,3}(?:[-+][0-9A-Za-z.]{1,20})?[+*]?"
338+ _P2_REQ_OP = r"(?:>=|<=|==|!=|~=|\^|>|<|=|~)"
339+ _P2_REQ_VER_SUFFIX = r"(?:\s*" + _P2_REQ_OP + r"\s*" + _P2_REQ_VER + r"|\s+" + _P2_REQ_VER + r")"
340+ _P2_VERSION_RE = re .compile (r"\A" + _P2_REQ_VER + r"\Z" )
316341_P2_DATE_RE = re .compile (
317342 r"\A(?:\d{4}-\d{2}-\d{2}|(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?"
318343 r"|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:t(?:ember)?)?"
319344 r"|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\s+\d{1,2},\s+\d{4})\Z" ,
320345 re .IGNORECASE ,
321346)
322- _P2_NAME_RE = re .compile (r"\A[A-Za-z] [A-Za-z .'\-] {0,39 }\Z" )
347+ _P2_NAME_RE = re .compile (r"\A[A-Z][A- Za-z']*(?:\s+ [A-Z][A- Za-z']*) {0,3 }\Z" )
323348_P2_EMAIL_RE = re .compile (r"\A[\w.+\-]+@[\w.\-]+\.[A-Za-z]{2,}\Z" )
324349_P2_SPDX_EXPR_RE = re .compile (r"\A" + _P2_SPDX_ATOM + r"\Z" )
325350_P2_REQUIREMENTS_RE = re .compile (
326- r"\A[A-Za-z0-9_.\-@/]+(?:\s+[<>=!~^*]*\d[\w.+\-*]*)?"
327- r"(?:\s*[,;]\s*[A-Za-z0-9_.\-@/]+(?:\s+[<>=!~^*]*\d[\w.+\-*]*)?)*\Z"
351+ r"\A" + _P2_REQ_NAME + _P2_REQ_VER_SUFFIX + r"?"
352+ r"(?:\s*[,;]\s*" + _P2_REQ_NAME + _P2_REQ_VER_SUFFIX + r")*\Z"
353+ )
354+ _P2_PATH_VALUE_RE = re .compile (
355+ r"\A(?=[\w\-./#:?]{1,80}\Z)(?:https?://[\w\-.~]+(?::\d+)?/)?"
356+ r"(?:[\w\-]+/){0,3}[\w\-]+\.[\w]{1,10}(?:[#?]\S*)?\Z"
328357)
329- _P2_PATH_VALUE_RE = re .compile (r"\A(?=\S*[/.#:])\S{1,120}\Z" )
330- _P2_SINGLE_TOKEN_RE = re .compile (r"\A\S{1,40}\Z" )
358+ _P2_SINGLE_TOKEN_RE = re .compile (r"\A[A-Za-z0-9.]{1,24}\Z" )
331359_P2_COPYRIGHT_VALUE_RE = re .compile (
332360 r"\A" + _P2_COPYRIGHT_CORE + r"\Z" ,
333361 re .IGNORECASE ,
@@ -407,6 +435,8 @@ def _is_frontmatter_adjacent(content: str, match_start: int) -> bool:
407435
408436def _is_license_only_fragment (fragment : str ) -> bool :
409437 """Return True when the fragment fully matches an anchored license-line form."""
438+ if _P2_TRIGGER_STANDALONE .search (fragment ) is not None :
439+ return False
410440 return any (pattern .match (fragment ) is not None for pattern in _P2_LICENSE_LINE_RES )
411441
412442
@@ -426,6 +456,8 @@ def _is_allowlisted_metadata_fragment(fragment: str) -> bool:
426456 value = match .group (2 ).strip ()
427457 if len (value ) > 1 and value .endswith ("." ):
428458 value = value [:- 1 ]
459+ if _P2_TRIGGER_STANDALONE .search (value ) is not None :
460+ return False
429461 if key in _P2_FREE_TEXT_KEYS :
430462 return _P2_SINGLE_TOKEN_RE .match (value ) is not None
431463 validators = _P2_METADATA_VALUE_RES .get (key )
@@ -437,21 +469,29 @@ def _is_allowlisted_metadata_fragment(fragment: str) -> bool:
437469def _is_benign_license_or_metadata_body (inner : str ) -> bool :
438470 """Return True only when every fragment is license- or metadata-shaped.
439471
440- Numbers are masked before sentence splitting so ``3.10`` does not
441- split into fragments. Only digits are masked (dots stay), so a
442- ``2.0.`` boundary still splits. Masking maps digits to ``0`` and
443- cannot create an allowlist hit.
472+ Fragments are split and validated on the raw text: digit-masking
473+ never affected split points (digits are not split characters), and
474+ validating masked text broke real dates. An allowlisted key may
475+ appear only once, so an instruction split across repeated keys
476+ still fires.
444477 """
445478 body = inner .strip ()
446479 if not body :
447480 return False
448- masked = _P2_NUMERIC_MASK . sub ( "0" , body )
449- for fragment in re .split (r"[.!?]+\s+|\n|;" , masked ):
481+ seen_keys : set [ str ] = set ( )
482+ for fragment in re .split (r"[.!?]+\s+|\n|;" , body ):
450483 fragment = fragment .strip ()
451484 if not fragment :
452485 continue
453486 if _is_license_only_fragment (fragment ):
454487 continue
488+ key_match = _P2_METADATA_LINE .match (fragment )
489+ if key_match is not None :
490+ key = key_match .group (1 ).lower ()
491+ if key in _P2_BENIGN_METADATA_KEYS :
492+ if key in seen_keys :
493+ return False
494+ seen_keys .add (key )
455495 if _is_allowlisted_metadata_fragment (fragment ):
456496 continue
457497 return False
0 commit comments