From 272ac062c73ef8d593f85052e06bfbdc71f071fd Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:18:28 +0000 Subject: [PATCH 1/4] fix(trust): fail closed on active Idris2 trust escapes --- scripts/check-idris-escape-hatches.sh | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 scripts/check-idris-escape-hatches.sh diff --git a/scripts/check-idris-escape-hatches.sh b/scripts/check-idris-escape-hatches.sh new file mode 100644 index 00000000..65f81714 --- /dev/null +++ b/scripts/check-idris-escape-hatches.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# Fail closed on active Idris2 trust escapes. This is a lexical gate, +# not a theorem checker; a clean run does not discharge any proof. +set -euo pipefail + +source_root=${1:-src} +if [[ ! -d "$source_root" ]]; then + printf 'ERROR: Idris source root not found: %s\n' "$source_root" >&2 + exit 2 +fi + +mapfile -d '' -t sources < <(find "$source_root" -type f -name '*.idr' -print0 | LC_ALL=C sort -z) +if (( ${#sources[@]} == 0 )); then + printf 'ERROR: no Idris2 sources found under %s\n' "$source_root" >&2 + exit 2 +fi + +# Track Idris block comments across lines, including nested comments. Remove +# comments and strings before scanning tokens to avoid counting documentation, +# inline comments or harmless quoted text as executable code. Any active token +# remains forbidden, with no permissive threshold or implicit waiver. +awk ' +function code_of(line, i, pair, ch, out, quoted, escaped) { + out = ""; quoted = 0; escaped = 0 + if (line ~ /^[[:space:]]*\|\|\|/) return "" + for (i = 1; i <= length(line); i++) { + ch = substr(line, i, 1) + pair = substr(line, i, 2) + if (depth > 0) { + if (pair == "{-") { depth++; i++; continue } + if (pair == "-}") { depth--; i++; continue } + continue + } + if (quoted) { + if (escaped) { escaped = 0; continue } + if (ch == "\\") { escaped = 1; continue } + if (ch == "\"") { quoted = 0; continue } + continue + } + if (pair == "--") break + if (pair == "{-") { depth++; i++; continue } + if (ch == "\"") { quoted = 1; continue } + out = out ch + } + return out +} +FNR == 1 { + if (NR > 1 && depth != 0) { + print "ERROR: unterminated Idris block comment in previous file" > "/dev/stderr" + errors++ + } + depth = 0 + files++ +} +{ + executable = code_of($0) + if (executable ~ /(^|[^[:alnum:]_])(really_believe_me|believe_me|idris_crash)([^[:alnum:]_]|$)/) { + printf "ERROR: active Idris trust escape at %s:%d: %s\n", FILENAME, FNR, executable > "/dev/stderr" + errors++ + } +} +END { + if (depth != 0) { + print "ERROR: unterminated Idris block comment in final file" > "/dev/stderr" + errors++ + } + if (errors) { + printf "FAIL: %d active trust escape(s) or lexical error(s)\n", errors > "/dev/stderr" + exit 1 + } + print "PASS: no active believe_me, really_believe_me, or idris_crash tokens in " files " Idris2 files" +} +' "${sources[@]}" From 41a8b83a59b6e7b694c64d32c344549ad0ab3ef2 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:18:38 +0000 Subject: [PATCH 2/4] test(trust): plant forbidden escapes and reject non-failing gate --- scripts/test-idris-escape-gate.sh | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 scripts/test-idris-escape-gate.sh diff --git a/scripts/test-idris-escape-gate.sh b/scripts/test-idris-escape-gate.sh new file mode 100644 index 00000000..3b3a0d2f --- /dev/null +++ b/scripts/test-idris-escape-gate.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# Deliberately planted false-negative controls for the Idris escape gate. +set -euo pipefail +here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +scanner="$here/check-idris-escape-hatches.sh" +work=$(mktemp -d -t proven-escape-controls.XXXXXXXX) +trap 'rm -rf -- "$work"' EXIT +mkdir -p "$work/src" +source_file="$work/src/Control.idr" + +expect_pass() { + if ! bash "$scanner" "$work/src" > "$work/stdout" 2> "$work/stderr"; then + printf 'FAIL: expected scanner success for %s\n' "$1" >&2 + cat "$work/stderr" >&2 + exit 1 + fi +} +expect_fail() { + if bash "$scanner" "$work/src" > "$work/stdout" 2> "$work/stderr"; then + printf 'FAIL: scanner accepted planted %s\n' "$1" >&2 + exit 1 + fi + grep -q 'active Idris trust escape' "$work/stderr" || { + printf 'FAIL: wrong failure mechanism for %s\n' "$1" >&2 + cat "$work/stderr" >&2 + exit 1 + } +} + +cat > "$source_file" <<'IDR' +module Control +||| Documentation mentioning believe_me, really_believe_me and idris_crash +-- Ordinary comment: believe_me +{- block comment: believe_me + {- nested: idris_crash -} + really_believe_me -} +x = "believe_me and idris_crash" +y = x -- really_believe_me +IDR +expect_pass 'comments and string literals' + +for token in believe_me really_believe_me idris_crash; do + printf 'module Control\nvalue = %s 42\n' "$token" > "$source_file" + expect_fail "$token" +done + +cat > "$source_file" <<'IDR' +module Control +value = pure 42 -- a mention: believe_me +{- comment open + really_believe_me + comment close -} +value2 = "not an idris_crash call" +IDR +expect_pass 'inline comments and multiline block comments' + +rm -f "$source_file" +if bash "$scanner" "$work/src" >/dev/null 2>&1; then + echo 'FAIL: scanner accepts a missing source census' >&2 + exit 1 +fi +printf 'PASS: Idris escape gate negative and positive controls\n' From 1c2f18eb30bcc4bf224e30c0705cb7abe3a1130d Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:18:53 +0000 Subject: [PATCH 3/4] ci(trust): execute fail-closed escape gate before other policy checks --- .github/workflows/trustfile.yml | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/.github/workflows/trustfile.yml b/.github/workflows/trustfile.yml index a2b2e14c..e0984e1d 100644 --- a/.github/workflows/trustfile.yml +++ b/.github/workflows/trustfile.yml @@ -12,6 +12,8 @@ on: - 'ffi/**' - 'bindings/**' - '.github/workflows/trustfile.yml' + - 'scripts/check-idris-escape-hatches.sh' + - 'scripts/test-idris-escape-gate.sh' pull_request: branches: [main, master] workflow_dispatch: @@ -86,6 +88,11 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 + - name: Reject Idris2 trust escapes (with planted controls) + run: | + set -euo pipefail + bash scripts/test-idris-escape-gate.sh + bash scripts/check-idris-escape-hatches.sh src - name: Check SPDX license compliance run: | # Estate licence policy: code is MPL-2.0, docs are CC-BY-SA-4.0. @@ -99,16 +106,6 @@ jobs: exit 1 fi echo "License compliance: PASS" - - name: Check believe_me audit trail - run: | - # Count believe_me instances and verify they have justification comments - BELIEVE_ME_COUNT=$(grep -r "believe_me" src/ --include="*.idr" 2>/dev/null | wc -l) - UNJUSTIFIED=$(grep -r "believe_me" src/ --include="*.idr" -B1 2>/dev/null | \ - grep -c "believe_me" 2>/dev/null || true) - echo "believe_me instances: $BELIEVE_ME_COUNT" - if [ "$BELIEVE_ME_COUNT" -gt 300 ]; then - echo "WARNING: believe_me count ($BELIEVE_ME_COUNT) exceeds threshold (300)" - fi - name: Verify FFI bridge purity if: always() run: | From 3aaf61de736bd68f4b558a5d6a552c0ef95a6e65 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 11:34:06 +0000 Subject: [PATCH 4/4] fix(trust): make the Idris escape lexer stateful and symlink/literate aware Address review findings on the escape gate: character literals are consumed before strings, multiline strings and \{ interpolation keep state across lines, prim__believe_me and prim__crash are forbidden, symlinked and .lidr sources are scanned, and a dangling link is an error. Each hazard has a planted control. --- scripts/check-idris-escape-hatches.sh | 82 ++++++++++++++++++--------- scripts/test-idris-escape-gate.sh | 38 ++++++++++++- 2 files changed, 92 insertions(+), 28 deletions(-) diff --git a/scripts/check-idris-escape-hatches.sh b/scripts/check-idris-escape-hatches.sh index 65f81714..1ce56c3a 100644 --- a/scripts/check-idris-escape-hatches.sh +++ b/scripts/check-idris-escape-hatches.sh @@ -10,65 +10,93 @@ if [[ ! -d "$source_root" ]]; then exit 2 fi -mapfile -d '' -t sources < <(find "$source_root" -type f -name '*.idr' -print0 | LC_ALL=C sort -z) +# Follow symlinks (-L): a package-listed source may be a link to a tracked file +# outside the root. A dangling link is an error, never a silent skip. Literate +# Idris (.lidr) is included and scanned raw (every line is treated as code), which +# over-approximates and so fails closed. +mapfile -d '' -t dangling < <(find -L "$source_root" -type l -print0) +if (( ${#dangling[@]} > 0 )); then + printf 'ERROR: dangling symlink under %s: %s\n' "$source_root" "${dangling[0]}" >&2 + exit 2 +fi +mapfile -d '' -t sources < <(find -L "$source_root" -type f \( -name '*.idr' -o -name '*.lidr' \) -print0 | LC_ALL=C sort -z) if (( ${#sources[@]} == 0 )); then printf 'ERROR: no Idris2 sources found under %s\n' "$source_root" >&2 exit 2 fi -# Track Idris block comments across lines, including nested comments. Remove -# comments and strings before scanning tokens to avoid counting documentation, -# inline comments or harmless quoted text as executable code. Any active token -# remains forbidden, with no permissive threshold or implicit waiver. +# Lexer state persists across lines: nested block comments, single-line strings, +# triple-quoted multiline strings, and \{ ... } interpolation (code inside a +# string, with its own brace depth and nested strings). Character literals are +# consumed before strings so '"' cannot open one. Comments, doc comments and +# string text are dropped; every remaining token is scanned. No waiver. awk ' -function code_of(line, i, pair, ch, out, quoted, escaped) { - out = ""; quoted = 0; escaped = 0 - if (line ~ /^[[:space:]]*\|\|\|/) return "" - for (i = 1; i <= length(line); i++) { - ch = substr(line, i, 1) - pair = substr(line, i, 2) +function reset_file() { depth = 0; instr = 0; sp = 0 } +function code_of(line, lit, i, n, ch, pair, tri, out, prev, j) { + out = ""; n = length(line) + if (lit) return line + if (!instr && depth == 0 && line ~ /^[[:space:]]*\|\|\|/) return "" + for (i = 1; i <= n; i++) { + ch = substr(line, i, 1); pair = substr(line, i, 2); tri = substr(line, i, 3) if (depth > 0) { - if (pair == "{-") { depth++; i++; continue } - if (pair == "-}") { depth--; i++; continue } + if (pair == "{-") { depth++; i++ } else if (pair == "-}") { depth--; i++ } continue } - if (quoted) { - if (escaped) { escaped = 0; continue } - if (ch == "\\") { escaped = 1; continue } - if (ch == "\"") { quoted = 0; continue } + if (instr == 1 || instr == 2) { + if (ch == "\\") { + if (substr(line, i + 1, 1) == "{") { # interpolation: back to code + stk[++sp] = instr; brace[sp] = 1; instr = 0; i++ + } else i++ + continue + } + if (instr == 2 && tri == "\"\"\"") { instr = 0; i += 2; continue } + if (instr == 1 && ch == "\"") { instr = 0 } continue } if (pair == "--") break if (pair == "{-") { depth++; i++; continue } - if (ch == "\"") { quoted = 1; continue } + if (sp > 0 && ch == "{") { brace[sp]++ } + if (sp > 0 && ch == "}") { brace[sp]--; if (brace[sp] == 0) { instr = stk[sp]; sp--; continue } } + if (tri == "\"\"\"") { instr = 2; i += 2; continue } + if (ch == "\"") { instr = 1; continue } + if (ch == "\047") { # character literal, not f'"'"' + prev = (i > 1) ? substr(line, i - 1, 1) : " " + if (prev !~ /[[:alnum:]_\047]/) { + if (substr(line, i + 1, 1) == "\\") { + j = index(substr(line, i + 2), "\047") + if (j > 0) { i += j + 1; continue } + } else if (substr(line, i + 2, 1) == "\047") { i += 2; continue } + } + } out = out ch } + if (instr == 1 && sp == 0) instr = 0 # unterminated single-line string ends at EOL return out } FNR == 1 { - if (NR > 1 && depth != 0) { - print "ERROR: unterminated Idris block comment in previous file" > "/dev/stderr" + if (NR > 1 && (depth != 0 || instr != 0)) { + print "ERROR: unterminated Idris comment or string in previous file" > "/dev/stderr" errors++ } - depth = 0 - files++ + reset_file(); files++ + literate = (FILENAME ~ /\.lidr$/) } { - executable = code_of($0) - if (executable ~ /(^|[^[:alnum:]_])(really_believe_me|believe_me|idris_crash)([^[:alnum:]_]|$)/) { + executable = code_of($0, literate) + if (executable ~ /(^|[^[:alnum:]_])(really_believe_me|believe_me|idris_crash|prim__believe_me|prim__crash)([^[:alnum:]_]|$)/) { printf "ERROR: active Idris trust escape at %s:%d: %s\n", FILENAME, FNR, executable > "/dev/stderr" errors++ } } END { - if (depth != 0) { - print "ERROR: unterminated Idris block comment in final file" > "/dev/stderr" + if (depth != 0 || instr != 0) { + print "ERROR: unterminated Idris comment or string in final file" > "/dev/stderr" errors++ } if (errors) { printf "FAIL: %d active trust escape(s) or lexical error(s)\n", errors > "/dev/stderr" exit 1 } - print "PASS: no active believe_me, really_believe_me, or idris_crash tokens in " files " Idris2 files" + print "PASS: no active trust-escape tokens in " files " Idris2 files" } ' "${sources[@]}" diff --git a/scripts/test-idris-escape-gate.sh b/scripts/test-idris-escape-gate.sh index 3b3a0d2f..8c2a41f0 100644 --- a/scripts/test-idris-escape-gate.sh +++ b/scripts/test-idris-escape-gate.sh @@ -40,7 +40,7 @@ y = x -- really_believe_me IDR expect_pass 'comments and string literals' -for token in believe_me really_believe_me idris_crash; do +for token in believe_me really_believe_me idris_crash prim__believe_me prim__crash; do printf 'module Control\nvalue = %s 42\n' "$token" > "$source_file" expect_fail "$token" done @@ -55,6 +55,42 @@ value2 = "not an idris_crash call" IDR expect_pass 'inline comments and multiline block comments' + +# Controls for each lexer hazard: active escapes that a naive scanner hides. +plant_fail() { # name, body + printf 'module Control\n%s\n' "$2" > "$source_file" + expect_fail "$1" +} +plant_pass() { + printf 'module Control\n%s\n' "$2" > "$source_file" + expect_pass "$1" +} +plant_fail 'escape after a double-quote character literal' "f : Char -> Nat +f '\"' = believe_me 42" +plant_fail 'escape inside string interpolation' 'x = "forged: \{believe_me value}"' +plant_fail 'direct primitive with erased arguments' 'v = prim__believe_me _ _ value' +plant_fail 'escape after a multiline string closes' 's = """ +text +""" +v = believe_me 1' +plant_pass 'forbidden name inside a multiline string interior' 's = """ +believe_me idris_crash +"""' +plant_pass 'primed identifier is not a character literal' "g' = x'" +printf 'module Control\nv = believe_me 1\n' > "$work/Real.idr" +rm -f "$source_file" +mkdir -p "$work/outside" +mv "$work/Real.idr" "$work/outside/Real.idr" +ln -s ../outside/Real.idr "$work/src/Evil.idr" +expect_fail 'symlinked source outside the root' +rm -f "$work/src/Evil.idr" +ln -s ../outside/Missing.idr "$work/src/Dangling.idr" +if bash "$scanner" "$work/src" >/dev/null 2>&1; then echo 'FAIL: dangling symlink accepted' >&2; exit 1; fi +rm -f "$work/src/Dangling.idr" +printf 'Prose.\n\n> v = believe_me 1\n' > "$work/src/Lit.lidr" +expect_fail 'literate Idris source' +rm -f "$work/src/Lit.lidr" +printf 'module Control\n' > "$source_file" rm -f "$source_file" if bash "$scanner" "$work/src" >/dev/null 2>&1; then echo 'FAIL: scanner accepts a missing source census' >&2