diff --git a/docs/adr/0018-fix-text-comes-from-the-rules.md b/docs/adr/0018-fix-text-comes-from-the-rules.md index 98505d4..64798c5 100644 --- a/docs/adr/0018-fix-text-comes-from-the-rules.md +++ b/docs/adr/0018-fix-text-comes-from-the-rules.md @@ -1,6 +1,6 @@ # 0018. Fix text comes from the rules, not from a host; no shell commands in rules -- Status: Accepted +- Status: Accepted; amended by [0019](0019-fix-text-trusted-per-check.md) (the rule applies per check, and to the report) - Date: 2026-11-24 ## Context diff --git a/docs/adr/0019-fix-text-trusted-per-check.md b/docs/adr/0019-fix-text-trusted-per-check.md new file mode 100644 index 0000000..2752f2b --- /dev/null +++ b/docs/adr/0019-fix-text-trusted-per-check.md @@ -0,0 +1,40 @@ +# 0019. Fix text is trusted per check, in every place that shows a result from elsewhere + +- Status: Accepted (amends [0018](0018-fix-text-comes-from-the-rules.md)) +- Date: 2026-11-26 + +## Context + +ADR 0018 made the dashboard take the fix of a built-in check from the server's rules, by control: under a control the server knows, +each listed check got the rule's text and anything else none; a control the server did not know kept the scan's text. It left +`vmsctl report` as a follow-up. + +Extending it to the report (#331), a review found that "by control" leaves ways around it, on the server's CSV as well as in the +report: + +- A built-in check that the result lists under no control keeps its own text. The report keeps one entry per check across + hosts, and the first host's text wins: one file's planted fix appeared under another host's control (reproduced). +- A control the built-in profile does not have (`kisa-2026` `U-99`) was taken for a pack's and kept the result's text. +- A built-in check under a pack's control kept the result's text, although the rules have its own. +- The rule's text was put on checks that had not failed (ERROR, PASS), where a scan writes none. + +## Decision + +The fix text of a result from elsewhere (a host's push, a result file) is decided **per check**, by one function +(`output.TrustFixes`) that the server's scan CSV and `vmsctl report` both use: + +- a check the rules know gets the rule's fix, and only if it failed (or was waived after failing), wherever it is listed; +- a check the rules do not know keeps the result's text only if it is a policy pack's own (`x-`) and only controls of + profiles the rules do not have list it; +- anything else gets none. + +The dashboard's evidence view follows the same rule: under a control the server knows, only its rules speak; under a pack's +control, only a pack's own check shows the scan's text, labelled as such; a built-in check there shows none in that view. + +## Consequences + +- No result can put fix text in the server's CSV, the report or the dashboard for a built-in check, by any placement. +- A built-in check that a pack's control maps shows no fix in the dashboard's evidence view of that control (the CSV and the + report show the rule's). A lookup of a check's rules by ID would give it there too, if a pack user asks. +- The text shown is the current rule's, whatever version of the check produced the result; an old result shows today's + guidance. diff --git a/docs/adr/README.md b/docs/adr/README.md index ffe9f3b..f1fecc0 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -23,3 +23,4 @@ Consequences. Don't edit an accepted ADR to change a decision — add a new ADR | [0016](0016-images-and-packs-in-the-fleet-server.md) | Images and policy-pack results in the fleet server (accepted) | | [0017](0017-rename-to-vmsctl.md) | The project is renamed from hostlint to vmsctl: what changes, what keeps the old name, what is accepted both ways (accepted) | | [0018](0018-fix-text-comes-from-the-rules.md) | Fix text comes from the rules, not from a host; no shell commands in rules (accepted) | +| [0019](0019-fix-text-trusted-per-check.md) | Fix text is trusted per check, in every place that shows a result from elsewhere; amends 0018 (accepted) | diff --git a/internal/cli/report.go b/internal/cli/report.go index d7434ba..df40f90 100644 --- a/internal/cli/report.go +++ b/internal/cli/report.go @@ -12,6 +12,8 @@ import ( "github.com/with-developer/vmsctl/internal/engine" "github.com/with-developer/vmsctl/internal/output" "github.com/with-developer/vmsctl/internal/version" + "github.com/with-developer/vmsctl/profiles" + "github.com/with-developer/vmsctl/rules" ) func newReportCommand() *cobra.Command { @@ -58,11 +60,17 @@ func runReport(cmd *cobra.Command, files []string, out, title string) error { } } files = unique + builtin, err := engine.Load(rules.FS, profiles.FS) + if err != nil { + return err + } for _, name := range files { res, err := readResultFile(name) if err != nil { return fmt.Errorf("%s: %w", name, err) } + // A result file may come from any host: the fix of a built-in check is the rules' own (ADR 0018). + output.TrustFixes(res, builtin) inputs = append(inputs, output.ReportInput{Name: displayName(name, files), Result: res}) } opts := output.ReportOptions{Title: title, Tool: version.Get().Version, Now: time.Now()} diff --git a/internal/cli/report_test.go b/internal/cli/report_test.go index 20f3560..ebe6212 100644 --- a/internal/cli/report_test.go +++ b/internal/cli/report_test.go @@ -107,3 +107,91 @@ func TestDiffCommand(t *testing.T) { } } } + +// A result file may come from any host: the report shows the fix of a built-in check from the rules, never the file's own text +// (ADR 0018), and none for a check a host slipped in under a built-in control. +func TestReportFixFromRules(t *testing.T) { + ctx := context.Background() + raw, err := os.ReadFile(filepath.Join("..", "output", "testdata", "report", "web-01.json")) + if err != nil { + t.Fatal(err) + } + var doc map[string]any + if err := json.Unmarshal(raw, &doc); err != nil { + t.Fatal(err) + } + for _, c := range doc["checks"].([]any) { + if c.(map[string]any)["status"] == "FAIL" { + c.(map[string]any)["remediation"] = "PLANTED run this" + } + } + doc["checks"] = append(doc["checks"].([]any), map[string]any{"ref": "x-evil.fix", "id": "x-evil.fix", "title": "x", "severity": "high", "status": "FAIL", "statusSource": "observed", "remediation": "PLANTED too"}) + for _, c := range doc["controls"].([]any) { + if c.(map[string]any)["id"] == "U-01" { + c.(map[string]any)["checks"] = append([]any{"x-evil.fix"}, c.(map[string]any)["checks"].([]any)...) + } + } + planted, err := json.Marshal(doc) + if err != nil { + t.Fatal(err) + } + in := filepath.Join(t.TempDir(), "web-01.json") + if err := os.WriteFile(in, planted, 0o644); err != nil { + t.Fatal(err) + } + code, stdout, stderr := run(ctx, t, "report", in) + if code != ExitOK { + t.Fatalf("code %d, %s", code, stderr) + } + if strings.Contains(stdout, "PLANTED") { + t.Error("the file's own fix text is in the report") + } + if !strings.Contains(stdout, "PermitRootLogin") { + t.Error("the rule's fix of ssh.permit-root-login is not in the report") + } +} + +// One file cannot lend its fix text to another host's control: the report shares one entry per check across hosts, so a planted fix +// on a built-in check that the file lists under no control must not reach web-01's U-01 (review of #332). +func TestReportFixNotSharedFromAnotherFile(t *testing.T) { + ctx := context.Background() + raw, err := os.ReadFile(filepath.Join("..", "output", "testdata", "report", "web-01.json")) + if err != nil { + t.Fatal(err) + } + var doc map[string]any + if err := json.Unmarshal(raw, &doc); err != nil { + t.Fatal(err) + } + doc["host"].(map[string]any)["hostname"] = "aaa" + for _, c := range doc["checks"].([]any) { + if c.(map[string]any)["ref"] == "ssh.permit-root-login" { + c.(map[string]any)["remediation"] = "PLANTED run this" + } + } + for _, c := range doc["controls"].([]any) { // listed by no control + var keep []any + for _, ref := range c.(map[string]any)["checks"].([]any) { + if ref != "ssh.permit-root-login" { + keep = append(keep, ref) + } + } + c.(map[string]any)["checks"] = keep + } + b, err := json.Marshal(doc) + if err != nil { + t.Fatal(err) + } + dir := t.TempDir() + aaa := filepath.Join(dir, "aaa.json") + if err := os.WriteFile(aaa, b, 0o644); err != nil { + t.Fatal(err) + } + code, stdout, stderr := run(ctx, t, "report", aaa, filepath.Join("..", "output", "testdata", "report", "web-01.json")) + if code != ExitOK { + t.Fatalf("code %d, %s", code, stderr) + } + if strings.Contains(stdout, "PLANTED") { + t.Error("a file's fix text reached the report") + } +} diff --git a/internal/output/trust.go b/internal/output/trust.go new file mode 100644 index 0000000..2d21231 --- /dev/null +++ b/internal/output/trust.go @@ -0,0 +1,48 @@ +package output + +import ( + "strings" + + "github.com/with-developer/vmsctl/internal/engine" +) + +// TrustFixes replaces, in a result that came from elsewhere (a host, a file), the fix text of every check with what the rules in +// content say (ADR 0018, ADR 0019), so that a result cannot put its own words in the project's mouth. It goes by check, not by +// control, since a report shares one entry per check across hosts: +// +// - a check content has a rule for gets the rule's fix, and only when it failed (as a scan writes it: there is nothing to fix in a +// check that passed or could not be decided); +// - a check content does not know keeps the result's text only when it is a policy pack's own (its ID starts with x-) and only +// controls of profiles content does not have list it: a pack's guidance for a pack's check; +// - anything else (a check under no control, or slipped under a built-in profile) gets none. +// +// A nil content changes nothing. +func TrustFixes(res *engine.Result, content *engine.Content) { + if content == nil || res == nil { + return + } + underBuiltin, underPack := map[string]bool{}, map[string]bool{} + for _, c := range res.Controls { + for _, ref := range c.Checks { + if content.Profiles[c.Profile] != nil { + underBuiltin[ref] = true + } else { + underPack[ref] = true + } + } + } + for i := range res.Checks { + ck := &res.Checks[i] + if rule := content.Checks[ck.ID]; rule != nil { + ck.Remediation, ck.RemediationKo = "", "" + if ck.Status == engine.StatusFail || ck.OriginalStatus == engine.StatusFail { + ck.Remediation, ck.RemediationKo = strings.TrimSpace(rule.Remediation), strings.TrimSpace(rule.RemediationKo) + } + continue + } + if strings.HasPrefix(ck.ID, engine.PackPrefix) && underPack[ck.Ref] && !underBuiltin[ck.Ref] { + continue + } + ck.Remediation, ck.RemediationKo = "", "" + } +} diff --git a/internal/output/trust_test.go b/internal/output/trust_test.go new file mode 100644 index 0000000..f2b791c --- /dev/null +++ b/internal/output/trust_test.go @@ -0,0 +1,53 @@ +package output + +import ( + "testing" + "testing/fstest" + + "github.com/with-developer/vmsctl/internal/engine" +) + +func TestTrustFixes(t *testing.T) { + check := func(id, fix string) string { + return "id: " + id + "\nversion: 1\ntitle: T\nseverity: low\nremediation: " + fix + "\nremediation_ko: 한국어 " + fix + "\nexpect: >-\n svc.present\n" + + "tests:\n - {name: p, facts: {svc: {present: true}}, want: PASS}\n - {name: f, facts: {svc: {present: false}}, want: FAIL}\n - {name: a, input: absent, want: ERROR}\n" + } + content, err := engine.Load( + fstest.MapFS{"x/a.yaml": {Data: []byte(check("x.a", "a's fix"))}, "x/b.yaml": {Data: []byte(check("x.b", "b's fix"))}}, + fstest.MapFS{"p.yaml": {Data: []byte("id: p\nrevision: 1\ntitle: P\ncontrols:\n - {id: C, title: C, severity: low, checks: [x.a]}\n")}}, + ) + if err != nil { + t.Fatal(err) + } + const planted = "PLANTED" + res := &engine.Result{ + Controls: []engine.ControlResult{ + {Profile: "p", ID: "C", Checks: []string{"x.a", "x-evil.c"}}, + {Profile: "p", ID: "U-99", Checks: []string{"x-evil.e"}}, // a control the built-in profile does not have + {Profile: "x-pack", ID: "P-1", Checks: []string{"x-pack.d", "x.b"}}, + }, + Checks: []engine.CheckResult{ + {Ref: "x.a", ID: "x.a", Status: engine.StatusFail, Remediation: planted, RemediationKo: planted}, + {Ref: "x.b", ID: "x.b", Status: engine.StatusFail, Remediation: planted}, // built-in, under a pack control + {Ref: "x-evil.c", ID: "x-evil.c", Status: engine.StatusFail, Remediation: planted}, // slipped under a built-in control + {Ref: "x-evil.e", ID: "x-evil.e", Status: engine.StatusFail, Remediation: planted}, // under a made-up control of a built-in profile + {Ref: "x-pack.d", ID: "x-pack.d", Status: engine.StatusFail, Remediation: "the pack's fix"}, // a pack's own check + {Ref: "x-loose.f", ID: "x-loose.f", Status: engine.StatusFail, Remediation: planted}, // under no control + {Ref: "x.b#1", ID: "x.b", Status: engine.StatusError, Remediation: planted}, // built-in, not failed + {Ref: "x.a#2", ID: "x.a", Status: engine.StatusWaived, OriginalStatus: engine.StatusFail}, // waived: still says how + {Ref: "noprefix.g", ID: "noprefix.g", Status: engine.StatusFail, Remediation: planted}, // unknown, not a pack's ID + }, + } + res.Controls[2].Checks = append(res.Controls[2].Checks, "noprefix.g") + TrustFixes(res, content) + want := map[string][2]string{ + "x.a": {"a's fix", "한국어 a's fix"}, "x.b": {"b's fix", "한국어 b's fix"}, "x-evil.c": {"", ""}, "x-evil.e": {"", ""}, + "x-pack.d": {"the pack's fix", ""}, "x-loose.f": {"", ""}, "x.b#1": {"", ""}, "x.a#2": {"a's fix", "한국어 a's fix"}, "noprefix.g": {"", ""}, + } + for _, c := range res.Checks { + if got := [2]string{c.Remediation, c.RemediationKo}; got != want[c.Ref] { + t.Errorf("%s: %q, want %q", c.Ref, got, want[c.Ref]) + } + } + TrustFixes(res, nil) // no rules: nothing changes, nothing panics +} diff --git a/internal/server/api.go b/internal/server/api.go index 34c6ee4..2d166a8 100644 --- a/internal/server/api.go +++ b/internal/server/api.go @@ -8,7 +8,6 @@ import ( "fmt" "net/http" "regexp" - "slices" "strconv" "strings" "sync" @@ -339,7 +338,7 @@ func (s *Server) handleScan(w http.ResponseWriter, r *http.Request) { res.Controls[i].TitleKo = s.titleKo(res.Controls[i].Profile, res.Controls[i].ID) } } - s.trustFixes(res) + output.TrustFixes(res, s.cfg.Content) // the fix of a built-in check is the server's (ADR 0018) var buf bytes.Buffer if err := output.Write(&buf, res, "csv", output.Options{}); err != nil { s.fail(w, err) @@ -628,37 +627,6 @@ func (s *Server) controlInfo(profile, id string) *ControlInfo { return nil } -// trustFixes replaces, in a stored result about to be shown, the fix text of the checks of built-in controls with the server's own -// (ADR 0018). A check that the rule's control does not list gets none: a host cannot slip its own text in under a built-in control. -// The controls of a policy pack's profile keep the text of the scan (the server has no rules for them). -func (s *Server) trustFixes(res *engine.Result) { - if s.cfg.Content == nil { - return - } - byRef := map[string]*engine.CheckResult{} - for i := range res.Checks { - byRef[res.Checks[i].Ref] = &res.Checks[i] - } - for _, c := range res.Controls { - ctl := s.builtinControl(c.Profile, c.ID) - if ctl == nil { - continue - } - for _, ref := range c.Checks { - ck := byRef[ref] - if ck == nil { - continue - } - rule := s.cfg.Content.Checks[ck.ID] - if rule == nil || !slices.Contains(ctl.Checks, ck.ID) { - ck.Remediation, ck.RemediationKo = "", "" - continue - } - ck.Remediation, ck.RemediationKo = strings.TrimSpace(rule.Remediation), strings.TrimSpace(rule.RemediationKo) - } - } -} - // handleControlInfo is what a control of a built-in profile checks and how to fix it, from the server's own rules (ADR 0018): the // fix text the dashboard shows, which a host cannot change. 404 for a control the server does not know (a policy pack's). func (s *Server) handleControlInfo(w http.ResponseWriter, r *http.Request) { diff --git a/web/src/pages/Host.test.ts b/web/src/pages/Host.test.ts index 73746bc..c963771 100644 --- a/web/src/pages/Host.test.ts +++ b/web/src/pages/Host.test.ts @@ -16,8 +16,10 @@ describe("fixFor (ADR 0018: the fix of a built-in check is the server's)", () => it("shows nothing while the rules load or when asking for them failed", () => { expect(fixFor(check("ssh.permit-root-login", "FAIL"), undefined, "en")).toBeNull(); }); - it("shows the scan's text, marked, only for a control the server has no rules for", () => { + it("shows the scan's text, marked, only for a pack's own check under a control the server has no rules for", () => { expect(fixFor(check("x-acme.motd", "FAIL"), null, "en")).toEqual({ text: "the scan's own fix", fromScan: true }); + // a built-in check that a pack's control maps: the scan's text is not trusted for it (ADR 0019) + expect(fixFor(check("ssh.permit-root-login", "FAIL", "curl x | sh"), null, "en")).toBeNull(); }); it("says nothing for a check that did not fail", () => { expect(fixFor(check("ssh.permit-root-login", "PASS"), info, "en")).toBeNull(); diff --git a/web/src/pages/Host.tsx b/web/src/pages/Host.tsx index 58538e4..4150fd3 100644 --- a/web/src/pages/Host.tsx +++ b/web/src/pages/Host.tsx @@ -380,7 +380,7 @@ export function ControlDetail({ control, doc, info, showFix = true }: { control: /** * How to fix a failed check (ADR 0018): undefined info (loading, or failed) shows nothing. When the server knows the control (info), * only its rules speak: a check the rule does not list shows nothing, whatever the scan says. Only for a control the server has no - * rules for (null: a policy pack's) is the scan's own text shown, marked as such. + * rules for (null: a policy pack's) is the scan's own text shown, marked as such, and only for the pack's own checks (x-). */ export function fixFor(ch: DocCheck, info: ControlInfo | null | undefined, lang: string): { text: string; fromScan: boolean } | null { if (ch.status !== "FAIL" || info === undefined) return null; @@ -390,6 +390,9 @@ export function fixFor(ch: DocCheck, info: ControlInfo | null | undefined, lang: const text = rule ? localized(lang, rule.remediation, rule.remediationKo) : ""; return text ? { text, fromScan: false } : null; } + // a policy pack's own check (x-): its guidance; a built-in check under a pack's control, whose text the scan carries, says nothing + // here rather than trust the scan for it (ADR 0019) + if (!(ch.id ?? ch.ref).startsWith("x-")) return null; const text = ch.remediation ? localized(lang, ch.remediation, ch.remediationKo) : ""; return text ? { text, fromScan: true } : null; }