omniqa 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/cli.js +6 -1
- package/dist/cli.js.map +1 -1
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +48 -2
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/init.d.ts +13 -0
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +141 -23
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/install.d.ts.map +1 -1
- package/dist/commands/install.js +20 -3
- package/dist/commands/install.js.map +1 -1
- package/dist/commands/knowledge.d.ts +23 -0
- package/dist/commands/knowledge.d.ts.map +1 -1
- package/dist/commands/knowledge.js +215 -39
- package/dist/commands/knowledge.js.map +1 -1
- package/dist/commands/plan-add.d.ts.map +1 -1
- package/dist/commands/plan-add.js +23 -1
- package/dist/commands/plan-add.js.map +1 -1
- package/dist/commands/plan.d.ts.map +1 -1
- package/dist/commands/plan.js +7 -4
- package/dist/commands/plan.js.map +1 -1
- package/dist/commands/security.d.ts +7 -0
- package/dist/commands/security.d.ts.map +1 -0
- package/dist/commands/security.js +69 -0
- package/dist/commands/security.js.map +1 -0
- package/dist/commands/status.d.ts.map +1 -1
- package/dist/commands/status.js +2 -2
- package/dist/commands/status.js.map +1 -1
- package/dist/commands/test.d.ts +5 -0
- package/dist/commands/test.d.ts.map +1 -1
- package/dist/commands/test.js +9 -2
- package/dist/commands/test.js.map +1 -1
- package/dist/commands/write.d.ts.map +1 -1
- package/dist/commands/write.js +26 -5
- package/dist/commands/write.js.map +1 -1
- package/dist/coverage-page.d.ts.map +1 -1
- package/dist/coverage-page.js +2 -2
- package/dist/coverage-page.js.map +1 -1
- package/dist/devices.d.ts +4 -2
- package/dist/devices.d.ts.map +1 -1
- package/dist/devices.js +3 -3
- package/dist/devices.js.map +1 -1
- package/dist/mcp/tools.js +1 -1
- package/dist/mcp/tools.js.map +1 -1
- package/dist/planner.d.ts.map +1 -1
- package/dist/planner.js +13 -9
- package/dist/planner.js.map +1 -1
- package/dist/runner.d.ts +1 -1
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +64 -56
- package/dist/runner.js.map +1 -1
- package/dist/security-checks.d.ts +146 -0
- package/dist/security-checks.d.ts.map +1 -0
- package/dist/security-checks.js +661 -0
- package/dist/security-checks.js.map +1 -0
- package/dist/security-gates.d.ts +28 -0
- package/dist/security-gates.d.ts.map +1 -0
- package/dist/security-gates.js +40 -0
- package/dist/security-gates.js.map +1 -0
- package/dist/security-report.d.ts +24 -0
- package/dist/security-report.d.ts.map +1 -0
- package/dist/security-report.js +49 -0
- package/dist/security-report.js.map +1 -0
- package/dist/security-request.d.ts +109 -0
- package/dist/security-request.d.ts.map +1 -0
- package/dist/security-request.js +254 -0
- package/dist/security-request.js.map +1 -0
- package/dist/security-sources.d.ts +55 -0
- package/dist/security-sources.d.ts.map +1 -0
- package/dist/security-sources.js +75 -0
- package/dist/security-sources.js.map +1 -0
- package/dist/security.d.ts +63 -0
- package/dist/security.d.ts.map +1 -0
- package/dist/security.js +624 -0
- package/dist/security.js.map +1 -0
- package/dist/sync.d.ts +12 -1
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +11 -3
- package/dist/sync.js.map +1 -1
- package/dist/test.d.ts +78 -0
- package/dist/test.d.ts.map +1 -1
- package/dist/test.js +330 -19
- package/dist/test.js.map +1 -1
- package/dist/writer.d.ts +5 -1
- package/dist/writer.d.ts.map +1 -1
- package/dist/writer.js +15 -12
- package/dist/writer.js.map +1 -1
- package/package.json +7 -7
package/dist/security.js
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join, relative } from "node:path";
|
|
4
|
+
import { addCases, caseTags, deniedBy, GUARD_CHECK, KNOWLEDGE_DIR, loadConfig, loadProduct, OWASP_2025, parseSecurity, planPath, PRODUCT_FILE, readPlan, recordFirstRun, recordSecurityChecks, recordTests, SECURITY_FILE, setSecurityChecks, testTitle, writeCoverageFile, writePlan } from "@omniqa/core";
|
|
5
|
+
import { posix } from "./devices.js";
|
|
6
|
+
import { requestRoute } from "@omniqa/web";
|
|
7
|
+
import { checkNamed, CHECKS, turnedDown } from "./security-checks.js";
|
|
8
|
+
import { ANTI_FORGERY } from "./security-request.js";
|
|
9
|
+
import { clearFinding, REPORT_FILE, setFinding } from "./security-report.js";
|
|
10
|
+
import { runCase, setCaseStatus, suiteFile, writeBlock } from "./writer.js";
|
|
11
|
+
/** The pages a guard walks at most. */
|
|
12
|
+
const GUARD_PAGES = 15;
|
|
13
|
+
/** One guard per actor the pass walked as: the pages it asked for, walked again, and no finding beyond the accepted ones. A signed-in guard mutates, as every case that signs in. */
|
|
14
|
+
const guards = (k, ctx) => k.actors.flatMap((actor) => {
|
|
15
|
+
const signedIn = actor !== "guest";
|
|
16
|
+
if (signedIn && ctx.product.accounts[actor] === undefined) {
|
|
17
|
+
ctx.leave(`the guard as ${actor} is left out: ${PRODUCT_FILE} has no account ${actor}, so no test signs in as it`);
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
if (signedIn && ctx.environment?.mutations === false) {
|
|
21
|
+
ctx.leave(`the guard as ${actor} is left out: it signs in, and ${k.environment} is read-only (mutations: false in ${PRODUCT_FILE})`);
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
// ponytail: a page with an id in its path is left out of the walk (a concrete id would be a value in a committed test), and so is one whose path echoed an account's value; a seeds list if a product's findings live on such pages.
|
|
25
|
+
const routes = (k.pages[actor] ?? []).filter((route) => !route.includes("{") && !route.includes("[redacted]")).slice(0, GUARD_PAGES);
|
|
26
|
+
return [{
|
|
27
|
+
addition: {
|
|
28
|
+
suite: "sec-passive",
|
|
29
|
+
suiteTitle: "What browsing reveals (OWASP A02, A03, A04, A07)",
|
|
30
|
+
title: `${signedIn ? `A member signed in as ${actor}` : "A guest"} browsing ${ctx.product.name} raises no new security finding`,
|
|
31
|
+
type: "security",
|
|
32
|
+
tiers: ["production"],
|
|
33
|
+
priority: "P0",
|
|
34
|
+
mutating: signedIn,
|
|
35
|
+
...(signedIn ? { note: `account ${actor}` } : {}),
|
|
36
|
+
knowledge: [`${SECURITY_FILE} › Findings`],
|
|
37
|
+
routes: routes.length === 0 ? ["/"] : routes,
|
|
38
|
+
},
|
|
39
|
+
check: { check: GUARD_CHECK, actor, request: "", with: "" },
|
|
40
|
+
}];
|
|
41
|
+
});
|
|
42
|
+
/** The suite of a check's cases, by its OWASP category. */
|
|
43
|
+
const SUITES = {
|
|
44
|
+
A01: { suite: "sec-access", suiteTitle: "Access control (OWASP A01)" },
|
|
45
|
+
A02: { suite: "sec-config", suiteTitle: "Misconfiguration (OWASP A02)" },
|
|
46
|
+
A04: { suite: "sec-crypto", suiteTitle: "Cryptographic failures (OWASP A04)" },
|
|
47
|
+
A05: { suite: "sec-injection", suiteTitle: "Injection (OWASP A05)" },
|
|
48
|
+
A07: { suite: "sec-auth", suiteTitle: "Authentication (OWASP A07)" },
|
|
49
|
+
A10: { suite: "sec-errors", suiteTitle: "Exceptional conditions (OWASP A10)" },
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* The cases that ask the sign-in's own fields each in turn (sql, template, header, errors on the request that signs in):
|
|
53
|
+
* a list of their own, since they belong where the sign-in's other checks are, after the ten wrong passwords. Each sends
|
|
54
|
+
* a handful of sign-ins that fail, and a product that slows an address down for those must not meet them before a test
|
|
55
|
+
* that still has to sign in.
|
|
56
|
+
*/
|
|
57
|
+
const SIGN_IN_FIELDS = "the sign-in's fields";
|
|
58
|
+
/**
|
|
59
|
+
* The order the checks are planned in, a group after the other and within one a check of each kind before a second of
|
|
60
|
+
* any: access control, injection and exposed files first (P0), authentication next (P1), the rest after (P2). The ten
|
|
61
|
+
* wrong passwords come after everything else that signs in: a product that slows this machine down after them slows
|
|
62
|
+
* only what asks the sign-in's own fields, which is planned last of that suite (SIGN_IN_FIELDS). The start page over plain http is the last of its suite too: a port that drops what is sent to it keeps
|
|
63
|
+
* the proxy's repeater busy for a while (security-checks.ts).
|
|
64
|
+
*/
|
|
65
|
+
const GROUPS = [
|
|
66
|
+
{ priority: "P0", checks: ["no-session", "other-account", "lower-role", "csrf", "redirect", "traversal", "reflection", "sql", "template", "header", "exposed-files"] },
|
|
67
|
+
{ priority: "P1", checks: ["after-logout", "fixation", "enumeration", "default-credentials"] },
|
|
68
|
+
{ priority: "P1", checks: ["throttle"] },
|
|
69
|
+
{ priority: "P1", checks: [SIGN_IN_FIELDS] },
|
|
70
|
+
{ priority: "P2", checks: ["origin", "token", "errors"] },
|
|
71
|
+
{ priority: "P2", checks: ["https-only"] },
|
|
72
|
+
];
|
|
73
|
+
/** The checks that ask each parameter of a request whatever it holds and whatever answers: planned on the requests with the most parameters. */
|
|
74
|
+
const EVERY_PARAMETER = ["sql", "template", "header", "errors"];
|
|
75
|
+
/** Every check that asks a request's parameters in turn: those, markup where the answer is a page, and where a parameter may lead. */
|
|
76
|
+
const SWEEPS = ["reflection", ...EVERY_PARAMETER, "redirect", "traversal"];
|
|
77
|
+
/** The requests a kind of sweep is planned on at most. */
|
|
78
|
+
const SWEPT = 6;
|
|
79
|
+
const answered = (statuses) => (statuses ?? []).some((status) => status >= 200 && status < 300);
|
|
80
|
+
/** Answered or sent on: what a route that redirects answers everyone. */
|
|
81
|
+
const went = (statuses) => (statuses ?? []).some((status) => status < 400);
|
|
82
|
+
/** Sent away every time: to the login, or with a 401, a 403 or a 404. */
|
|
83
|
+
const turnedAway = (statuses) => statuses !== undefined && statuses.length > 0 && statuses.every((status) => [301, 302, 303, 307, 308, 401, 403, 404].includes(status));
|
|
84
|
+
/** A request that writes, as the gate reads one: what a browser asks before it (OPTIONS) and a HEAD are neither a read to ask nor a write. */
|
|
85
|
+
const WRITES = /^(POST|PUT|PATCH|DELETE) /;
|
|
86
|
+
/** The request the walking account signed in with. */
|
|
87
|
+
const isSignIn = (row) => row.carries.includes("credentials");
|
|
88
|
+
const hasSession = (row) => row.carries.includes("cookie") || row.carries.includes("authorization");
|
|
89
|
+
const named = (name) => CHECKS.find((check) => check.name === name);
|
|
90
|
+
/** A route a test can open by its address, and a request it can name: no id (a concrete one would be a value in a committed test), nothing redacted. */
|
|
91
|
+
const literal = (route) => !route.includes("{") && !route.includes("[redacted]");
|
|
92
|
+
/** The parameters of a request a test can name and a sweep asks: one written by its shape (`{n}`, `{value}`), one that echoed an account's value and one named like an anti-forgery token are not. */
|
|
93
|
+
export const askable = (parameters) => parameters.filter((name) => literal(name) && !ANTI_FORGERY.test(name));
|
|
94
|
+
/** This machine, where a browser asks for no https and nothing travels a network. */
|
|
95
|
+
const loopback = (host) => host === "localhost" || host.endsWith(".localhost") || /^127(\.\d+){3}$/.test(host) || host === "[::1]";
|
|
96
|
+
/**
|
|
97
|
+
* The basic checks, each on the requests of security.md that call for it. Those of access control and authentication sign
|
|
98
|
+
* in, so none is planned without an account a test can sign in as, or where the environment is read-only. Those of
|
|
99
|
+
* injection, configuration and failure only read: a request a guest is answered is asked as a guest, on a read-only
|
|
100
|
+
* environment too, and the site is asked once. What cannot be planned is said in one line with what it needs: a plan lists
|
|
101
|
+
* only what a test can prove.
|
|
102
|
+
*/
|
|
103
|
+
const basicChecks = (k, ctx) => {
|
|
104
|
+
const deny = ctx.environment?.deny ?? [];
|
|
105
|
+
const walked = k.actors.filter((actor) => actor !== "guest" && ctx.product.accounts[actor] !== undefined);
|
|
106
|
+
/** The accounts a test can sign in as: of product.yaml, walked, where a test may change state (signing in does). */
|
|
107
|
+
const accounts = ctx.environment?.mutations === false ? [] : walked;
|
|
108
|
+
const documents = new Set(Object.values(k.pages).flat());
|
|
109
|
+
const isPage = (row) => row.request.startsWith("GET ") && documents.has(row.request.slice(4));
|
|
110
|
+
/**
|
|
111
|
+
* A request an account is answered and a guest is not: the guest was seen turned away, or, for a request that is no page
|
|
112
|
+
* and carries a session, never seen asking. A page nobody was turned away from is left alone: a single-page product serves
|
|
113
|
+
* every visitor the same page and keeps what matters behind its API.
|
|
114
|
+
*/
|
|
115
|
+
const membersOnly = (row, actor) => answered(row.statuses[actor]) && !isSignIn(row) && hasSession(row) && (turnedAway(row.statuses.guest) || (row.statuses.guest === undefined && !isPage(row)));
|
|
116
|
+
/**
|
|
117
|
+
* An account the product was seen keeping from a request: turned away from the request itself, or from a page that makes
|
|
118
|
+
* it. product.yaml names roles and ties them to no account, so this is what tells a lower role: two walks that merely
|
|
119
|
+
* differ (a page one of them did not open) say nothing, and asking with either session would report what the product
|
|
120
|
+
* rightly answers.
|
|
121
|
+
*/
|
|
122
|
+
const keptFrom = (row, actor) => turnedAway(row.statuses[actor]) || row.pages.some((page) => turnedAway(k.requests.find((other) => other.request === `GET ${page}`)?.statuses[actor]));
|
|
123
|
+
/** The per-account checks the plan already has, by what security.md › Checks says: a later build that lists another request first plans no second one. */
|
|
124
|
+
const has = (check, actor) => k.checks.some((row) => row.check === check && row.actor === actor);
|
|
125
|
+
/**
|
|
126
|
+
* How a test of `actor` has the request made: the page of the actor's walk it opens, the first that makes the request or,
|
|
127
|
+
* for a request the actor was answered a page on, that page itself (another page that asks for its route, a router's
|
|
128
|
+
* script, is answered data). Where no page makes it by being opened, the form of the walk that does, with the page it is
|
|
129
|
+
* on: the test sends it, which only an environment that allows a change lets it do. A button whose text echoed an
|
|
130
|
+
* account's value is written without it, and a test could not find its form by that.
|
|
131
|
+
*/
|
|
132
|
+
const madeBy = (row, actor) => {
|
|
133
|
+
const opens = (page) => literal(page) && (k.pages[actor] ?? []).includes(page);
|
|
134
|
+
const pages = row.pages.filter(opens);
|
|
135
|
+
const page = (row.pageFor.includes(actor) ? pages.find((candidate) => row.request === `GET ${candidate}`) : undefined) ?? pages[0];
|
|
136
|
+
return page !== undefined ? { page } : ctx.environment?.mutations === false ? undefined : row.forms.find((form) => opens(form.page) && literal(form.button));
|
|
137
|
+
};
|
|
138
|
+
/** The routes a case on that request opens, or why no test can be written for it: no page a test can open makes it and no form it may send, or the deny list matches. */
|
|
139
|
+
const routesFor = (row, actor, more = []) => {
|
|
140
|
+
const made = isSignIn(row) ? { page: ctx.product.crawl.loginPath } : madeBy(row, actor);
|
|
141
|
+
if (made === undefined || row.request.includes("[redacted]"))
|
|
142
|
+
return "unopenable";
|
|
143
|
+
return deniedBy(deny, [row.request.split(" ")[1], made.page, made.button, ...more]) === undefined ? [made.page, ...more] : "denied";
|
|
144
|
+
};
|
|
145
|
+
const unopenable = new Set();
|
|
146
|
+
let denied = 0;
|
|
147
|
+
const lists = new Map([...CHECKS.map((check) => [check.name, []]), [SIGN_IN_FIELDS, []]]);
|
|
148
|
+
/** What every case of a check has: its suite by its category (the authentication suite for what asks the sign-in's fields), its priority by its place in the order. */
|
|
149
|
+
const suiteOf = (check, list = check.name) => ({ ...SUITES[list === SIGN_IN_FIELDS ? "A07" : check.category], priority: GROUPS.find((group) => group.checks.includes(list))?.priority ?? "P2", type: "security", tiers: ["production"] });
|
|
150
|
+
/** One case of `check`, unless no test can have the request made or the deny list matches it. A guest signs nobody in: such a case changes what its request changes, and what the form it sends changes, and no more. */
|
|
151
|
+
const plan = (check, actor, row, other = "", extra = {}) => {
|
|
152
|
+
const routes = routesFor(row, actor, extra.routes);
|
|
153
|
+
if (routes === "unopenable")
|
|
154
|
+
return void unopenable.add(row.request);
|
|
155
|
+
if (routes === "denied")
|
|
156
|
+
return void denied++;
|
|
157
|
+
const signedIn = actor !== "guest";
|
|
158
|
+
lists.get(extra.list ?? check.name)?.push({
|
|
159
|
+
addition: { ...suiteOf(check, extra.list), title: check.title({ request: row.request, actor, with: other, product: ctx.product.name }), mutating: signedIn || check.mutates === "always" || !row.request.startsWith("GET ") || (!isSignIn(row) && madeBy(row, actor)?.button !== undefined), ...(signedIn ? { note: other === "" ? `account ${actor}` : `accounts ${actor} and ${other}` } : {}), knowledge: [`${SECURITY_FILE} › ${extra.section ?? "Requests"}`], routes },
|
|
160
|
+
check: { check: check.name, actor, request: row.request, with: other },
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
/** A check with no case, now or from an earlier round: on a request, or on the sign-in's fields. */
|
|
164
|
+
const none = (name) => lists.get(name)?.length === 0 && !lists.get(SIGN_IN_FIELDS)?.some((planned) => planned.check.check === name) && !k.checks.some((row) => row.check === name);
|
|
165
|
+
// ---- access control and authentication: each signs in ----
|
|
166
|
+
const all = "the checks of access control and authentication are left out";
|
|
167
|
+
if (walked.length === 0)
|
|
168
|
+
ctx.leave(`${all}: each signs in, and no account of ${PRODUCT_FILE} walked`);
|
|
169
|
+
else if (accounts.length === 0)
|
|
170
|
+
ctx.leave(`${all}: each signs in, and ${k.environment} is read-only (mutations: false in ${PRODUCT_FILE})`);
|
|
171
|
+
else if (k.requests.length === 0)
|
|
172
|
+
ctx.leave(`${all}: this ${SECURITY_FILE} lists no request (omniqa knowledge build reads them off the walk)`);
|
|
173
|
+
else {
|
|
174
|
+
for (const row of k.requests) {
|
|
175
|
+
const member = accounts.find((actor) => membersOnly(row, actor));
|
|
176
|
+
if (member !== undefined)
|
|
177
|
+
plan(named("no-session"), member, row);
|
|
178
|
+
// Something of one account asked for with another's session: another account that has its own of the kind.
|
|
179
|
+
const owner = row.carries.includes("an identifier") && hasSession(row) && !answered(row.statuses.guest) && !isSignIn(row) ? accounts.find((actor) => answered(row.statuses[actor])) : undefined;
|
|
180
|
+
const second = owner === undefined ? undefined : accounts.find((actor) => actor !== owner && answered(row.statuses[actor]));
|
|
181
|
+
if (owner !== undefined && second !== undefined)
|
|
182
|
+
plan(named("other-account"), owner, row, second);
|
|
183
|
+
// What a higher account's pages ask for and a lower one's do not, asked with the lower one's session.
|
|
184
|
+
const higher = ctx.product.roles.length > 1 && !isPage(row) && !isSignIn(row) && hasSession(row) && !answered(row.statuses.guest) ? accounts.find((actor) => answered(row.statuses[actor])) : undefined;
|
|
185
|
+
const lower = higher === undefined ? undefined : accounts.find((actor) => actor !== higher && !answered(row.statuses[actor]) && keptFrom(row, actor));
|
|
186
|
+
if (higher !== undefined && lower !== undefined)
|
|
187
|
+
plan(named("lower-role"), higher, row, lower);
|
|
188
|
+
// A state-changing request of a session: one a guest is answered too (a beacon) is nothing to forge.
|
|
189
|
+
const sender = /^(POST|PUT|PATCH|DELETE) /.test(row.request) && row.carries.includes("cookie") && !isSignIn(row) && !answered(row.statuses.guest) ? accounts.find((actor) => (row.statuses[actor] ?? []).some((status) => status < 400)) : undefined;
|
|
190
|
+
if (sender !== undefined)
|
|
191
|
+
plan(named("csrf"), sender, row);
|
|
192
|
+
// ponytail: once, on the first request a guest does not get: where the token is the session every request carries it, and what checks its signature is one piece of the product. A case per service when a product has several.
|
|
193
|
+
const bearer = row.carries.includes("a JWT") && lists.get("token")?.length === 0 ? accounts.find((actor) => membersOnly(row, actor)) : undefined;
|
|
194
|
+
if (bearer !== undefined)
|
|
195
|
+
plan(named("token"), bearer, row);
|
|
196
|
+
}
|
|
197
|
+
// Per account: the session a sign-out ends, and the one a sign-in must not keep, each shown on a request a guest does not get.
|
|
198
|
+
const noSignOut = [];
|
|
199
|
+
for (const account of accounts) {
|
|
200
|
+
const row = k.requests.find((candidate) => membersOnly(candidate, account) && Array.isArray(routesFor(candidate, account)));
|
|
201
|
+
const signOut = k.sessions.find((session) => session.account === account)?.signOut;
|
|
202
|
+
if (row === undefined)
|
|
203
|
+
continue;
|
|
204
|
+
if (signOut === undefined || !literal(signOut))
|
|
205
|
+
noSignOut.push(account);
|
|
206
|
+
else if (!has("after-logout", account))
|
|
207
|
+
plan(named("after-logout"), account, row, "", { routes: [signOut], section: "Sessions" });
|
|
208
|
+
if (!has("fixation", account))
|
|
209
|
+
plan(named("fixation"), account, row, "", { section: "Sessions" });
|
|
210
|
+
}
|
|
211
|
+
// On the sign-in itself, known by the password it carried.
|
|
212
|
+
const signIn = k.requests.find((row) => isSignIn(row) && accounts.some((actor) => row.statuses[actor] !== undefined));
|
|
213
|
+
if (signIn !== undefined)
|
|
214
|
+
for (const check of CHECKS.filter((c) => c.signIn === true))
|
|
215
|
+
plan(check, accounts.find((actor) => signIn.statuses[actor] !== undefined), signIn);
|
|
216
|
+
// And its fields, each in turn: what is typed into a sign-in reaches a query as readily as any other value. The checks ask it as a sign-in
|
|
217
|
+
// that fails, for an account nobody has (security-checks.ts), so the account that signs in here is never the one tried.
|
|
218
|
+
if (signIn !== undefined)
|
|
219
|
+
for (const name of EVERY_PARAMETER)
|
|
220
|
+
if (!k.checks.some((row) => row.check === name && row.request === signIn.request))
|
|
221
|
+
plan(named(name), accounts.find((actor) => signIn.statuses[actor] !== undefined), signIn, "", { list: SIGN_IN_FIELDS });
|
|
222
|
+
// What a check with no case would need, in the order the checks are planned in. One the plan has from an earlier round needs nothing.
|
|
223
|
+
const needs = {
|
|
224
|
+
"no-session": "the pass saw no request of an account that a guest was turned away from or never made",
|
|
225
|
+
"other-account": accounts.length < 2 ? "it asks with a second account's session, and one account walked" : "the pass saw no request with an identifier in it that two accounts both made",
|
|
226
|
+
"lower-role": ctx.product.roles.length < 2 ? `it asks with a lower role's session, and ${PRODUCT_FILE} names one role (roles:)` : accounts.length < 2 ? "it asks with a lower role's session, and one account walked" : `no account was seen turned away from a request, or from the page that makes it, that another account is answered on, and ${PRODUCT_FILE} ties its roles to no account: the walks do not say which one is lower`,
|
|
227
|
+
csrf: "the pass saw no state-changing request with a session cookie that a test may send",
|
|
228
|
+
token: "no request of the pass carried a JSON Web Token",
|
|
229
|
+
fixation: "the pass saw no request of an account that a guest was turned away from or never made",
|
|
230
|
+
};
|
|
231
|
+
for (const name of ["no-session", "other-account", "lower-role", "csrf", "after-logout", "fixation", "enumeration", "token"]) {
|
|
232
|
+
if (name === "after-logout")
|
|
233
|
+
for (const account of noSignOut)
|
|
234
|
+
ctx.leave(`after-logout as ${account} is left out: the walk saw no link or button named Log out or Sign out on a page of ${account}`);
|
|
235
|
+
if (!none(name))
|
|
236
|
+
continue;
|
|
237
|
+
if (name === "enumeration")
|
|
238
|
+
ctx.leave("enumeration, throttle and default-credentials are left out: the pass saw no request that carried an account's password (the sign-in)");
|
|
239
|
+
else if (needs[name] !== undefined && !(name === "fixation" && none("no-session")))
|
|
240
|
+
ctx.leave(`${name} is left out: ${needs[name]}`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// ---- injection, configuration and failure: a request is asked as who made it, a guest's as a guest ----
|
|
244
|
+
// A file written before the pass listed requests has nothing to ask a parameter of.
|
|
245
|
+
if (k.requests.length === 0)
|
|
246
|
+
ctx.leave(`${SWEEPS.join(", ")} and origin are left out: this ${SECURITY_FILE} lists no request (omniqa knowledge build reads them off the walk)`);
|
|
247
|
+
else {
|
|
248
|
+
const got = (row, answer) => ["guest", ...accounts].filter((actor) => answer(row.statuses[actor], actor, row));
|
|
249
|
+
/** Who a test asks a request as: a guest where a guest got that answer, else an account a test can sign in as; of them, the first whose test can open a page that makes it. */
|
|
250
|
+
const asker = (row, answer) => got(row, answer).find((actor) => Array.isArray(routesFor(row, actor))) ?? got(row, answer)[0];
|
|
251
|
+
// A request with a parameter a test can name. One that writes is asked too, where a test may change state and only there: each
|
|
252
|
+
// value is that change once more, so mutations: false keeps a probe out of it. The sign-in is a list of its own (above).
|
|
253
|
+
const withParameter = k.requests.filter((row) => (row.request.startsWith("GET ") || WRITES.test(row.request)) && !isSignIn(row) && askable(row.parameters).length > 0);
|
|
254
|
+
const reads = withParameter.filter((row) => row.request.startsWith("GET ") || ctx.environment?.mutations !== false);
|
|
255
|
+
const kept = withParameter.length - reads.length;
|
|
256
|
+
if (kept > 0)
|
|
257
|
+
ctx.leave(`${plural(kept, "request")} that ${kept === 1 ? "writes is" : "write are"} left out: ${k.environment} is read-only (mutations: false in ${PRODUCT_FILE})`);
|
|
258
|
+
/** Answered, for a request that reads; answered or sent on, for one that writes: what answers a form is a redirect. */
|
|
259
|
+
const took = (statuses, _actor, row) => (row.request.startsWith("GET ") ? answered(statuses) : went(statuses));
|
|
260
|
+
/** What checks with no case would need: nothing of the kind was seen, or what was seen no test may make (the lines on signing in, on the deny list and on pages say why). */
|
|
261
|
+
const unseen = (names, what, seen, aside = "") => void (names.length > 0 && names.every(none) && ctx.leave(`${names.length === 1 ? `${names[0]} is` : `${names.slice(0, -1).join(", ")} and ${names.at(-1)} are`} left out: the pass saw no ${what}${seen === 0 ? "" : " that a test may make"}${aside}`));
|
|
262
|
+
/**
|
|
263
|
+
* The requests of `rows` a kind of sweep is planned on: the first ones a test can write and the plan has no case of that
|
|
264
|
+
* kind for, up to six that read and six that write, whatever a later build lists first (six forms' worth of fields that
|
|
265
|
+
* a page posts must not take the places of the one search that reads). One no test can write takes no place, and is
|
|
266
|
+
* counted. The sign-in's own row is another list's: it takes none either.
|
|
267
|
+
*/
|
|
268
|
+
const sweep = (name, rows, answer) => {
|
|
269
|
+
const have = k.checks.filter((row) => row.check === name && !k.requests.some((request) => isSignIn(request) && request.request === row.request)).map((row) => row.request);
|
|
270
|
+
const writable = rows.filter((row) => Array.isArray(routesFor(row, asker(row, answer))));
|
|
271
|
+
for (const writes of [false, true]) {
|
|
272
|
+
const places = Math.max(0, SWEPT - have.filter((request) => WRITES.test(request) === writes).length);
|
|
273
|
+
for (const row of writable.filter((candidate) => WRITES.test(candidate.request) === writes && !have.includes(candidate.request)).slice(0, places))
|
|
274
|
+
plan(named(name), asker(row, answer), row);
|
|
275
|
+
}
|
|
276
|
+
for (const row of rows.filter((candidate) => !writable.includes(candidate))) {
|
|
277
|
+
if (routesFor(row, asker(row, answer)) === "denied")
|
|
278
|
+
denied++;
|
|
279
|
+
else
|
|
280
|
+
unopenable.add(row.request);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
// Each parameter in turn, whatever it holds: the requests with the most parameters.
|
|
284
|
+
const wide = reads.filter((row) => asker(row, took) !== undefined).toSorted((a, b) => askable(b.parameters).length - askable(a.parameters).length);
|
|
285
|
+
for (const name of EVERY_PARAMETER)
|
|
286
|
+
sweep(name, wide, took);
|
|
287
|
+
// Markup is asked where an answer is a page: of those requests, the ones the pass saw answered one while they carried a parameter, as an actor who
|
|
288
|
+
// got that page. A route among the pages says too little: it may send this actor on, and what a script asks of it answers data, where a marker runs nothing.
|
|
289
|
+
const shown = (statuses, actor, row) => answered(statuses) && row.pageFor.includes(actor);
|
|
290
|
+
sweep("reflection", wide.filter((row) => asker(row, shown) !== undefined), shown);
|
|
291
|
+
// Nothing with a parameter is one thing left out, not three. Where the sign-in is the one request with any, the line says so, and
|
|
292
|
+
// names the checks that are left: the ones that ask its fields are planned where a test may sign in (the lines above say where not).
|
|
293
|
+
const onSignIn = k.requests.some((row) => isSignIn(row) && askable(row.parameters).length > 0);
|
|
294
|
+
unseen((reads.length === 0 ? SWEEPS : EVERY_PARAMETER).filter(none), "request with a parameter", withParameter.length, onSignIn ? ", the sign-in aside" : "");
|
|
295
|
+
if (reads.length > 0)
|
|
296
|
+
unseen(["reflection"], "page that takes a parameter", reads.filter((row) => asker(row, shown) !== undefined).length);
|
|
297
|
+
const writable = wide.filter((row) => Array.isArray(routesFor(row, asker(row, took))));
|
|
298
|
+
const over = [false, true].reduce((n, writes) => n + Math.max(0, writable.filter((row) => WRITES.test(row.request) === writes).length - SWEPT), 0);
|
|
299
|
+
if (over > 0)
|
|
300
|
+
ctx.leave(`${plural(over, "more request")} with parameters ${over === 1 ? "is" : "are"} left out: ${EVERY_PARAMETER.slice(0, -1).join(", ")} and ${EVERY_PARAMETER.at(-1)} are planned for the ${SWEPT} that read and the ${SWEPT} that write with the most`);
|
|
301
|
+
// Where a parameter may lead: a request the pass saw carry a url, a path or a file name. A route that sends everyone on is still one to ask.
|
|
302
|
+
const leading = reads.filter((row) => row.carries.includes("a url"));
|
|
303
|
+
sweep("redirect", leading.filter((row) => asker(row, went) !== undefined), went);
|
|
304
|
+
if (reads.length > 0)
|
|
305
|
+
unseen(["redirect"], "request with a url or a path in a parameter", leading.length);
|
|
306
|
+
const filed = reads.filter((row) => row.carries.includes("a file name"));
|
|
307
|
+
sweep("traversal", filed.filter((row) => asker(row, took) !== undefined), took);
|
|
308
|
+
if (reads.length > 0)
|
|
309
|
+
unseen(["traversal"], "request with a file name in a parameter", filed.length);
|
|
310
|
+
// Who may read an answer: one request of the API, a member's own where a test can sign in (what another site would read is the member's).
|
|
311
|
+
const api = k.requests.filter((row) => row.request.startsWith("GET ") && !isSignIn(row) && !isPage(row));
|
|
312
|
+
const readers = [...api.flatMap((row) => accounts.filter((actor) => membersOnly(row, actor)).slice(0, 1).map((actor) => [row, actor])), ...api.flatMap((row) => (asker(row, answered) === undefined ? [] : [[row, asker(row, answered)]]))];
|
|
313
|
+
const reader = readers.find(([row, actor]) => Array.isArray(routesFor(row, actor))) ?? readers[0];
|
|
314
|
+
// Once: a later build that lists another request first plans no second one.
|
|
315
|
+
if (reader !== undefined && !k.checks.some((row) => row.check === "origin"))
|
|
316
|
+
plan(named("origin"), reader[1], reader[0]);
|
|
317
|
+
unseen(["origin"], "request of the API that reads (a GET that is no page)", api.length);
|
|
318
|
+
}
|
|
319
|
+
// The site itself, once, as a guest, from its start page.
|
|
320
|
+
const site = (name, section) => {
|
|
321
|
+
// Once, by what security.md › Checks says: its case is named after the product, and a product is renamed.
|
|
322
|
+
if (k.checks.some((row) => row.check === name))
|
|
323
|
+
return;
|
|
324
|
+
if (deniedBy(deny, ["/"]) !== undefined)
|
|
325
|
+
return void denied++;
|
|
326
|
+
const check = named(name);
|
|
327
|
+
lists.get(name)?.push({ addition: { ...suiteOf(check), title: check.title({ request: "", actor: "guest", with: "", product: ctx.product.name }), mutating: false, knowledge: [`${SECURITY_FILE} › ${section}`], routes: ["/"] }, check: { check: name, actor: "guest", request: "", with: "" } });
|
|
328
|
+
};
|
|
329
|
+
site("exposed-files", "Pages");
|
|
330
|
+
const at = new URL(ctx.environment?.url ?? k.url);
|
|
331
|
+
if (loopback(at.hostname))
|
|
332
|
+
ctx.leave(`https-only is left out: ${k.environment} is on this machine (${at.hostname}), where a browser asks for no https`);
|
|
333
|
+
// An https environment on a port of its own: which port answers plain http for it its url does not say.
|
|
334
|
+
else if (at.protocol === "https:" && at.port !== "")
|
|
335
|
+
ctx.leave(`https-only is left out: the url of ${k.environment} names a port, and what answers plain http for it is not known`);
|
|
336
|
+
else
|
|
337
|
+
site("https-only", "TLS");
|
|
338
|
+
if (denied > 0)
|
|
339
|
+
ctx.leave(`${plural(denied, "check")} ${denied === 1 ? "is" : "are"} left out: the deny list of ${k.environment} matches their request, or the page or the form that makes it`);
|
|
340
|
+
if (unopenable.size > 0)
|
|
341
|
+
ctx.leave(`${plural(unopenable.size, "request")} ${unopenable.size === 1 ? "is" : "are"} left out: no page a test can open by its address makes ${unopenable.size === 1 ? "it" : "them"}, and no form a test may send (an id or an account's value is in the path of the request, of every page that makes it or in the button of the form that does, or only a form sends it and ${k.environment} is read-only)`);
|
|
342
|
+
// A group after the other, and in each a check of each kind before a second of any; a check no group names comes last.
|
|
343
|
+
const ordered = [...GROUPS.map((group) => group.checks), CHECKS.map((check) => check.name).filter((name) => !GROUPS.some((group) => group.checks.includes(name)))];
|
|
344
|
+
return ordered.flatMap((names) => {
|
|
345
|
+
const kinds = names.map((name) => lists.get(name) ?? []);
|
|
346
|
+
return Array.from({ length: Math.max(0, ...kinds.map((list) => list.length)) }, (_, i) => kinds.flatMap((list) => (list[i] === undefined ? [] : [list[i]]))).flat();
|
|
347
|
+
});
|
|
348
|
+
};
|
|
349
|
+
/** The guards first, then the basic checks in their order: a round takes from the front. */
|
|
350
|
+
const RULES = [guards, basicChecks];
|
|
351
|
+
/** The cases one round of planning appends at most: a review page stays readable, and the next round takes what is left. */
|
|
352
|
+
const ROUND = 24;
|
|
353
|
+
/** A case by its suite and title, as the plan tells two cases apart. */
|
|
354
|
+
const caseKey = (suite, title) => `${suite}\n${title.trim().toLowerCase()}`;
|
|
355
|
+
/**
|
|
356
|
+
* The security cases security.md calls for that the plan does not have yet, appended (no id moves; a case the plan already
|
|
357
|
+
* has, skipped ones included, is not planned twice), and their rows written into security.md › Checks. A case the plan has
|
|
358
|
+
* and the file has no row for gets its row too: omniqa write reads it, and a security.md written anew holds none.
|
|
359
|
+
*/
|
|
360
|
+
export async function planSecurity(cwd, env, o = {}) {
|
|
361
|
+
const config = await loadConfig(cwd);
|
|
362
|
+
if (!config.scope.security)
|
|
363
|
+
throw new Error("security tests are off in this project: scope: { security: true } in omniqa.config.ts turns them on");
|
|
364
|
+
const file = join(cwd, KNOWLEDGE_DIR, SECURITY_FILE);
|
|
365
|
+
const text = await readFile(file, "utf8").catch(() => undefined);
|
|
366
|
+
if (text === undefined)
|
|
367
|
+
throw new Error(`no ${posix(relative(cwd, file))} yet: omniqa knowledge build writes it (its security pass walks a production-tier environment with explore on behind Interceptor)`);
|
|
368
|
+
const planFile = planPath(join(cwd, config.testDir));
|
|
369
|
+
const plan = await readPlan(planFile);
|
|
370
|
+
if (plan === undefined)
|
|
371
|
+
throw new Error(`no plan at ${posix(relative(cwd, planFile))}; run omniqa plan first`);
|
|
372
|
+
const k = parseSecurity(text);
|
|
373
|
+
const product = await loadProduct(cwd, env);
|
|
374
|
+
// The switches of the environment the file describes decide what is planned: without them nothing can be.
|
|
375
|
+
if (product.environments[k.environment] === undefined)
|
|
376
|
+
throw new Error(`${SECURITY_FILE} describes the environment ${k.environment}, which ${PRODUCT_FILE} no longer has: omniqa knowledge build writes it again`);
|
|
377
|
+
const left = [];
|
|
378
|
+
const have = new Map(plan.suites.flatMap((s) => s.cases.map((c) => [caseKey(s.slug, c.title), c])));
|
|
379
|
+
const called = RULES.flatMap((rule) => rule(k, { product, environment: product.environments[k.environment], leave: (line) => void left.push(line) }));
|
|
380
|
+
// A case the plan has is not planned twice, and neither are two the plan would tell apart by nothing (two routes that differ by case).
|
|
381
|
+
const keys = new Set(have.keys());
|
|
382
|
+
const fresh = called.filter((p) => !keys.has(caseKey(p.addition.suite, p.addition.title)) && keys.add(caseKey(p.addition.suite, p.addition.title)) !== undefined);
|
|
383
|
+
// A build plans the security cases of a plan that has none; once it has some, the next round is the user's to ask for.
|
|
384
|
+
const round = o.firstOnly === true && [...have.values()].some((c) => c.type === "security") ? 0 : ROUND;
|
|
385
|
+
const planned = fresh.slice(0, round);
|
|
386
|
+
const waiting = fresh.length - planned.length;
|
|
387
|
+
if (waiting > 0)
|
|
388
|
+
left.push(`${plural(waiting, "more security case")} ${waiting === 1 ? "waits" : "wait"} for the next round: omniqa plan add --security plans ${waiting === 1 ? "it" : "them"}`);
|
|
389
|
+
const added = planned.length === 0 ? [] : await (async () => {
|
|
390
|
+
const appended = addCases(plan, planned.map((p) => p.addition));
|
|
391
|
+
await writePlan(planFile, appended.plan);
|
|
392
|
+
return appended.added;
|
|
393
|
+
})();
|
|
394
|
+
const rowed = new Set(k.checks.map((row) => row.case));
|
|
395
|
+
const known = called.flatMap((p) => {
|
|
396
|
+
const c = have.get(caseKey(p.addition.suite, p.addition.title));
|
|
397
|
+
return c === undefined || rowed.has(c.id) ? [] : [{ case: c.id, ...p.check }];
|
|
398
|
+
});
|
|
399
|
+
const rows = [...known, ...added.map((c, i) => ({ case: c.id, ...planned[i].check }))];
|
|
400
|
+
if (rows.length > 0)
|
|
401
|
+
await writeFile(file, setSecurityChecks(text, [...k.checks, ...rows]));
|
|
402
|
+
if (added.length > 0)
|
|
403
|
+
await writeCoverageFile(cwd).catch(() => undefined);
|
|
404
|
+
// A round that ran has considered every check this version has: what a later version brings is then new to this project.
|
|
405
|
+
if (round > 0)
|
|
406
|
+
await recordSecurityChecks(cwd, CHECKS.map((check) => check.name));
|
|
407
|
+
return { added, restored: known.length, left, planFile: posix(relative(cwd, planFile)) };
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* What a project is told after an upgrade brought checks its security suites were planned without, with the command that
|
|
411
|
+
* plans them; undefined when there is none, and for a project whose planning never recorded what it knew of.
|
|
412
|
+
*/
|
|
413
|
+
export function newChecksLine(state) {
|
|
414
|
+
const considered = state.security?.considered;
|
|
415
|
+
if (considered === undefined)
|
|
416
|
+
return undefined;
|
|
417
|
+
const fresh = CHECKS.map((check) => check.name).filter((name) => !considered.includes(name));
|
|
418
|
+
return fresh.length === 0 ? undefined : `${plural(fresh.length, "security check")} ${fresh.length === 1 ? "is" : "are"} new since this project's suites were planned (${fresh.join(", ")}): omniqa plan add --security`;
|
|
419
|
+
}
|
|
420
|
+
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
421
|
+
/** `2 security cases appended to tests/plan.md (suite sec-passive)`, or the suites with their counts when there are several. */
|
|
422
|
+
export function plannedLine(p) {
|
|
423
|
+
const bySuite = new Map();
|
|
424
|
+
for (const c of p.added)
|
|
425
|
+
bySuite.set(c.id.replace(/-\d+$/, ""), (bySuite.get(c.id.replace(/-\d+$/, "")) ?? 0) + 1);
|
|
426
|
+
const suites = bySuite.size === 1 ? `suite ${[...bySuite.keys()][0]}` : [...bySuite].map(([slug, n]) => `${slug} ${n}`).join(", ");
|
|
427
|
+
return `${plural(p.added.length, "security case")} appended to ${p.planFile} (${suites})`;
|
|
428
|
+
}
|
|
429
|
+
const q = (s) => JSON.stringify(s);
|
|
430
|
+
/** A guard as a test: the actor signed in when it is an account, the pages of the plan row visited (each left once its traffic is quiet), the findings against the accepted ones, each accepted line dated. */
|
|
431
|
+
export function renderGuard(c, actor, accepted, date) {
|
|
432
|
+
const signedIn = actor !== "guest";
|
|
433
|
+
const list = accepted.length === 0 ? "[]" : `[\n${accepted.map((line) => ` ${q(line)}, // accepted ${date}: fix it, then delete the line\n`).join("")} ]`;
|
|
434
|
+
return [
|
|
435
|
+
`test(${q(testTitle(c))}, { tag: [${caseTags(c).map(q).join(", ")}] }, async ({ ${signedIn ? "logIn, " : ""}security }) => {`,
|
|
436
|
+
...(signedIn ? [` await logIn(${q(actor)});`] : []),
|
|
437
|
+
` for (const path of [${(c.routes.length === 0 ? ["/"] : c.routes).map(q).join(", ")}]) await security.visit(path);`,
|
|
438
|
+
` expect(await security.findings({ accepted: ${list} })).toEqual([]);`,
|
|
439
|
+
"});",
|
|
440
|
+
].join("\n");
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* A basic check as a test: the account signed in (a guest signs nobody in), the page that makes the request opened, the
|
|
444
|
+
* request found, then the check; a check on the site asks from the page alone. A check that is one replay asserts what the
|
|
445
|
+
* product answered when the test was written (`observed`; without it the test only sends, which is the run that learns the
|
|
446
|
+
* answer); the others are run by name and throw by themselves. `query` are the parameters the page is opened with, each
|
|
447
|
+
* holding a placeholder: security.md keeps their names, never a value. `button` is what the button says of the form the
|
|
448
|
+
* test sends on that page, for a request that only a sent form makes.
|
|
449
|
+
*/
|
|
450
|
+
export function renderCheck(c, row, check, observed, query = [], signIn = check.signIn === true, button) {
|
|
451
|
+
const [page = "/", signOutPage = page] = c.routes;
|
|
452
|
+
const signedIn = row.actor !== "guest";
|
|
453
|
+
const found = `security.request(${q(row.request)})`;
|
|
454
|
+
const replay = check.replay;
|
|
455
|
+
const call = replay === undefined ? "" : `request.replay(${replay.without !== undefined ? `{ without: [${replay.without.map(q).join(", ")}] }` : replay.as === true ? `{ as: ${q(row.with)} }` : ""})`;
|
|
456
|
+
const steps = check.target === "site"
|
|
457
|
+
? [` await security.check(${q(check.name)});`]
|
|
458
|
+
: replay === undefined
|
|
459
|
+
? [` await (await ${found}).check(${q(check.name)});`]
|
|
460
|
+
: [
|
|
461
|
+
` const request = await ${found};`,
|
|
462
|
+
// A session that a sign-out ended: the request is the one made before it.
|
|
463
|
+
...(replay.signOut === true ? [...(signOutPage === page ? [] : [` await page.goto(${q(signOutPage)});`]), " await security.signOut();"] : []),
|
|
464
|
+
observed === undefined ? ` await ${call};` : observed.location === undefined ? ` expect((await ${call}).status).toBe(${observed.status});` : ` expect(await ${call}).toEqual({ status: ${observed.status}, location: ${q(observed.location)} });`,
|
|
465
|
+
];
|
|
466
|
+
return [
|
|
467
|
+
`test(${q(testTitle(c))}, { tag: [${caseTags(c).map(q).join(", ")}] }, async ({ ${signIn ? "" : "page, "}${signedIn ? "logIn, " : ""}security }) => {`,
|
|
468
|
+
...(signedIn ? [` await logIn(${q(row.actor)});`] : []),
|
|
469
|
+
// A check on the sign-in works on the request logIn made: there is no page to open for it.
|
|
470
|
+
...(signIn ? [] : [` await page.goto(${q(`${page}${query.length === 0 ? "" : `?${query.map((name) => `${encodeURIComponent(name)}=1`).join("&")}`}`)});`, ...(button === undefined ? [] : [` await security.submit(${q(button)});`])]),
|
|
471
|
+
...steps,
|
|
472
|
+
"});",
|
|
473
|
+
].join("\n");
|
|
474
|
+
}
|
|
475
|
+
/** Why a run of one test is not a pass: its failure's first line, the reason it was skipped, or that it left no report. */
|
|
476
|
+
function notPassed(run) {
|
|
477
|
+
if (run.report === undefined || run.summary === undefined)
|
|
478
|
+
return run.error ?? "the run left no report";
|
|
479
|
+
const failed = run.summary.failures[0]?.error;
|
|
480
|
+
if (failed !== undefined)
|
|
481
|
+
return failed;
|
|
482
|
+
if (run.summary.passed > 0)
|
|
483
|
+
return undefined;
|
|
484
|
+
const skipped = (run.report.suites ?? []).flatMap((s) => s.specs ?? []).flatMap((spec) => spec.tests ?? []).flatMap((t) => t.annotations ?? []).find((a) => a.type === "skip")?.description;
|
|
485
|
+
return skipped ?? "the test did not run";
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Write one security case by code, from its row in security.md › Checks. A guard is walked once with nothing accepted, in a run
|
|
489
|
+
* that lists what the sidecar saw instead of failing on it (OMNIQA_SECURITY_ACCEPT), then written with that list accepted and
|
|
490
|
+
* run once more: the walk is the test's own, so the test is exactly what was observed. No model is asked. A case that cannot be
|
|
491
|
+
* written (no Interceptor, a sign-in that fails, a page that does not load) leaves no draft and stays planned, with the reason.
|
|
492
|
+
*/
|
|
493
|
+
export async function writeSecurity(o) {
|
|
494
|
+
const c = o.selected.case;
|
|
495
|
+
const started = Date.now();
|
|
496
|
+
const file = suiteFile(o.testDir, o.selected.suite.slug, "security");
|
|
497
|
+
const rel = posix(relative(o.cwd, file));
|
|
498
|
+
const untouched = await readFile(file, "utf8").catch(() => undefined);
|
|
499
|
+
const result = (status, extra = {}) => ({ id: c.id, status, rounds: 1, calls: 0, by: { writer: 0, session: 0, run: 0, reviewer: 0 }, ms: Date.now() - started, ...extra });
|
|
500
|
+
const scratch = await mkdtemp(join(tmpdir(), "omniqa-accept-"));
|
|
501
|
+
try {
|
|
502
|
+
const text = await readFile(join(o.cwd, KNOWLEDGE_DIR, SECURITY_FILE), "utf8").catch(() => undefined);
|
|
503
|
+
const known = text === undefined ? undefined : parseSecurity(text);
|
|
504
|
+
// The newest row of the case: an id a removed case had may have an older one above it.
|
|
505
|
+
const check = known?.checks.findLast((x) => x.case === c.id);
|
|
506
|
+
if (check === undefined)
|
|
507
|
+
throw new Error(`${SECURITY_FILE} › Checks has no row for ${c.id}: omniqa plan add --security plans the security cases`);
|
|
508
|
+
const run = (env) => runCase(o.cwd, env, { ids: [c.id], files: [rel], envName: o.envName, project: o.project, reportName: "omniqa-write-security", log: o.log });
|
|
509
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
510
|
+
const seen = join(scratch, "seen");
|
|
511
|
+
if (check.check !== GUARD_CHECK) {
|
|
512
|
+
const basic = checkNamed(check.check);
|
|
513
|
+
if (basic === undefined)
|
|
514
|
+
throw new Error(`this version of OmniQA writes no ${check.check} check`);
|
|
515
|
+
/** The test is written and its first run passed. */
|
|
516
|
+
const written = async (what) => {
|
|
517
|
+
await setCaseStatus(planPath(o.testDir), c.id, "written", rel);
|
|
518
|
+
// A case that holds now is no finding any more.
|
|
519
|
+
await clearFinding(o.cwd, c.id);
|
|
520
|
+
const r = result("written", { file: rel });
|
|
521
|
+
await recordTests(o.cwd, { written: 1, modelCalls: 0, ms: r.ms, case: { id: c.id, status: "written", file: rel } });
|
|
522
|
+
o.out(`${c.id}: written ${rel} (${what}, 0 model calls, ${Math.round(r.ms / 100) / 10} s)`);
|
|
523
|
+
return r;
|
|
524
|
+
};
|
|
525
|
+
/**
|
|
526
|
+
* The check does not hold: a hole that was there before the test is no red test. What was found goes to the report
|
|
527
|
+
* outside git; the test, the plan and the state say that the case needs a human and where to look, and nothing else.
|
|
528
|
+
*/
|
|
529
|
+
const finding = async (sentence) => {
|
|
530
|
+
const found = o.redact(sentence);
|
|
531
|
+
const category = OWASP_2025.find((x) => x.id === basic.category);
|
|
532
|
+
await setFinding(o.cwd, { id: c.id, title: c.title, finding: found, check: basic.name, category: `${basic.category}${category === undefined ? "" : ` ${category.name}`}`, cwe: basic.cwe, environment: o.envName, date: today });
|
|
533
|
+
// A block of its own, closed on a line of its own: that line is where the next write of this case knows it ends.
|
|
534
|
+
await writeBlock(c, file, `// needs a human: see ${REPORT_FILE}\ntest.fixme(${q(testTitle(c))}, { tag: [${caseTags(c).map(q).join(", ")}] }, async () => {\n});`, "security");
|
|
535
|
+
await setCaseStatus(planPath(o.testDir), c.id, "needs-human", rel);
|
|
536
|
+
const r = result("needs-human", { file: rel, feedback: `see ${REPORT_FILE}`, finding: true });
|
|
537
|
+
await recordTests(o.cwd, { needsHuman: 1, modelCalls: 0, ms: r.ms, case: { id: c.id, status: "needs-human", file: rel, reason: `see ${REPORT_FILE}` } });
|
|
538
|
+
// Said at once, to the terminal alone: what `out` prints is also the body of the commit and the pull request a persisting run makes.
|
|
539
|
+
o.tell(`${c.id} finding: ${found}; OWASP ${basic.category}, ${basic.cwe.join(", ")}; in ${REPORT_FILE}; the case needs a human`);
|
|
540
|
+
return r;
|
|
541
|
+
};
|
|
542
|
+
// A request the case's page does not make by being opened is made by the form of that page the walk sent: the test sends it too.
|
|
543
|
+
const made = known?.requests.find((row) => row.request === check.request);
|
|
544
|
+
const signIn = basic.signIn === true || made?.carries.includes("credentials") === true;
|
|
545
|
+
const button = made === undefined || made.pages.includes(c.routes[0] ?? "/") ? undefined : made.forms.find((form) => form.page === c.routes[0])?.button;
|
|
546
|
+
// The page is opened with the parameters the pass saw it asked for with, each holding a placeholder: a check that asks each parameter
|
|
547
|
+
// needs the page's own request made with some, and what a page's script asks only when the page is given one (a search's results)
|
|
548
|
+
// is made for any check, a replay included.
|
|
549
|
+
// A page whose form is sent is opened by its address alone, as the walk opened it: with what the page was asked for once
|
|
550
|
+
// the form had gone (`?sent=1`) it may show thanks where the form was.
|
|
551
|
+
const query = button === undefined ? askable(known?.requests.find((row) => row.request === `GET ${c.routes[0]}`)?.parameters ?? []) : [];
|
|
552
|
+
if (basic.replay === undefined) {
|
|
553
|
+
// The check throws by itself on a reply that does not hold: its one run is the observation and the first run alike.
|
|
554
|
+
await writeBlock(c, file, renderCheck(c, check, basic, undefined, query, signIn, button), "security");
|
|
555
|
+
const ran = await run(o.env);
|
|
556
|
+
const first = notPassed(ran);
|
|
557
|
+
const open = /omniqa: \S+ does not hold: (.+)$/.exec(first ?? "");
|
|
558
|
+
if (open !== null)
|
|
559
|
+
return await finding(open[1]);
|
|
560
|
+
await recordFirstRun(o.cwd, c.id, first === undefined);
|
|
561
|
+
if (first !== undefined)
|
|
562
|
+
throw new Error(first);
|
|
563
|
+
// What the check left out (a parameter past the test's requests, a path the deny list keeps it from) is said with the case: names, as security.md has them.
|
|
564
|
+
const notes = (ran.report?.suites ?? []).flatMap((suite) => suite.specs ?? []).flatMap((spec) => spec.tests ?? []).flatMap((t) => t.annotations ?? []).flatMap((a) => (a.type === "omniqa" && a.description !== undefined ? [o.redact(a.description.replace(/^omniqa: /, ""))] : []));
|
|
565
|
+
return await written([`${basic.name} holds`, ...notes].join("; "));
|
|
566
|
+
}
|
|
567
|
+
// One replay: sent once to learn what the product answers, then written with that answer and run as written.
|
|
568
|
+
await writeBlock(c, file, renderCheck(c, check, basic, undefined, query, signIn, button), "security");
|
|
569
|
+
const learned = notPassed(await run({ ...o.env, OMNIQA_SECURITY_ACCEPT: seen }));
|
|
570
|
+
if (learned !== undefined)
|
|
571
|
+
throw new Error(learned);
|
|
572
|
+
const answer = /^replay (\d{3})(?: (\S+))?$/m.exec((await readFile(seen, "utf8").catch(() => "")).trim().split("\n").at(-1) ?? "");
|
|
573
|
+
if (answer === null)
|
|
574
|
+
throw new Error("the check's run re-sent nothing");
|
|
575
|
+
const status = Number(answer[1]);
|
|
576
|
+
const location = answer[2];
|
|
577
|
+
const loginPath = (await loadProduct(o.cwd, o.env)).crawl.loginPath;
|
|
578
|
+
// Where a redirect points is said and asserted when it is a name: a path with an id in it, or one that echoes an account's value, is a value in a committed test.
|
|
579
|
+
const isName = location !== undefined && requestRoute(URL.parse(location)?.pathname ?? location) === (URL.parse(location)?.pathname ?? location) && o.redact(location) === location;
|
|
580
|
+
if (!turnedDown(check.request.split(" ")[0], { status, ...(location === undefined ? {} : { location }) }, loginPath))
|
|
581
|
+
return await finding(basic.open({ request: check.request, actor: check.actor, with: check.with, answer: `${status}${isName ? ` to ${location}` : ""}` }));
|
|
582
|
+
await writeBlock(c, file, renderCheck(c, check, basic, { status, ...(isName ? { location } : {}) }, query, signIn, button), "security");
|
|
583
|
+
const first = notPassed(await run(o.env));
|
|
584
|
+
await recordFirstRun(o.cwd, c.id, first === undefined);
|
|
585
|
+
if (first !== undefined)
|
|
586
|
+
throw new Error(first);
|
|
587
|
+
return await written(`${basic.name}: refused with ${status}${location === undefined ? "" : ` to ${isName ? location : "a page"}`}`);
|
|
588
|
+
}
|
|
589
|
+
await writeBlock(c, file, renderGuard(c, check.actor, [], today), "security");
|
|
590
|
+
const walk = notPassed(await run({ ...o.env, OMNIQA_SECURITY_ACCEPT: seen }));
|
|
591
|
+
if (walk !== undefined)
|
|
592
|
+
throw new Error(walk);
|
|
593
|
+
const accepted = [...new Set((await readFile(seen, "utf8").catch(() => "")).split("\n").filter((line) => line !== ""))];
|
|
594
|
+
// The accepted list is committed with the test: a subject that echoes an account's value cannot be written into it.
|
|
595
|
+
const echoed = accepted.find((line) => o.redact(line) !== line);
|
|
596
|
+
if (echoed !== undefined)
|
|
597
|
+
throw new Error(`the finding ${echoed.split(" ")[0]} names something that holds an account's value, which a committed test must not: fix it in the product, or write this guard by hand`);
|
|
598
|
+
await writeBlock(c, file, renderGuard(c, check.actor, accepted, today), "security");
|
|
599
|
+
const first = notPassed(await run(o.env));
|
|
600
|
+
await recordFirstRun(o.cwd, c.id, first === undefined);
|
|
601
|
+
if (first !== undefined)
|
|
602
|
+
throw new Error(first);
|
|
603
|
+
await setCaseStatus(planPath(o.testDir), c.id, "written", rel);
|
|
604
|
+
const r = result("written", { file: rel });
|
|
605
|
+
await recordTests(o.cwd, { written: 1, modelCalls: 0, ms: r.ms, case: { id: c.id, status: "written", file: rel } });
|
|
606
|
+
o.out(`${c.id}: written ${rel} (${plural(accepted.length, "accepted finding")}, 0 model calls, ${Math.round(r.ms / 100) / 10} s)`);
|
|
607
|
+
return r;
|
|
608
|
+
}
|
|
609
|
+
catch (err) {
|
|
610
|
+
// The reason is printed, kept in state.json and may reach a pull request: it is redacted as the writer's reasons are.
|
|
611
|
+
const why = o.redact(err.message.split("\n")[0] || "failed");
|
|
612
|
+
if (untouched === undefined)
|
|
613
|
+
await rm(file, { force: true });
|
|
614
|
+
else
|
|
615
|
+
await writeFile(file, untouched);
|
|
616
|
+
await recordTests(o.cwd, { case: { id: c.id, status: "failed", reason: why } });
|
|
617
|
+
o.out(`${c.id}: failed: ${why}`);
|
|
618
|
+
return result("failed", { feedback: why });
|
|
619
|
+
}
|
|
620
|
+
finally {
|
|
621
|
+
await rm(scratch, { recursive: true, force: true });
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
//# sourceMappingURL=security.js.map
|