# anu.critical7_rules.v1
# task-2611 Track B (critical7_and_codex_high_classifier) — 회장 verbatim 2026-05-19.
#
# Critical7 = 보안 · credential · permission · forbidden path · scope expansion
#             · merge-write · OWNER PAT.
# 어느 하나라도 매치 = CHAIR_HOLD (회장 보고). 그 외 non-Critical = AUTO_REMEDIATION_HOLD.
#
# §5 금지: Critical7 분류 기준 임의 약화(security/credential/scope 누락) 금지.
#   - 7 family 는 모두 필수. 삭제/비활성 불가.
#   - match 는 OR(부분일치·정규식·카테고리 exact) — false-negative 회피 우선.
#   - 신규 위협어 추가는 허용(강화). 기존 term 삭제는 약화 = 금지.

schema: anu.critical7_rules.v1
version: "1.0.0"
authority: >-
  회장 결정 2026-05-19 — batch-task-2604-multitrack ACCEPT 후 시스템화.
  Critical7 = 보안·credential·permission·forbidden-path·scope-expansion·
  merge-write·OWNER PAT. 분류 기준 임의 약화 금지.
preflight_ref: memory/events/task-2610-batch-hold-system-preflight-decision_260519.json

# severity_floor: codex 가 이 severity 이상으로 보고한 HIGH/HOLD 만 Critical7 후보.
severity_in_scope: [HIGH, CRITICAL]

