@fabrika/check 0.0.0-stage → 0.4.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/package.json +48 -5
- package/src/ast.ts +268 -0
- package/src/bun-plugin.ts +63 -0
- package/src/check.ts +183 -0
- package/src/cli.ts +88 -0
- package/src/contract.ts +254 -0
- package/src/endpoint-fixtures.ts +221 -0
- package/src/finding.ts +88 -0
- package/src/graph.ts +206 -0
- package/src/index.ts +15 -0
- package/src/inputs.ts +88 -0
- package/src/jsonc.ts +63 -0
- package/src/layout.ts +115 -0
- package/src/limits.ts +15 -0
- package/src/packed.ts +66 -0
- package/src/reader/problems.ts +11 -0
- package/src/reader/schema.ts +551 -0
- package/src/reader/surface.ts +1087 -0
- package/src/reader/tables.ts +116 -0
- package/src/reader/value.ts +644 -0
- package/src/references.ts +95 -0
- package/src/resolution.ts +178 -0
- package/src/rules/contract.ts +78 -0
- package/src/rules/crontab.ts +147 -0
- package/src/rules/framework.ts +177 -0
- package/src/rules/identity.ts +80 -0
- package/src/rules/isolation.ts +79 -0
- package/src/rules/json.ts +28 -0
- package/src/rules/literal.ts +736 -0
- package/src/rules/migrations.ts +87 -0
- package/src/rules/modules.ts +226 -0
- package/src/rules/packages.ts +168 -0
- package/src/rules/runtime.ts +507 -0
- package/src/rules/structure.ts +299 -0
- package/src/rules/tests.ts +208 -0
- package/src/rules/types.ts +62 -0
- package/src/scaffold-sample.ts +108 -0
- package/src/scaffold.ts +605 -0
- package/src/scope.ts +108 -0
- package/src/slice.ts +339 -0
- package/src/toml.ts +159 -0
- package/src/trust.ts +189 -0
- package/src/version.ts +4 -0
- package/README.md +0 -3
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { type AstNode, child, children, type ParsedModule, staticKey, walk } from '../ast'
|
|
2
|
+
import { type Finding, reporter } from '../finding'
|
|
3
|
+
import { type Located, type ModuleGraph, propertyValue } from '../graph'
|
|
4
|
+
|
|
5
|
+
const IDENTITY_FIELDS = new Set([
|
|
6
|
+
'userId',
|
|
7
|
+
'ownerId',
|
|
8
|
+
'createdBy',
|
|
9
|
+
'tenantId',
|
|
10
|
+
'workspaceId',
|
|
11
|
+
'user_id',
|
|
12
|
+
'owner_id',
|
|
13
|
+
'created_by',
|
|
14
|
+
'tenant_id',
|
|
15
|
+
'workspace_id',
|
|
16
|
+
])
|
|
17
|
+
/** The contract builders whose options carry an `input`. */
|
|
18
|
+
const ENDPOINT_BUILDERS = new Set(['query', 'mutation', 'form', 'webhook'])
|
|
19
|
+
const SHAPE_METHODS = new Set(['object', 'strictObject', 'looseObject', 'extend', 'safeExtend'])
|
|
20
|
+
const MERGE_METHODS = new Set(['merge', 'and'])
|
|
21
|
+
|
|
22
|
+
/** The top-level field names of a zod object schema, through consts, imports, `.extend()` and `.merge()`. */
|
|
23
|
+
const schemaFields = (located: Located, graph: ModuleGraph, depth = 0): ReadonlySet<string> => {
|
|
24
|
+
const fields = new Set<string>()
|
|
25
|
+
const value = depth > 16 ? undefined : graph.resolveValue(located)
|
|
26
|
+
const callee = value?.node.type === 'CallExpression' ? child(value.node, 'callee') : undefined
|
|
27
|
+
if (value === undefined || callee?.type !== 'MemberExpression') return fields
|
|
28
|
+
const add = (from: Located) => {
|
|
29
|
+
for (const field of schemaFields(from, graph, depth + 1)) fields.add(field)
|
|
30
|
+
}
|
|
31
|
+
const receiver = child(callee, 'object')
|
|
32
|
+
if (receiver !== undefined) add({ module: value.module, node: receiver })
|
|
33
|
+
const method = staticKey(callee, 'property')
|
|
34
|
+
const argument = children(value.node, 'arguments')[0]
|
|
35
|
+
if (method === undefined || !argument) return fields
|
|
36
|
+
if (SHAPE_METHODS.has(method)) {
|
|
37
|
+
const shape = graph.resolveValue({ module: value.module, node: argument })
|
|
38
|
+
for (const property of shape?.node.type === 'ObjectExpression' ? children(shape.node, 'properties') : []) {
|
|
39
|
+
const key = property === null ? undefined : staticKey(property, 'key')
|
|
40
|
+
if (key !== undefined) fields.add(key)
|
|
41
|
+
}
|
|
42
|
+
} else if (MERGE_METHODS.has(method)) add({ module: value.module, node: argument })
|
|
43
|
+
return fields
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The field an `in`/`on` access form takes from the input; the framework removes it and checks the caller's access to it. */
|
|
47
|
+
const checkedField = (access: Located | undefined, graph: ModuleGraph): string | undefined => {
|
|
48
|
+
const value = access === undefined ? undefined : graph.resolveValue(access)
|
|
49
|
+
if (value === undefined || (propertyValue(value, 'in') === undefined && propertyValue(value, 'on') === undefined)) return undefined
|
|
50
|
+
const from = propertyValue(value, 'from')
|
|
51
|
+
return from === undefined ? undefined : graph.resolveString(from)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Endpoint inputs that carry the caller's identity or tenant, outside the `in`/`on` form that checks it. */
|
|
55
|
+
export const checkClientIdentity = (contract: ParsedModule, graph: ModuleGraph): Finding[] => {
|
|
56
|
+
const findings: Finding[] = []
|
|
57
|
+
const report = reporter(contract.file, findings)
|
|
58
|
+
walk(contract.program, (node) => {
|
|
59
|
+
const callee = node.type === 'CallExpression' ? child(node, 'callee') : undefined
|
|
60
|
+
if (callee?.type !== 'MemberExpression' || !ENDPOINT_BUILDERS.has(staticKey(callee, 'property') ?? '')) return
|
|
61
|
+
for (const argument of children(node, 'arguments')) {
|
|
62
|
+
if (argument === null) continue
|
|
63
|
+
const options = graph.resolveValue({ module: contract, node: argument })
|
|
64
|
+
const input = options === undefined ? undefined : propertyValue(options, 'input')
|
|
65
|
+
if (options === undefined || input === undefined) continue
|
|
66
|
+
const checked = checkedField(propertyValue(options, 'access'), graph)
|
|
67
|
+
const at: AstNode = input.module === contract ? input.node : argument
|
|
68
|
+
for (const field of schemaFields(input, graph)) {
|
|
69
|
+
if (!IDENTITY_FIELDS.has(field) || field === checked) continue
|
|
70
|
+
report(
|
|
71
|
+
at,
|
|
72
|
+
'client-identity',
|
|
73
|
+
`input field '${field}' trusts the client to say who it is or which tenant it acts in`,
|
|
74
|
+
`access { action, in: '<dimension>', from: '${field}' } or { action, on: … }, or the value from ctx`,
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
return findings
|
|
80
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ContractError } from '@fabrika/app/contract'
|
|
2
|
+
import { ContractSourceError } from '../contract'
|
|
3
|
+
import type { Finding } from '../finding'
|
|
4
|
+
import { CONTRACT_FILE } from '../layout'
|
|
5
|
+
import { type GeneratedTest, generatedTests, missingEndpointFixtures, scaffoldInputs } from '../scaffold'
|
|
6
|
+
import { GENERATED_TEST_HEADER, generatedTestHeader } from './tests'
|
|
7
|
+
|
|
8
|
+
const RESCAFFOLD = 'the files fabrika db test-scaffold writes for the current contract and src/db/tables.ts; run it and commit the result'
|
|
9
|
+
|
|
10
|
+
const at = (file: string, message: string): Finding => ({
|
|
11
|
+
file,
|
|
12
|
+
line: 1,
|
|
13
|
+
column: 1,
|
|
14
|
+
severity: 'refusal',
|
|
15
|
+
rule: 'generated-test-hash',
|
|
16
|
+
message,
|
|
17
|
+
allowed: RESCAFFOLD,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
/** Whether the first line is a generated-test header whose hash fails: `checkGeneratedTest` reports that file already. */
|
|
21
|
+
const failsItsHash = async (text: string): Promise<boolean> => {
|
|
22
|
+
if (!text.startsWith(GENERATED_TEST_HEADER)) return false
|
|
23
|
+
const newline = text.indexOf('\n')
|
|
24
|
+
return newline === -1 || text.slice(0, newline) !== (await generatedTestHeader(text.slice(newline + 1)))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The tests the scaffold writes for these files, or the findings that stop it from reading them. */
|
|
28
|
+
export const expectedGeneratedTests = async (
|
|
29
|
+
files: ReadonlyMap<string, string>,
|
|
30
|
+
): Promise<
|
|
31
|
+
| { readonly ok: true; readonly tests: ReadonlyMap<string, GeneratedTest>; readonly missing: readonly Finding[] }
|
|
32
|
+
| { readonly ok: false; readonly findings: readonly Finding[] }
|
|
33
|
+
> => {
|
|
34
|
+
try {
|
|
35
|
+
const inputs = scaffoldInputs(files)
|
|
36
|
+
return { ok: true, tests: await generatedTests(inputs.policies, inputs.artifact, inputs.fixtures), missing: missingEndpointFixtures(inputs) }
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (error instanceof ContractSourceError) return { ok: false, findings: error.findings }
|
|
39
|
+
if (error instanceof ContractError) {
|
|
40
|
+
return {
|
|
41
|
+
ok: false,
|
|
42
|
+
findings: error.problems.map((problem): Finding => ({
|
|
43
|
+
file: CONTRACT_FILE,
|
|
44
|
+
line: 1,
|
|
45
|
+
column: 1,
|
|
46
|
+
severity: 'refusal',
|
|
47
|
+
rule: 'contract',
|
|
48
|
+
message: problem,
|
|
49
|
+
allowed: 'a contract that compiles against src/db/tables.ts',
|
|
50
|
+
})),
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
throw error
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Every generated test is byte for byte what the scaffold writes from the current contract and policies: a missing,
|
|
59
|
+
* edited or replaced one is a finding, header or not, and so is a generated test the scaffold no longer writes.
|
|
60
|
+
* `readable` is false while the literal or trust rules report the sources; otherwise what the reader refuses is
|
|
61
|
+
* reported here, so a source it cannot read does not silence the check.
|
|
62
|
+
*/
|
|
63
|
+
export const checkGeneratedTests = async (files: ReadonlyMap<string, string>, readable: boolean): Promise<Finding[]> => {
|
|
64
|
+
if (!readable) return []
|
|
65
|
+
const expected = await expectedGeneratedTests(files)
|
|
66
|
+
if (!expected.ok) return [...expected.findings]
|
|
67
|
+
const findings: Finding[] = [...expected.missing]
|
|
68
|
+
for (const [path, { text }] of expected.tests) {
|
|
69
|
+
const present = files.get(path)
|
|
70
|
+
if (present === undefined) findings.push(at(path, 'this generated test is missing'))
|
|
71
|
+
else if (present !== text && !(await failsItsHash(present))) findings.push(at(path, 'this file is not the generated test the scaffold writes here'))
|
|
72
|
+
}
|
|
73
|
+
for (const [path, text] of files) {
|
|
74
|
+
if (text.startsWith(GENERATED_TEST_HEADER) && !expected.tests.has(path)) {
|
|
75
|
+
findings.push(at(path, 'a generated test the scaffold no longer writes'))
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return findings
|
|
79
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Report } from '../finding'
|
|
2
|
+
|
|
3
|
+
const STRING_TOKEN = /"(?:[^"\\\n]|\\.)*"/g
|
|
4
|
+
|
|
5
|
+
const decode = (token: string): string | undefined => {
|
|
6
|
+
try {
|
|
7
|
+
const value: unknown = JSON.parse(token)
|
|
8
|
+
return typeof value === 'string' ? value : undefined
|
|
9
|
+
} catch {
|
|
10
|
+
return undefined
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** JSON can be imported as a module, so its strings fall under the same credential rule as source literals. */
|
|
15
|
+
export const checkJsonStrings = (text: string, report: Report): void => {
|
|
16
|
+
for (const match of text.matchAll(STRING_TOKEN)) {
|
|
17
|
+
if (!decode(match[0])?.includes('px_')) continue
|
|
18
|
+
const before = text.slice(0, match.index)
|
|
19
|
+
const line = before.split('\n').length
|
|
20
|
+
const column = match.index - before.lastIndexOf('\n')
|
|
21
|
+
report(
|
|
22
|
+
{ line, column },
|
|
23
|
+
'key-literal',
|
|
24
|
+
`'px_' in a JSON string: a key or a session cookie name in source`,
|
|
25
|
+
'the credential the framework supplies; keys never appear in files',
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
}
|