From 2a22c75231d91c3e88b915c3f757fa22240d267a Mon Sep 17 00:00:00 2001 From: CLAUDE <355856132+HoTT-stuff@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:30:14 +0000 Subject: [PATCH 1/2] feat(audit): add package mode so a fixed include is audited, not forbidden audit_package and audit-package permit one literal include form and audit every file it reaches. audit_file and audit_source are unchanged. Co-Authored-By: Claude Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0136P7Zpa1S5bdGh4d9CQ9tt --- CHANGELOG.adoc | 8 ++ docs/plan/ULTRAPLAN.adoc | 20 ++++ scripts/apodeixis.jl | 43 +++++++- src/Apodeixis.jl | 187 ++++++++++++++++++++++++++++++- test/runtests.jl | 232 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 486 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 1e7d4419..d76e09b9 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -8,6 +8,14 @@ https://semver.org/spec/v2.0.0.html[Semantic Versioning]. === [Unreleased] +==== Added + +* Package mode: `audit_package(root_file)` and `audit-package ROOT.jl` permit + one form of `include` (a plain string literal, a statement at top level or + directly in a module body, a relative `.jl` path inside the root directory) + and audit every file it reaches. Other forms stay `APX_DYNAMIC_ESCAPE`. + Unreached `.jl` files are listed. `audit_file` still forbids `include`. + ==== Fixed * Source-policy audit (2026-11-15): the audit reported `POLICY CLEAN` for diff --git a/docs/plan/ULTRAPLAN.adoc b/docs/plan/ULTRAPLAN.adoc index 401fb9c4..d15e6b37 100644 --- a/docs/plan/ULTRAPLAN.adoc +++ b/docs/plan/ULTRAPLAN.adoc @@ -57,6 +57,26 @@ The seven family gates apply. The text below is the Apodeixis reading. | Release and estate | Registered in the General registry or documented as not registered. README, deed and changelog true. | A `POLICY CLEAN` result is described as a proof. |=== +=== First external target: CoprocessorRuntime.jl + +Source-policy audit, not a proof. Measured with the Julia 1.12.1 build of this +auditor on `hyperpolymath/CoprocessorRuntime.jl` at commit `a0e2c82` +(`a0e2c823cbc87905b9810641fd8bb55e4b7eeef6`). The clone was read only and no +code from it ran. + +* Before: `audit` on each of its 8 source files. 7 files were policy clean. The + package root `src/CoprocessorRuntime.jl` had 7 findings, all + `APX_DYNAMIC_ESCAPE: include is forbidden in the closed verified subset`. + No real package could pass, because a package must `include` its own files. +* After: `audit-package src/CoprocessorRuntime.jl` reaches 8 files, all policy + clean, with 0 package findings and 0 unreached files. Exit code 0. +* What changed: `audit_package` permits one form of `include` (one plain string + literal, as a statement at top level or directly in a module body, path + relative, ending in `.jl`, inside the root directory) and audits the included + file. `audit_file` and `audit_source` still forbid `include`. +* What this does not show: the package is not verified. The audit is a + deny-list over syntax. + == Sequence [cols="1,4,1,1,3",options="header"] diff --git a/scripts/apodeixis.jl b/scripts/apodeixis.jl index 0caa7879..f9103d9d 100755 --- a/scripts/apodeixis.jl +++ b/scripts/apodeixis.jl @@ -6,16 +6,56 @@ using Apodeixis function usage(io=stdout) println(io, "Usage: julia --project=. scripts/apodeixis.jl audit [--mutation-score PERCENT] FILE.jl") + println(io, " julia --project=. scripts/apodeixis.jl audit-package [--mutation-score PERCENT] ROOT.jl") println(io, " julia --project=. scripts/apodeixis.jl --help") println(io, "Audits source policy only; POLICY CLEAN is not a verification proof.") end +function run_package(file, score) + report = try + audit_package(file; mutation_score=score) + catch err + println(stderr, "Apodeixis could not read ", file, ": ", sprint(showerror, err)) + return 2 + end + for r in report.reports + if isempty(r.findings) + println("POLICY CLEAN (NOT VERIFIED): ", r.filename) + else + println("REJECTED: ", r.filename) + for f in r.findings + println(" ", f.code, " [line ", f.line, "]: ", f.message) + end + end + end + if !isempty(report.findings) + println("PACKAGE FINDINGS:") + for f in report.findings + println(" ", f.code, " [line ", f.line, "]: ", f.message) + end + end + if !isempty(report.unreached) + println("UNREACHED (not audited, no include reaches them):") + for u in report.unreached + println(" ", u) + end + end + nfiles = length(report.reports) + nbad = count(r -> !isempty(r.findings), report.reports) + println(is_policy_clean(report) ? "PACKAGE POLICY CLEAN (NOT VERIFIED): " : "PACKAGE REJECTED: ", + nfiles, " file(s) reached, ", nbad, " with findings, ", + length(report.findings), " package finding(s), ", + length(report.unreached), " unreached") + return is_policy_clean(report) ? 0 : 1 +end + function main(args) if isempty(args) || args == ["--help"] || args == ["-h"] usage() return isempty(args) ? 2 : 0 end - args[1] == "audit" || (usage(stderr); return 2) + args[1] in ("audit", "audit-package") || (usage(stderr); return 2) + package_mode = args[1] == "audit-package" score = nothing file = nothing @@ -43,6 +83,7 @@ function main(args) file === nothing && (usage(stderr); return 2) isfile(file) || (println(stderr, "file not found: ", file); return 2) + package_mode && return run_package(file, score) report = try audit_file(file; mutation_score=score) catch err diff --git a/src/Apodeixis.jl b/src/Apodeixis.jl index 9da60bca..91314cf3 100644 --- a/src/Apodeixis.jl +++ b/src/Apodeixis.jl @@ -12,7 +12,8 @@ module Apodeixis using JuliaSyntax -export Finding, AuditReport, audit_source, audit_file, is_policy_clean +export Finding, AuditReport, PackageReport, audit_source, audit_file, + audit_package, is_policy_clean """One source-policy diagnostic. `line` is 1-based when known.""" struct Finding @@ -228,13 +229,20 @@ inside `@verified` bodies and unmodeled foreign calls are rejected. """ function audit_source(source::AbstractString; filename::AbstractString="", mutation_score::Union{Nothing,Real}=nothing) + return _audit(String(source), String(filename), mutation_score, nothing) +end + +# Shared by `audit_source` and package mode. `prepare!`, when given, receives +# the parsed tree before the walk and may rewrite it (package mode removes the +# one permitted form of `include`). `audit_source` passes `nothing`. +function _audit(source::String, filename::String, mutation_score, prepare!) if mutation_score !== nothing && !(0 <= mutation_score <= 100) throw(ArgumentError("mutation_score must be a percentage between 0 and 100")) end findings = Finding[] expr = try - JuliaSyntax.parseall(Expr, String(source)) + JuliaSyntax.parseall(Expr, source) catch err _push_once!(findings, :APX_PARSE_ERROR, 1, "JuliaSyntax could not parse source: " * sprint(showerror, err)) @@ -242,11 +250,12 @@ function audit_source(source::AbstractString; filename::AbstractString=" end if expr !== nothing + prepare! === nothing || prepare!(expr) _walk!(expr, false, mutation_score, 1, findings) end status = isempty(findings) ? :policy_clean : :rejected - return AuditReport(String(filename), status, findings) + return AuditReport(filename, status, findings) end """Read and audit a Julia source file.""" @@ -254,4 +263,176 @@ function audit_file(path::AbstractString; kwargs...) return audit_source(read(path, String); filename=String(path), kwargs...) end +# --------------------------------------------------------------------------- +# Package mode +# --------------------------------------------------------------------------- + +""" +Result of `audit_package`: the root file, one `AuditReport` per reached file +(in the order reached), package-level findings, and the `.jl` files under the +root directory that no `include` reaches. Unreached files are listed so that an +unaudited file is visible; they are not findings and do not change `status`. +Package-level findings name the including file in their message and carry the +line of the `include` in that file. +""" +struct PackageReport + root::String + status::Symbol + reports::Vector{AuditReport} + findings::Vector{Finding} + unreached::Vector{String} +end + +"""True when every reached file is policy clean and there is no package-level finding.""" +is_policy_clean(report::PackageReport) = + report.status === :policy_clean && + isempty(report.findings) && all(is_policy_clean, report.reports) + +# The one permitted form: `include("literal.jl")`, one plain string literal +# argument, as a statement directly at top level or directly in a module body. +_is_literal_include(x) = x isa Expr && x.head === :call && length(x.args) == 2 && + x.args[1] === :include && x.args[2] isa String + +# The statement list of a `module` or `baremodule`, or `nothing`. A docstring on +# the module (JuliaSyntax writes it as a `Core.@doc` call around the module) does +# not change where the module body is; no other macro is looked through. +function _module_body(x) + x isa Expr || return nothing + if x.head === :macrocall && length(x.args) == 4 && x.args[1] isa GlobalRef && + x.args[1].mod === Core && x.args[1].name === Symbol("@doc") + x = x.args[4] + x isa Expr || return nothing + end + x.head === :module && length(x.args) >= 3 && x.args[end] isa Expr && + x.args[end].head === :block ? x.args[end].args : nothing +end + +# Remove each permitted include from the tree, record it, and leave `nothing` +# in its place. Every other `include` stays in the tree and the walk flags it. +function _extract_includes!(stmts::Vector{Any}, found::Vector{Tuple{String,Int}}) + line = 1 + for i in eachindex(stmts) + arg = stmts[i] + if arg isa LineNumberNode + line = Int(arg.line) + elseif _is_literal_include(arg) + push!(found, (arg.args[2], line)) + stmts[i] = nothing + else + body = _module_body(arg) + body === nothing || _extract_includes!(body, found) + end + end + return found +end + +function _extract_includes!(expr::Expr) + found = Tuple{String,Int}[] + expr.head === :toplevel && _extract_includes!(expr.args, found) + return found +end + +_inside_dir(path::AbstractString, dir::AbstractString) = + path == dir || startswith(path, dir * Base.Filesystem.path_separator) + +""" + audit_package(root_file; mutation_score=nothing) + +Audit a package from its root file. The root and every file that a permitted +`include` reaches are audited with the rules of `audit_source`. Only one form +of `include` is permitted, and only here: a call with one plain string literal +argument, written as a statement at top level or directly in a `module` or +`baremodule` body. The path must be relative, end in `.jl`, resolve against the +directory of the including file, and stay inside the root file's directory +after `realpath`. Every other `include` is `APX_DYNAMIC_ESCAPE`. + +Package-level finding codes: `APX_INCLUDE_PATH` (path breaks a form rule), +`APX_INCLUDE_OUTSIDE_ROOT`, `APX_INCLUDE_MISSING`, `APX_INCLUDE_DUPLICATE` +(file reached twice), `APX_INCLUDE_CYCLE`, `APX_PACKAGE_SCAN_ERROR` (a directory +could not be listed). This is a source-policy audit, not a proof. +""" +function audit_package(root_file::AbstractString; + mutation_score::Union{Nothing,Real}=nothing) + if mutation_score !== nothing && !(0 <= mutation_score <= 100) + throw(ArgumentError("mutation_score must be a percentage between 0 and 100")) + end + root = String(root_file) + reports = AuditReport[] + findings = Finding[] + + if !isfile(root) + push!(findings, Finding(:APX_INCLUDE_MISSING, 1, + "root file does not exist or is not a regular file: $root")) + return PackageReport(root, :rejected, reports, findings, String[]) + end + real_root = realpath(root) + root_dir = dirname(real_root) + visited = Set{String}() + stack = String[] + + relname(p) = relpath(p, root_dir) + + function visit(path::String, real::String) + push!(visited, real) + push!(stack, real) + found = Tuple{String,Int}[] + report = _audit(read(real, String), path, mutation_score, + expr -> append!(found, _extract_includes!(expr))) + push!(reports, report) + here = dirname(real) + who = relname(real) + for (rel, line) in found + fail(code, msg) = push!(findings, Finding(code, max(line, 1), "$who: $msg")) + if isempty(rel) || occursin('\0', rel) || isabspath(rel) || !endswith(rel, ".jl") + fail(:APX_INCLUDE_PATH, + "include path must be relative, non-empty and end in .jl: $(repr(rel))") + continue + end + cand = normpath(joinpath(here, rel)) + if ispath(cand) + rp = realpath(cand) + if !_inside_dir(rp, root_dir) + fail(:APX_INCLUDE_OUTSIDE_ROOT, + "include leaves the package directory: $(repr(rel))") + elseif !isfile(rp) + fail(:APX_INCLUDE_MISSING, "include is not a regular file: $(repr(rel))") + elseif rp in stack + fail(:APX_INCLUDE_CYCLE, + "include cycle: $(repr(rel)) is already being included") + elseif rp in visited + fail(:APX_INCLUDE_DUPLICATE, + "file is included more than once: $(repr(rel))") + else + visit(cand, rp) + end + elseif !_inside_dir(cand, root_dir) + fail(:APX_INCLUDE_OUTSIDE_ROOT, + "include leaves the package directory: $(repr(rel))") + else + fail(:APX_INCLUDE_MISSING, "included file does not exist: $(repr(rel))") + end + end + pop!(stack) + return nothing + end + + visit(root, real_root) + + unreached = String[] + scan_error(err) = push!(findings, Finding(:APX_PACKAGE_SCAN_ERROR, 1, + "could not list a directory: " * sprint(showerror, err))) + for (dir, _, files) in walkdir(root_dir; onerror=scan_error) + for f in files + endswith(f, ".jl") || continue + p = joinpath(dir, f) + rp = try realpath(p) catch; p end + rp in visited || push!(unreached, relname(p)) + end + end + sort!(unreached) + + clean = isempty(findings) && all(is_policy_clean, reports) + return PackageReport(root, clean ? :policy_clean : :rejected, reports, findings, unreached) +end + end # module Apodeixis diff --git a/test/runtests.jl b/test/runtests.jl index 494a3b86..d5b154d4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -461,3 +461,235 @@ end end end end + +# Package mode: one permitted form of include, every file reached is audited. +function _make_package(files::Dict{String,String}) + dir = realpath(mktempdir()) + for (rel, text) in files + path = joinpath(dir, rel) + mkpath(dirname(path)) + write(path, text) + end + return dir +end + +pkg_codes(report) = Set(f.code for f in report.findings) + +@testset "Package mode: audit_package" begin + clean_pkg = Dict("a.jl" => "a() = 1\n", "b.jl" => "b() = 2\n") + + @testset "(a) literal includes of clean files are clean" begin + dir = _make_package(merge(clean_pkg, Dict("Root.jl" => + "module Root\ninclude(\"a.jl\")\ninclude(\"b.jl\")\nend\n"))) + report = audit_package(joinpath(dir, "Root.jl")) + @test is_policy_clean(report) + @test report.status === :policy_clean + @test length(report.reports) == 3 + @test isempty(report.findings) + @test isempty(report.unreached) + end + + @testset "top level include and nested modules are permitted" begin + dir = _make_package(merge(clean_pkg, Dict("Root.jl" => + "include(\"a.jl\")\nbaremodule M\nmodule N\ninclude(\"b.jl\")\nend\nend\n"))) + @test is_policy_clean(audit_package(joinpath(dir, "Root.jl"))) + end + + @testset "a docstring on the module does not hide its body" begin + dir = _make_package(merge(clean_pkg, Dict("Root.jl" => + "\"\"\"Docs.\"\"\"\nmodule Root\ninclude(\"a.jl\")\n\"\"\"Inner.\"\"\"\nmodule N\ninclude(\"b.jl\")\nend\nend\n"))) + @test is_policy_clean(audit_package(joinpath(dir, "Root.jl"))) + # Only the docstring form is looked through, not an arbitrary macro. + dir = _make_package(Dict("Root.jl" => "@other module Root\ninclude(\"a.jl\")\nend\n", + "a.jl" => "a() = 1\n")) + @test !is_policy_clean(audit_package(joinpath(dir, "Root.jl"))) + end + + @testset "(b) an included file with eval is not clean" begin + dir = _make_package(Dict("Root.jl" => "include(\"a.jl\")\n", + "a.jl" => "f(x) = eval(x)\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test isempty(report.findings) + @test is_policy_clean(report.reports[1]) + @test :APX_DYNAMIC_ESCAPE in codes(report.reports[2]) + end + + @testset "(c) computed paths are flagged" begin + for arg in ("\"a\" * \".jl\"", "joinpath(\"a.jl\")", "\"\$(1)a.jl\"", + "raw\"a.jl\"", "name", "\"a.jl\"; x = 1", "@__FILE__") + dir = _make_package(Dict("Root.jl" => "name = \"a.jl\"\ninclude($arg)\n", + "a.jl" => "a() = 1\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test :APX_DYNAMIC_ESCAPE in codes(report.reports[1]) + end + end + + @testset "(d) include in a non-statement position is flagged" begin + bodies = ( + "function f()\n include(\"a.jl\")\nend", + "f() = include(\"a.jl\")", + "let\n include(\"a.jl\")\nend", + "begin\n include(\"a.jl\")\nend", + "if true\n include(\"a.jl\")\nend", + "for i in 1:2\n include(\"a.jl\")\nend", + "while false\n include(\"a.jl\")\nend", + "try\n include(\"a.jl\")\ncatch\nend", + "foreach(1:2) do i\n include(\"a.jl\")\nend", + "g = () -> include(\"a.jl\")", + "quote\n include(\"a.jl\")\nend", + "struct S\n x::Int\n S() = new(include(\"a.jl\"))\nend", + "@eval include(\"a.jl\")", + "@static if true\n include(\"a.jl\")\nend", + "@other include(\"a.jl\")", + "x = include(\"a.jl\")", + "module M\nf() = include(\"a.jl\")\nend", + ) + for body in bodies + dir = _make_package(Dict("Root.jl" => body * "\n", "a.jl" => "a() = 1\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test :APX_DYNAMIC_ESCAPE in codes(report.reports[1]) + @test length(report.reports) == 1 + end + end + + @testset "(e) a path that leaves the root is flagged" begin + outer = _make_package(Dict("outside.jl" => "o() = 1\n")) + mkpath(joinpath(outer, "pkg")) + write(joinpath(outer, "pkg", "Root.jl"), "include(\"../outside.jl\")\n") + report = audit_package(joinpath(outer, "pkg", "Root.jl")) + @test !is_policy_clean(report) + @test :APX_INCLUDE_OUTSIDE_ROOT in pkg_codes(report) + @test length(report.reports) == 1 + + abs_dir = _make_package(Dict("Root.jl" => "include(\"$(joinpath(outer, "outside.jl"))\")\n")) + report = audit_package(joinpath(abs_dir, "Root.jl")) + @test :APX_INCLUDE_PATH in pkg_codes(report) + @test !is_policy_clean(report) + + for rel in ("a.txt", "a", "") + d = _make_package(Dict("Root.jl" => "include(\"$rel\")\n")) + @test :APX_INCLUDE_PATH in pkg_codes(audit_package(joinpath(d, "Root.jl"))) + end + + if !Sys.iswindows() + linked = _make_package(Dict("Root.jl" => "include(\"link.jl\")\ninclude(\"dir/b.jl\")\n")) + symlink(joinpath(outer, "outside.jl"), joinpath(linked, "link.jl")) + symlink(outer, joinpath(linked, "dir")) + report = audit_package(joinpath(linked, "Root.jl")) + @test !is_policy_clean(report) + @test count(f -> f.code === :APX_INCLUDE_OUTSIDE_ROOT, report.findings) >= 1 + @test length(report.reports) == 1 + end + end + + @testset "(f) a missing file is flagged" begin + dir = _make_package(Dict("Root.jl" => "include(\"nope.jl\")\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test :APX_INCLUDE_MISSING in pkg_codes(report) + @test only(report.findings).line == 1 + @test !is_policy_clean(audit_package(joinpath(dir, "absent.jl"))) + end + + @testset "(g) a file included twice is flagged" begin + dir = _make_package(Dict("Root.jl" => "include(\"a.jl\")\ninclude(\"a.jl\")\n", + "a.jl" => "a() = 1\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test :APX_INCLUDE_DUPLICATE in pkg_codes(report) + @test length(report.reports) == 2 + end + + @testset "(h) an include cycle is flagged" begin + dir = _make_package(Dict("Root.jl" => "include(\"a.jl\")\n", + "a.jl" => "include(\"b.jl\")\n", + "b.jl" => "include(\"a.jl\")\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test :APX_INCLUDE_CYCLE in pkg_codes(report) + @test length(report.reports) == 3 + + self = _make_package(Dict("Root.jl" => "include(\"Root.jl\")\n")) + @test :APX_INCLUDE_CYCLE in pkg_codes(audit_package(joinpath(self, "Root.jl"))) + end + + @testset "(i) Base.include, Core.include and two-argument include are flagged" begin + for call in ("Base.include(@__MODULE__, \"a.jl\")", "Core.include(Main, \"a.jl\")", + "include(identity, \"a.jl\")", "include(\"a.jl\", \"b.jl\")", + "include()", "g = include", "map(include, [\"a.jl\"])") + dir = _make_package(Dict("Root.jl" => call * "\n", "a.jl" => "a() = 1\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test :APX_DYNAMIC_ESCAPE in codes(report.reports[1]) + @test length(report.reports) == 1 + end + end + + @testset "(j) nested include resolves against the including file" begin + dir = _make_package(Dict("Root.jl" => "include(\"sub/mid.jl\")\n", + "sub/mid.jl" => "include(\"leaf.jl\")\ninclude(\"../top.jl\")\n", + "sub/leaf.jl" => "leaf() = 1\n", + "top.jl" => "top() = 2\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test is_policy_clean(report) + @test length(report.reports) == 4 + @test isempty(report.unreached) + + # The same name beside the root is a different, missing file. + miss = _make_package(Dict("Root.jl" => "include(\"sub/mid.jl\")\n", + "sub/mid.jl" => "include(\"leaf.jl\")\n", + "leaf.jl" => "leaf() = 1\n")) + report = audit_package(joinpath(miss, "Root.jl")) + @test :APX_INCLUDE_MISSING in pkg_codes(report) + @test report.unreached == ["leaf.jl"] + end + + @testset "(k) unreached files are listed and do not change cleanliness" begin + dir = _make_package(Dict("Root.jl" => "include(\"a.jl\")\n", "a.jl" => "a() = 1\n", + "stray.jl" => "eval(:(1))\n", "deep/other.jl" => "o() = 1\n", + "notes.txt" => "x\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test is_policy_clean(report) + @test report.unreached == ["deep/other.jl", "stray.jl"] + end + + @testset "(l) audit_file and audit_source still forbid include" begin + dir = _make_package(Dict("Root.jl" => "module Root\ninclude(\"a.jl\")\nend\n", + "a.jl" => "a() = 1\n")) + report = audit_file(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test :APX_DYNAMIC_ESCAPE in codes(report) + @test :APX_DYNAMIC_ESCAPE in codes(audit_source("include(\"a.jl\")")) + end + + @testset "mutation score is validated and passed through" begin + dir = _make_package(Dict("Root.jl" => "include(\"a.jl\")\n", + "a.jl" => "f(A) = @inbounds A[1]\n")) + @test_throws ArgumentError audit_package(joinpath(dir, "Root.jl"); mutation_score=101) + @test !is_policy_clean(audit_package(joinpath(dir, "Root.jl"))) + end +end + +@testset "Package mode: CLI audit-package" begin + dir = _make_package(Dict("Root.jl" => "include(\"a.jl\")\n", "a.jl" => "a() = 1\n", + "stray.jl" => "s() = 1\n")) + root = joinpath(dir, "Root.jl") + result = CLIUnderTest.invoke(["audit-package", root]) + @test result.status == 0 + @test occursin("POLICY CLEAN (NOT VERIFIED): $root", result.out) + @test occursin("UNREACHED", result.out) && occursin("stray.jl", result.out) + @test occursin("2 file(s) reached, 0 with findings, 0 package finding(s), 1 unreached", result.out) + + bad = _make_package(Dict("Root.jl" => "include(\"gone.jl\")\nf() = eval(1)\n")) + result = CLIUnderTest.invoke(["audit-package", joinpath(bad, "Root.jl")]) + @test result.status == 1 + @test occursin("APX_DYNAMIC_ESCAPE", result.out) + @test occursin("APX_INCLUDE_MISSING", result.out) + @test occursin("PACKAGE REJECTED", result.out) + + @test CLIUnderTest.invoke(["audit-package"]).status == 2 + @test CLIUnderTest.invoke(["audit-package", joinpath(dir, "none.jl")]).status == 2 +end From b19a4bdddf4f0b0dbb51e5b414bf721846c70e41 Mon Sep 17 00:00:00 2001 From: CLAUDE <355856132+HoTT-stuff@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:33:22 +0000 Subject: [PATCH 2/2] test(audit): pin the docstring look-through and a user-defined include Co-Authored-By: Claude Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0136P7Zpa1S5bdGh4d9CQ9tt --- test/runtests.jl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index d5b154d4..b15444b4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -503,6 +503,30 @@ pkg_codes(report) = Set(f.code for f in report.findings) dir = _make_package(Dict("Root.jl" => "@other module Root\ninclude(\"a.jl\")\nend\n", "a.jl" => "a() = 1\n")) @test !is_policy_clean(audit_package(joinpath(dir, "Root.jl"))) + + # A user-written doc wrapper is not the parser's docstring node. + for wrapper in ("Core.@doc \"d\"", "@doc \"d\"") + dir = _make_package(Dict("Root.jl" => "$wrapper module Root\ninclude(\"a.jl\")\nend\n", + "a.jl" => "a() = 1\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test :APX_DYNAMIC_ESCAPE in codes(report.reports[1]) + end + + # Code inside the module docstring is still audited. + dir = _make_package(Dict("Root.jl" => "\"\"\"Docs \$(eval(1)).\"\"\"\nmodule Root\ninclude(\"a.jl\")\nend\n", + "a.jl" => "a() = 1\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test :APX_DYNAMIC_ESCAPE in codes(report.reports[1]) + end + + @testset "a package that defines its own include is flagged" begin + dir = _make_package(Dict("Root.jl" => "module Root\ninclude(x) = x\ninclude(\"a.jl\")\nend\n", + "a.jl" => "a() = 1\n")) + report = audit_package(joinpath(dir, "Root.jl")) + @test !is_policy_clean(report) + @test :APX_DYNAMIC_ESCAPE in codes(report.reports[1]) end @testset "(b) an included file with eval is not clean" begin