rules:
  - id: C7_SECURITY
    family: security
    title: 보안 취약점 / 안전하지 않은 sink / injection / secret 노출
    severity_floor: HIGH
    match:
      categories: [security, vuln, vulnerability, cwe, owasp, sast]
      # 짧고 모호한 토큰(rce/xss/ssrf/sqli/xxe/csrf)은 substring false-positive
      # ('fo*rce*' → rce) 방지를 위해 keyword 가 아닌 word-boundary regex 로만
      # 매치. Critical7 coverage 는 동일(약화 아님) — 정밀도만 강화.
      keywords:
        - security vulnerab
        - vulnerability
        - sql injection
        - command injection
        - cross-site scripting
        - remote code execution
        - deserializ
        - path traversal
        - directory traversal
        - unsafe eval
        - pickle.load
        - yaml.load
        - shell=true
        - subprocess injection
        - sensitive data exposure
        - hardcoded secret
        - secret exposure
        - prototype pollution
        - insecure deserial
      regex:
        - '(?i)\b(rce|ssrf|sqli|xss|xxe|csrf)\b'
        - '(?i)\binjection\b'
        - '(?i)cwe-\d+'
        - '(?i)owasp[ _-]?(a\d{1,2}|top[ _-]?10)'

  # task-2611+2 AUTO_REMEDIATION (SAFE·non-Critical): C7_CREDENTIAL 광의
  # 'access token' substring 이 owner-PAT 표기('owner personal access
  # token'/'owner access token')를 idx 선점하여 owner_pat 대신 credential
  # 로 라벨되던 LABEL mismatch 교정. owner-scoped(owner 접두 필수) 신규
  # rule 을 C7_CREDENTIAL 앞에 평가 — owner_pat family 가 credential 보다
  # 먼저 평가됨. 신규 term 추가만(§5 line 11 강화 허용), 기존 term 삭제 0,
  # C7_CREDENTIAL/기존 C7_OWNER_PAT 무변경. owner 접두 없는 bare
  # 'personal access token' 은 owner-scope 미충족 → 매치 안 함 → 그대로
  # C7_CREDENTIAL=credential 로 fall-through (selftest f-pat-words 계약·
  # REG-2/REG-6 보존). 7 family 불변(family=owner_pat 재사용·weakening
  # guard PASS)·coverage 무약화(전 케이스 CHAIR_HOLD·is_critical7 불변)·
  # escalation route 불변(CHAIR_HOLD). Track B fix-and-regression
  # -candidates(FIX-B owner-scoped regex 제안) 범위 한정.
  - id: C7_OWNER_PAT_PRIORITY
    family: owner_pat
    title: OWNER PAT 우선 (owner-scoped access token — C7_CREDENTIAL 'access token' 광의 선점 교정)
    severity_floor: HIGH
    match:
      categories: [owner_pat, privileged_token]
      keywords:
        - owner personal access token
        - owner access token
      regex:
        - '(?i)\bowner\b[\w \-]{0,20}\baccess[ _-]?tokens?\b'

  - id: C7_CREDENTIAL
    family: credential
    title: credential / token / key / password / PAT 노출·쓰기
    severity_floor: HIGH
    match:
      categories: [credential, secret, token]
      keywords:
        - credential
        - password
        - passwd
        - api key
        - apikey
        - secret key
        - access token
        - refresh token
        - private key
        - ssh key
        - aws_secret
        - bearer token
        - .env secret
        - token leak
        - key leak
        - credential write
        - plaintext password
      regex:
        - '(?i)\b(ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{8,}'
        - '(?i)\bAKIA[0-9A-Z]{8,}'
        - '(?i)(secret|token|password|api[_-]?key)\s*[=:]\s*[^\s"'']{6,}'

  - id: C7_PERMISSION
    family: permission
    title: permission / 권한 상승 / chmod 777 / sudo / ACL 변경
    severity_floor: HIGH
    match:
      categories: [permission, privilege, authz]
      keywords:
        - privilege escalation
        - permission change
        - chmod 777
        - chmod -r 777
        - chmod a+rwx
        - setuid
        - setgid
        - sudo
        - run as root
        - acl change
        - world-writable
        - 0777
        - umask 000
        - grant all privileges
        - authorization bypass
        - authz bypass
      regex:
        - '(?i)chmod\s+(-r\s+)?0?777'
        - '(?i)\bsudo\s+-'

  - id: C7_FORBIDDEN_PATH
    family: forbidden_path
    title: forbidden path write (allowlist 외 · frozen anchor · shared ledger · .git)
    severity_floor: HIGH
    match:
      categories: [forbidden_path, out_of_allowlist, path_violation]
      keywords:
        - forbidden path
        - outside allowlist
        - out of allowlist
        - not in expected_files
        - allowlist violation
        - frozen anchor
        - byte-0 violation
        - byte0 violation
        - shared ledger write
        - protected file
        - write outside scope
        - .git/
        - .git write
        - wrote to .git
      regex:
        - '(?i)(write|modify|overwrite|touch)[^\n]{0,40}\.git(/|\b)'
        - '(?i)expected_files\s+(allowlist\s+)?(violation|breach|exceeded)'

  - id: C7_SCOPE_EXPANSION
    family: scope_expansion
    title: scope expansion (작업 범위 외 변경 / unsanctioned cross-track write)
    severity_floor: HIGH
    match:
      categories: [scope, scope_expansion, out_of_scope]
      keywords:
        - scope expansion
        - scope creep
        - out-of-scope
        - out of scope
        - unsanctioned write
        - cross-track write
        - unsanctioned cross-track
        - unrelated file changed
        - modified files not in task
        - touched another track
        - tampered existing artifact
        - existing artifact modified
      regex:
        - '(?i)scope[ _-]?(expansion|creep|breach)'
        - '(?i)(out[ _-]?of[ _-]?scope)\s+(write|change|modif)'

  - id: C7_MERGE_WRITE
    family: merge_write
    title: merge / PR / branch / main write (git merge·push·PR open/merge)
    severity_floor: HIGH
    match:
      categories: [merge, pr, branch, vcs_write]
      keywords:
        - merge write
        - git merge
        - merged to main
        - push to main
        - git push
        - force push
        - pr open
        - pull request open
        - pr merge
        - merged pr
        - branch create
        - branch delete
        - rebase onto main
        - main branch write
        - protected branch
      regex:
        - '(?i)git\s+(push|merge|rebase)\b'
        - '(?i)(merge|push)[^\n]{0,30}\b(main|master|origin)\b'

  - id: C7_OWNER_PAT
    family: owner_pat
    title: OWNER PAT 사용 (owner personal access token / privileged GitHub token)
    severity_floor: HIGH
    match:
      categories: [owner_pat, privileged_token]
      keywords:
        - owner pat
        - owner personal access token
        - owner token
        - privileged pat
        - admin pat
        - org owner token
        - elevated github token
        - used owner credentials
      regex:
        - '(?i)owner[ _-]?(pat|token|personal[ _-]?access)'
        # bare 'PAT' 약어 + 'fine-grained PAT' + 'personal access token'
        # word-boundary detection. 갭: bare 'PAT'/'fine-grained PAT'/
        # 'personal access token' 이 owner-접두 없으면 C7_CREDENTIAL·
        # C7_OWNER_PAT 둘 다 미스 → AUTO_REMEDIATION_HOLD 강등(회장 §5
        # 보호 credential/OWNER-PAT 누락). 신규 term 추가 = 강화(§5 허용),
        # 기존 term 삭제 0, rule 순서/family 우선순위 불변(C7_CREDENTIAL 의
        # 'access token' 부분일치는 그대로 — owner-PAT 표기 family 안정).
        - '(?i)\bpat\b'
        - '(?i)\bfine[ _-]?grained\s+pat\b'
        - '(?i)\bpersonal[ _-]?access[ _-]?tokens?\b'

