Skip to content

Fix the compliance flags' arity, and gate the class - #842

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/808-compliance-flag-arity
Oct 11, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/808-compliance-flag-arity

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Closes #808. Correctness audit 2026-10. Last of Phase 3.

The defect

Both compliance flags were registered as StringVar, so pflag required a value —
while the mode was decided by Changed() and the value was never read:

validateCmd.Flags().StringVar(&validateComplianceMode, "nist-800-171", "", "…")
validateCmd.Flags().StringVar(&validateComplianceMode, "nist-800-53",  "", "…")

--nist-800-171 has nothing to supply, so it ate the next token:

$ spawn validate --nist-800-171
flag needs an argument: --nist-800-171

$ spawn validate --nist-800-171 -o json
Compliance Validation Report (NIST 800-171 Rev 3)      ← a TABLE, exit 0

-o became the flag's value and json a positional, so the documented JSON path
silently produced the wrong format. Three of the tool's own words failed: the Example
spawn validate --nist-800-171, the same invocation in its -o json Example, and the
remedy its error message prints.

After

$ spawn validate --nist-800-171 -o json
{ "compliance_mode": "NIST 800-171 Rev 3", "compliant_count": 2, …

$ spawn validate --nist-800-53              # bare = low, as the handler always assumed
Compliance Validation Report (NIST 800-53 Low Baseline)

$ spawn validate --nist-800-53=nonsense
invalid --nist-800-53 baseline "nonsense": want one of low, moderate, high

$ spawn validate --nist-800-171 --nist-800-53=high
--nist-800-171 and --nist-800-53 are mutually exclusive; pass one

--nist-800-171 is a bool. --nist-800-53 keeps its baseline and gains
NoOptDefVal = "low". The invalid baseline is rejected at the flag, naming the
accepted values, instead of surfacing twelve frames down as
unknown compliance mode: nist-800-53-nonsense — an internal string rather than the flag
the user typed. And both frameworks at once is refused rather than 800-171 silently
winning: a caller who passed both asked for something this command cannot do, and quietly
doing half of it is the failure mode this whole issue is about.

Gates, and why #807's didn't catch this

TestExampleFlagsCanBeWrittenAsDocumented — every flag a command documents without a
value must accept being written that way. #806's gate checks a named flag exists; it
cannot tell that the flag demands an argument the example omits. This resolves against
pflag's own notion of arity, which is the only authority on it.

TestNoTwoLiveFlagsShareOneVariable — this is precisely what hid #808 from #807's
unread-flag gate. Both flags bound to validateComplianceMode, so it had a declaration
and two registrations: a reference count of three, which looked read. Shared variables
also clobber each other, whichever parses last winning.

TestValidateCallsTheBaselineCheck — because testing a validator is not testing that
anything validates. TestNIST53BaselineValidation exercises isNIST53Baseline directly
and kept passing against a revert to if !isNIST53Baseline(val) && false, which left the
function correct and the handler unguarded.

Two gate bugs found by running them

Both worth recording, because each would have made the gate useless in a different
direction:

  • The arity gate's first version produced ~40 false positives. It computed "was a
    value supplied?" from whether any later flag appeared anywhere on the line, so
    --stack my-stack --region x looked like --stack had no value. The question is only
    ever about the immediately following token; it walks tokens now instead of regex
    positions.
  • The shared-variable gate exempted only deprecated aliases, and reported launch --print-config — a deliberate live alias of --dry-run, declared as such in its
    usage string. The exemption now comes from the code's own statement of intent
    (deprecated, or a usage matching "Alias for --x"), the same way Two inert flags: pipeline launch --wait never waits, and logs --region is dropped while shadowing the global --region #807's exemption comes
    from MarkDeprecated, rather than from a list I maintain.

Verification

make check, make check-docs with docs-gen/validate.md regenerated, changelog gate,
semgrep 0 findings, Tier 0 e2e with -count=1.

Five compiling reverts: --nist-800-171 back to a StringVar (caught by the general
Example gate, reported against the command's own documented examples), NoOptDefVal
dropped, the baseline check disabled, and two live flags on one variable. The
StringVar revert needed two attempts — the first did not compile, so it proved nothing.

@scttfrdmn scttfrdmn added this to the Correctness audit 2026-10 milestone Oct 11, 2026
@codecov

codecov Bot commented Oct 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/validate.go 50.00% 10 Missing ⚠️

📢 Thoughts on this report? Let us know!

Both compliance flags were registered as StringVar, so pflag REQUIRED a
value — while the mode was decided by Changed() and the value was never
read. --nist-800-171 has nothing to supply, so it ate the next token:
`spawn validate --nist-800-171 -o json` consumed "-o" as the flag's
value and "json" as a positional, then printed a human table and exited
0. The documented JSON path silently produced the wrong format.

Three of the tool's own words failed as a result: the Example `spawn
validate --nist-800-171`, the same invocation in its -o json Example,
and the remedy its error message prints.

--nist-800-171 is a bool now. --nist-800-53 keeps its baseline and gains
NoOptDefVal = "low", so a bare --nist-800-53 means what the handler
already assumed when the value was empty. An invalid baseline is
rejected at the flag naming the accepted values, instead of surfacing
twelve frames down as "unknown compliance mode: nist-800-53-nonsense".
Passing both frameworks is refused rather than 800-171 silently winning.

TestExampleFlagsCanBeWrittenAsDocumented gates the class: a flag a
command documents without a value must accept being written that way.
an argument the example omits.

Its first version asked "was a value supplied?" by checking whether any
later flag appeared anywhere on the line, which reported `--stack
my-stack --region x` as giving --stack no value — about forty false
positives. The question is only ever about the next token.

TestNoTwoLiveFlagsShareOneVariable covers what hid this from #807's
unread-flag gate: both flags bound to one variable, so its reference
count of three looked like it was read. Aliases are exempt by the code's
own declaration rather than a hand list — deprecated, or a usage saying
"Alias for --x". The first version exempted only deprecated flags and
reported `launch --print-config`, a deliberate LIVE alias of --dry-run.

TestValidateCallsTheBaselineCheck exists because testing a validator is
not testing that anything validates: TestNIST53BaselineValidation kept
passing against a revert to `if !isNIST53Baseline(val) && false`, which
left the function correct and the handler unguarded.

Closes #808
@scttfrdmn
scttfrdmn force-pushed the fix/808-compliance-flag-arity branch from 99963dd to a68a99e Compare October 11, 2026 20:29
@scttfrdmn
scttfrdmn merged commit 84d041c into main Oct 11, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/808-compliance-flag-arity branch October 11, 2026 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

spawn validate --nist-800-171 -o json silently prints a table: the compliance flags demand a value they then ignore

1 participant