routeguard-cli 0.0.0-stage → 1.1.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/LICENSE +21 -0
- package/README.md +164 -2
- package/bin/cli.js +116 -0
- package/package.json +45 -4
- package/src/flow.js +217 -0
- package/src/mapper.js +714 -0
- package/src/rules/bfla.js +51 -0
- package/src/rules/bola.js +69 -0
- package/src/rules/businessFlow.js +50 -0
- package/src/rules/consumption.js +164 -0
- package/src/rules/dataExposure.js +107 -0
- package/src/rules/handler.js +159 -0
- package/src/rules/inventory.js +84 -0
- package/src/rules/massAssignment.js +99 -0
- package/src/rules/misconfig.js +205 -0
- package/src/rules/rateLimit.js +53 -0
- package/src/rules/ssrf.js +57 -0
- package/src/sarif.js +45 -0
- package/src/spec.js +43 -0
- package/src/verify.js +142 -0
package/src/verify.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Runtime check: replay routes against a running app as two real users and compare the answers.
|
|
2
|
+
// Static analysis says where a check looks missing; this says whether another user really gets in.
|
|
3
|
+
|
|
4
|
+
const WRITE = new Set(["PUT", "PATCH", "DELETE", "POST"])
|
|
5
|
+
const OK = status => status >= 200 && status < 300
|
|
6
|
+
const DENIED = status => [401, 403, 404].includes(status)
|
|
7
|
+
|
|
8
|
+
// "${JUICE_PASSWORD}" in the config reads the environment, so passwords stay out of the file
|
|
9
|
+
function expand(value) {
|
|
10
|
+
if (typeof value === "string") return value.replace(/\$\{(\w+)\}/g, (_, name) => process.env[name] ?? "")
|
|
11
|
+
if (Array.isArray(value)) return value.map(expand)
|
|
12
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, expand(v)]))
|
|
13
|
+
return value
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const pick = (object, dotted) => dotted.split(".").reduce((o, k) => o?.[k], object)
|
|
17
|
+
|
|
18
|
+
async function call(baseUrl, method, path, headers = {}, body, timeoutMs) {
|
|
19
|
+
const controller = new AbortController()
|
|
20
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
|
21
|
+
try {
|
|
22
|
+
const res = await fetch(new URL(path, baseUrl), {
|
|
23
|
+
method,
|
|
24
|
+
headers: { accept: "application/json", ...(body ? { "content-type": "application/json" } : {}), ...headers },
|
|
25
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
26
|
+
redirect: "manual",
|
|
27
|
+
signal: controller.signal
|
|
28
|
+
})
|
|
29
|
+
return { status: res.status, text: await res.text() }
|
|
30
|
+
} catch (error) {
|
|
31
|
+
return { status: 0, text: "", error: error.name === "AbortError" ? "timeout" : error.message }
|
|
32
|
+
} finally {
|
|
33
|
+
clearTimeout(timer)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// a user is fixed headers, or a login call whose answer holds the token
|
|
38
|
+
async function signIn(baseUrl, user, timeoutMs) {
|
|
39
|
+
if (!user) return null
|
|
40
|
+
if (user.headers) return user.headers
|
|
41
|
+
const { login } = user
|
|
42
|
+
const res = await call(baseUrl, login.method ?? "POST", login.path, {}, login.body, timeoutMs)
|
|
43
|
+
if (!OK(res.status)) throw new Error(`login ${login.path} answered ${res.status}`)
|
|
44
|
+
const token = pick(JSON.parse(res.text), login.token)
|
|
45
|
+
if (!token) throw new Error(`login ${login.path}: no value at "${login.token}"`)
|
|
46
|
+
return { [login.header ?? "Authorization"]: `${login.prefix ?? "Bearer "}${token}` }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// the same object, by content: ignore key order and timestamps that change between calls
|
|
50
|
+
function sameObject(a, b) {
|
|
51
|
+
if (a.text === b.text) return true
|
|
52
|
+
try {
|
|
53
|
+
const strip = value =>
|
|
54
|
+
JSON.stringify(value, (key, v) => (/^(updatedAt|createdAt|timestamp|time|date|requestId|csrf\w*)$/i.test(key) ? undefined : v))
|
|
55
|
+
return strip(JSON.parse(a.text)) === strip(JSON.parse(b.text))
|
|
56
|
+
} catch {
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function fillPath(route, params, overrides) {
|
|
62
|
+
const own = overrides[`${route.method} ${route.path}`] ?? overrides[route.path] ?? {}
|
|
63
|
+
const missing = []
|
|
64
|
+
const path = route.path.replace(/[:{](\w+)\}?(\([^)]*\))?/g, (_, name) => {
|
|
65
|
+
const value = own[name] ?? params[name]
|
|
66
|
+
if (value === undefined) missing.push(name)
|
|
67
|
+
return encodeURIComponent(value ?? "")
|
|
68
|
+
})
|
|
69
|
+
return { path, missing }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// which routes to replay: every route that takes an id (BOLA) and every route a static finding
|
|
73
|
+
// called operator-only (BFLA), reads always, writes only when asked
|
|
74
|
+
function candidates(routes, findings, allowWrites) {
|
|
75
|
+
const flagged = new Map(findings.map(f => [f.route, f.rule]))
|
|
76
|
+
const seen = new Set()
|
|
77
|
+
return routes.filter(r => {
|
|
78
|
+
const key = `${r.method} ${r.path}`
|
|
79
|
+
if (seen.has(key) || /[*<]/.test(r.path) || r.method === "ALL" || r.method === "OPTIONS" || r.method === "HEAD") return false
|
|
80
|
+
if (WRITE.has(r.method) && !allowWrites) return false
|
|
81
|
+
const takesId = /[:{]/.test(r.path)
|
|
82
|
+
const operator = ["BFLA"].includes(flagged.get(key))
|
|
83
|
+
if (!takesId && !operator) return false
|
|
84
|
+
seen.add(key)
|
|
85
|
+
return true
|
|
86
|
+
}).map(r => ({ route: r, staticRule: flagged.get(`${r.method} ${r.path}`) ?? null }))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// owner: the user who owns the ids in the config · other: a second ordinary user · anonymous: no credentials
|
|
90
|
+
export async function verify({ routes, findings, baseUrl, config, allowWrites = false, timeoutMs = 10000 }) {
|
|
91
|
+
const settings = expand(config)
|
|
92
|
+
const prefix = settings.pathPrefix ?? ""
|
|
93
|
+
const headers = {
|
|
94
|
+
owner: await signIn(baseUrl, settings.users?.owner, timeoutMs),
|
|
95
|
+
other: await signIn(baseUrl, settings.users?.other, timeoutMs)
|
|
96
|
+
}
|
|
97
|
+
if (!headers.owner || !headers.other) throw new Error("the config needs users.owner and users.other")
|
|
98
|
+
|
|
99
|
+
const results = []
|
|
100
|
+
for (const { route, staticRule } of candidates(routes, findings, allowWrites)) {
|
|
101
|
+
const { path, missing } = fillPath(route, settings.params ?? {}, settings.routes ?? {})
|
|
102
|
+
const key = `${route.method} ${route.path}`
|
|
103
|
+
if (missing.length) {
|
|
104
|
+
results.push({ route: key, staticRule, verdict: "SKIPPED", reason: `no value for :${missing.join(", :")}` })
|
|
105
|
+
continue
|
|
106
|
+
}
|
|
107
|
+
const body = settings.bodies?.[key]
|
|
108
|
+
const url = prefix + path
|
|
109
|
+
// the owner goes last for writes, so a delete by the other user is not hidden by the owner's own
|
|
110
|
+
const owner = WRITE.has(route.method) ? null : await call(baseUrl, route.method, url, headers.owner, body, timeoutMs)
|
|
111
|
+
const other = await call(baseUrl, route.method, url, headers.other, body, timeoutMs)
|
|
112
|
+
const anonymous = await call(baseUrl, route.method, url, {}, body, timeoutMs)
|
|
113
|
+
results.push({ route: key, path: url, staticRule, ...judge(route, staticRule, owner, other, anonymous), status: { owner: owner?.status, other: other.status, anonymous: anonymous.status } })
|
|
114
|
+
}
|
|
115
|
+
return results
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// CONFIRMED: someone got in who should not · PUBLIC: open to anyone, and the code never asks for a login,
|
|
119
|
+
// so it is probably meant to be public · PROTECTED: the second user was refused or got their own data
|
|
120
|
+
function judge(route, staticRule, owner, other, anonymous) {
|
|
121
|
+
if (other.error) return { verdict: "INCONCLUSIVE", reason: `request failed: ${other.error}` }
|
|
122
|
+
const takesId = /[:{]/.test(route.path)
|
|
123
|
+
|
|
124
|
+
if (OK(anonymous.status) && (!takesId || (owner && OK(owner.status) && sameObject(owner, anonymous)))) {
|
|
125
|
+
if (route.auth) return { verdict: "CONFIRMED", reason: "the code expects a login, but it answers without any credentials" }
|
|
126
|
+
if (staticRule) return { verdict: "CONFIRMED", reason: "anyone can reach it, with no credentials" }
|
|
127
|
+
return { verdict: "PUBLIC", reason: "open to anyone and the code asks for no login; confirm it is meant to be public" }
|
|
128
|
+
}
|
|
129
|
+
if (owner && !OK(owner.status)) return { verdict: "INCONCLUSIVE", reason: `the owner got ${owner.status}: check the ids and paths in the config` }
|
|
130
|
+
// readable with no credentials and the code asks for none, even if the answer varies per viewer
|
|
131
|
+
if (OK(anonymous.status) && !route.auth && !staticRule)
|
|
132
|
+
return { verdict: "PUBLIC", reason: "open to anyone and the code asks for no login; confirm it is meant to be public" }
|
|
133
|
+
if (owner && OK(other.status) && takesId && sameObject(owner, other))
|
|
134
|
+
return { verdict: "CONFIRMED", reason: "another user got the owner's data" }
|
|
135
|
+
if (!owner && OK(other.status)) return { verdict: "CONFIRMED", reason: "another user's write was accepted" }
|
|
136
|
+
if (!takesId && OK(other.status) && staticRule) return { verdict: "CONFIRMED", reason: "an ordinary user reaches this operator route" }
|
|
137
|
+
if (owner && OK(other.status)) return { verdict: "PROTECTED", reason: "another user got different data (likely their own)" }
|
|
138
|
+
// the owner got in and the other user got any 4xx: some apps refuse with 400 rather than 403
|
|
139
|
+
if (DENIED(other.status) || (owner && OK(owner.status) && other.status >= 400 && other.status < 500))
|
|
140
|
+
return { verdict: "PROTECTED", reason: `another user was refused (${other.status})` }
|
|
141
|
+
return { verdict: "INCONCLUSIVE", reason: `another user got ${other.status}` }
|
|
142
|
+
}
|