# task-2620 §2.3 — negation/context-aware preprocessor metadata.
# Critical7 분류기는 본 metadata 와 무관하게도 동작하지만(코드 단일 진실원
# = anu_v3/critical7_classifier.py:_TIGHT_NEGATION_TOKENS/_STRUCTURAL_REFUTATION
# _MARKERS), 외부 audit 도구가 ruleset 의 context-flag 동작을 검증/문서화
# 할 수 있도록 metadata 만 명기한다. 7 family/우선순위/약화가드 byte-0.
# context_flag 는 SAFE 방향만(detection coverage 감소 0 · escalation route
# 변경 0). category 매치는 negation 으로 회피 불가(구조화된 명시 신호).
context_flag:
  schema: anu.critical7_rules.context_flag.v1
  applies_to: [keywords, regex]   # category 는 negation 불가
  safe_direction_only: true
  detection_coverage_loss_zero: true
  escalation_route_unchanged: true
  recognized_patterns:
    - "0 동반 (예: 'credential write 0', '= 0', 'count: 0')"
    - "no/zero 동반 (예: 'no credential write', 'zero injection')"
    - "없음/금지 동반 (예: 'SQL injection 없음', 'forbidden path 금지')"
    - "false / false positive / false-positive (sentence scope)"
    - "refute / refuted / REFUTATION (sentence scope)"
  rationale: |
    회장 2026-05-20 §2.3 — task-2615 C7R3 'credential write 0' REFUTATION
    매칭 + task-2611+2 SAFE precedence + doc/test 키워드 등장(설명/부정문/
    REFUTATION) 의 false-positive 차단. 약화가드 불변, 진성 Critical7 8/8
    무회귀.

# Critical7 미매치 + 아래 계열 = non-Critical → AUTO_REMEDIATION_HOLD 후보.
non_critical_families:
  description: >-
    Critical7 어느 rule 에도 매치되지 않은 HIGH/HOLD. 회장 §3:
    non-Critical HOLD 는 AUTO_REMEDIATION_HOLD 로 자동 수렴.
  families: [coverage, test_failure, claim_mismatch, lint, doc, style, flaky]

# shared invariant 파손은 Critical7 과 동급 — 전체 CHAIR_HOLD (회장 §6).
invariant_break_is_chair_hold: true
invariant_break_signals:
  - byte-0 violation
  - frozen anchor modified
  - executor self-callback
  - executor self-collector
  - executor self-adjudication
  - independent anu authoritative weakened
  - fixed-time progress trigger
  - dead-man progress trigger
