@cosmicdrift/kumiko-guards 0.413.1 → 0.415.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/README.md +15 -9
- package/package.json +4 -4
- package/src/_lib/agent-docs-api-nodes.ts +9 -2
- package/src/_lib/agent-docs-api-render.ts +31 -10
- package/src/_lib/agent-docs-api.ts +13 -9
- package/src/_lib/agent-docs-feature-imports.ts +12 -1
- package/src/_lib/baseline-compare.ts +1 -2
- package/src/_lib/doc-status.ts +8 -4
- package/src/_lib/dockerfile.ts +28 -5
- package/src/_lib/escaped-literal.ts +26 -0
- package/src/_lib/npm-registry.ts +2 -2
- package/src/_lib/run-slots.ts +4 -6
- package/src/_lib/security-audit.ts +22 -6
- package/src/_lib/test-es-raw-writes.ts +5 -3
- package/src/changes.json +51 -0
- package/src/check-security.ts +4 -3
- package/src/guard-app-dockerfile.ts +29 -12
- package/src/guard-ignore-tag-reason.ts +18 -4
- package/src/guard-internal-subpath-imports.ts +35 -25
- package/src/guard-rls-session-settings.ts +48 -6
- package/src/guard-schema-cli-features.ts +22 -1
- package/src/guard-security-plugin-suppression.ts +368 -0
- package/src/guard-test-es-row-writes.ts +2 -1
- package/src/guard-unsafe-array-param.ts +4 -7
- package/src/pre-push.sh +46 -6
- package/src/run-guards.ts +2 -28
- package/src/run-repo-checks.ts +0 -6
- package/src/run-steps-parallel.ts +4 -4
- package/src/run-ui-guards.ts +1 -15
- package/src/_lib/german-test-title.ts +0 -94
- package/src/_lib/primitives-access.ts +0 -23
- package/src/check-predicates.ts +0 -244
- package/src/guard-admin-api.ts +0 -138
- package/src/guard-direct-fetch.ts +0 -161
- package/src/guard-error-reasons.ts +0 -210
- package/src/guard-event-store-writes.ts +0 -124
- package/src/guard-i18n-locale-terminology.ts +0 -134
- package/src/guard-no-custom-primitives.ts +0 -193
- package/src/guard-no-date-api.ts +0 -183
- package/src/guard-no-direct-fs.ts +0 -346
- package/src/guard-no-direct-process-env.ts +0 -127
- package/src/guard-no-inline-styles.ts +0 -64
- package/src/guard-no-raw-hooks.ts +0 -80
- package/src/guard-no-temporal-polyfill-import.ts +0 -131
- package/src/guard-primitives-discipline.ts +0 -357
- package/src/guard-raw-classname.ts +0 -117
- package/src/guard-raw-interactive-elements.ts +0 -164
- package/src/guard-restricted-symbols.ts +0 -155
- package/src/guard-silent-skip.ts +0 -186
- package/src/guard-test-es-fixtures.ts +0 -85
- package/src/guard-test-title.ts +0 -159
- package/src/guard-thin-wrappers.ts +0 -471
- package/src/guard-unsafe-json-parse.ts +0 -87
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
// Design comes from widgets/theme tokens, not raw Tailwind in app code.
|
|
3
|
-
// Flags design-carrying classes in app `className`: default-palette colors
|
|
4
|
-
// (bg-red-500), arbitrary color values (bg-[#fff], text-[rgb(...)]), shadow-*.
|
|
5
|
-
// Layout utilities (flex/grid/gap/p-/m-/w-) and token classes
|
|
6
|
-
// (bg-primary, text-status-ok, …) stay allowed.
|
|
7
|
-
//
|
|
8
|
-
// Part of App-Mounting 2.0. Enabled per app repo in the
|
|
9
|
-
// respective migration PR (ui-guards input in _app-test.yml).
|
|
10
|
-
|
|
11
|
-
import { type JsxAttribute, type SourceFile, SyntaxKind } from "ts-morph";
|
|
12
|
-
import {
|
|
13
|
-
type AstGuard,
|
|
14
|
-
type GuardViolation,
|
|
15
|
-
relativeToCwd,
|
|
16
|
-
runStandalone,
|
|
17
|
-
type ScanSpec,
|
|
18
|
-
} from "./_lib/guard-kit";
|
|
19
|
-
import { hasIgnoreTag } from "./_lib/ignore-tag";
|
|
20
|
-
|
|
21
|
-
const SCAN: ScanSpec = {
|
|
22
|
-
scope: "source",
|
|
23
|
-
extensions: ["tsx"],
|
|
24
|
-
frameworkWithin: ["packages/bundled-features/src/**"],
|
|
25
|
-
};
|
|
26
|
-
const EXCLUDE = /(__tests__|\.test\.tsx?$|\.integration\.tsx?$|\.d\.ts$)/;
|
|
27
|
-
const IGNORE_TAG = "kumiko-lint-ignore raw-classname";
|
|
28
|
-
|
|
29
|
-
const PALETTE =
|
|
30
|
-
"red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose|slate|gray|zinc|neutral|stone";
|
|
31
|
-
const COLOR_PREFIX =
|
|
32
|
-
"bg|text|border|ring|fill|stroke|from|via|to|outline|decoration|divide|accent|caret";
|
|
33
|
-
|
|
34
|
-
const PALETTE_CLASS = new RegExp(`^(${COLOR_PREFIX})-(${PALETTE})-\\d+(/\\d+)?$`);
|
|
35
|
-
const ARBITRARY_COLOR = new RegExp(`^(${COLOR_PREFIX})-\\[(#|rgb|hsl|oklch|color-mix)`);
|
|
36
|
-
// Only shadows — `rounded-*` was included initially, but the evidence
|
|
37
|
-
// across studio/publicstatus/money-horse shows: radius hits are
|
|
38
|
-
// chips/pills/small surfaces, not design drift (that comes via
|
|
39
|
-
// colors/shadows). 41 of 44 mh hits were rounded noise.
|
|
40
|
-
const CHROME_CLASS = /^shadow(-.+)?$/;
|
|
41
|
-
|
|
42
|
-
// Strip modifier prefixes (hover:, dark:, sm:, group-open:, …) — the base
|
|
43
|
-
// class is what gets checked.
|
|
44
|
-
function baseClass(token: string): string {
|
|
45
|
-
const idx = token.lastIndexOf(":");
|
|
46
|
-
return idx === -1 ? token : token.slice(idx + 1);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function offendingTokens(text: string): string[] {
|
|
50
|
-
return text
|
|
51
|
-
.split(/\s+/)
|
|
52
|
-
.filter((t) => t.length > 0)
|
|
53
|
-
.map(baseClass)
|
|
54
|
-
.filter((t) => PALETTE_CLASS.test(t) || ARBITRARY_COLOR.test(t) || CHROME_CLASS.test(t));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function classNameStrings(attr: JsxAttribute): { text: string; line: number }[] {
|
|
58
|
-
const init = attr.getInitializer();
|
|
59
|
-
if (init === undefined) return [];
|
|
60
|
-
if (init.getKind() === SyntaxKind.StringLiteral) {
|
|
61
|
-
return [
|
|
62
|
-
{
|
|
63
|
-
text: init.asKindOrThrow(SyntaxKind.StringLiteral).getLiteralText(),
|
|
64
|
-
line: init.getStartLineNumber(),
|
|
65
|
-
},
|
|
66
|
-
];
|
|
67
|
-
}
|
|
68
|
-
// Expression: collect every string/template component (covers
|
|
69
|
-
// cn("…", cond && "…") and template literals).
|
|
70
|
-
const parts: { text: string; line: number }[] = [];
|
|
71
|
-
for (const s of init.getDescendantsOfKind(SyntaxKind.StringLiteral)) {
|
|
72
|
-
parts.push({ text: s.getLiteralText(), line: s.getStartLineNumber() });
|
|
73
|
-
}
|
|
74
|
-
for (const s of init.getDescendantsOfKind(SyntaxKind.NoSubstitutionTemplateLiteral)) {
|
|
75
|
-
parts.push({ text: s.getLiteralText(), line: s.getStartLineNumber() });
|
|
76
|
-
}
|
|
77
|
-
for (const kind of [
|
|
78
|
-
SyntaxKind.TemplateHead,
|
|
79
|
-
SyntaxKind.TemplateMiddle,
|
|
80
|
-
SyntaxKind.TemplateTail,
|
|
81
|
-
]) {
|
|
82
|
-
for (const s of init.getDescendantsOfKind(kind)) {
|
|
83
|
-
parts.push({ text: s.getText().replace(/^[`}]|[`$]{?$/g, ""), line: s.getStartLineNumber() });
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return parts;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export const guard: AstGuard = {
|
|
90
|
-
name: "Raw-ClassName Guard (App-Repos)",
|
|
91
|
-
scan: SCAN,
|
|
92
|
-
hint:
|
|
93
|
-
"Design-bearing classes belong in widgets/theme tokens (@cosmicdrift/kumiko-renderer/web widgets/, --color-status-*). " +
|
|
94
|
-
`Justified exception: // ${IGNORE_TAG} <reason>`,
|
|
95
|
-
run(files: readonly SourceFile[]) {
|
|
96
|
-
const violations: GuardViolation[] = [];
|
|
97
|
-
for (const sf of files) {
|
|
98
|
-
if (EXCLUDE.test(sf.getFilePath())) continue;
|
|
99
|
-
for (const attr of sf.getDescendantsOfKind(SyntaxKind.JsxAttribute)) {
|
|
100
|
-
if (attr.getNameNode().getText() !== "className") continue;
|
|
101
|
-
if (hasIgnoreTag(attr, IGNORE_TAG)) continue;
|
|
102
|
-
for (const part of classNameStrings(attr)) {
|
|
103
|
-
const bad = offendingTokens(part.text);
|
|
104
|
-
if (bad.length === 0) continue;
|
|
105
|
-
violations.push({
|
|
106
|
-
file: relativeToCwd(sf.getFilePath()),
|
|
107
|
-
line: part.line,
|
|
108
|
-
message: `design-bearing Tailwind classes in app code: ${bad.join(", ")}`,
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return { violations };
|
|
114
|
-
},
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
if (import.meta.main) runStandalone(guard);
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
/**
|
|
3
|
-
* Guard: raw interactive HTML elements in feature UI. <a>, <details> and
|
|
4
|
-
* <summary> each have a framework replacement (usePrimitives().Link,
|
|
5
|
-
* CollapsibleSection from @cosmicdrift/kumiko-renderer/web) but nothing was
|
|
6
|
-
* checking for them — turn-cards.tsx/agent-layer.tsx (raw <details>/<summary>)
|
|
7
|
-
* and agent-settings.tsx (raw <a href>) shipped past CI in kumiko-enterprise.
|
|
8
|
-
*
|
|
9
|
-
* <button>/<input>/<select>/<textarea>/<dialog>/<form> are deliberately NOT
|
|
10
|
-
* in this guard: <button>/<input>/<select>/<textarea>/<dialog> are already
|
|
11
|
-
* enforced by guard-primitives-discipline.ts (its own CI step, not this
|
|
12
|
-
* bundle), and <form> is a documented exception (agent-layer.tsx:71
|
|
13
|
-
* uses a native <form onSubmit> on purpose) — adding either back here would
|
|
14
|
-
* duplicate or contradict an existing rule.
|
|
15
|
-
*
|
|
16
|
-
* Only fires when the file demonstrably has primitives access (same gate as
|
|
17
|
-
* guard-no-custom-primitives's raw-form-html rule) — proof the replacement
|
|
18
|
-
* was reachable, not just a tag ban on files outside the primitives world.
|
|
19
|
-
*
|
|
20
|
-
* Baseline-regression guard like guard-tailwind-scan-surface.ts: pins the
|
|
21
|
-
* currently-known violation count per file. Reductions are allowed but
|
|
22
|
-
* don't auto-update the baseline. Without a baseline file the guard stays
|
|
23
|
-
* warning-only (bootstrap: run `--write-baseline` once).
|
|
24
|
-
*
|
|
25
|
-
* Usage:
|
|
26
|
-
* bun guards/guard-raw-interactive-elements.ts # compare against baseline
|
|
27
|
-
* bun guards/guard-raw-interactive-elements.ts --write-baseline # (re)write the baseline
|
|
28
|
-
* bun guards/guard-raw-interactive-elements.ts --no-baseline # skip the comparison
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
import * as path from "node:path";
|
|
32
|
-
import { type SourceFile, SyntaxKind } from "ts-morph";
|
|
33
|
-
import {
|
|
34
|
-
type AstGuard,
|
|
35
|
-
baselineRatchet,
|
|
36
|
-
buildSharedProject,
|
|
37
|
-
filesForGuard,
|
|
38
|
-
type GuardOutcome,
|
|
39
|
-
runStandalone,
|
|
40
|
-
type ScanSpec,
|
|
41
|
-
} from "./_lib/guard-kit";
|
|
42
|
-
import { hasIgnoreTag } from "./_lib/ignore-tag";
|
|
43
|
-
import { hasPrimitivesAccess } from "./_lib/primitives-access";
|
|
44
|
-
|
|
45
|
-
const ROOT = process.cwd();
|
|
46
|
-
|
|
47
|
-
const SCAN: ScanSpec = {
|
|
48
|
-
scope: "source",
|
|
49
|
-
extensions: ["tsx"],
|
|
50
|
-
frameworkWithin: ["packages/bundled-features/src/**"],
|
|
51
|
-
};
|
|
52
|
-
const EXCLUDE = /(__tests__|\.test\.tsx?$|\.integration\.tsx?$|\.d\.ts$)/;
|
|
53
|
-
const IGNORE_TAG = "kumiko-lint-ignore raw-interactive-elements";
|
|
54
|
-
|
|
55
|
-
type BannedTag = "a" | "details" | "summary";
|
|
56
|
-
|
|
57
|
-
const REPLACEMENT: Readonly<Record<BannedTag, string>> = {
|
|
58
|
-
a: "usePrimitives().Link",
|
|
59
|
-
details: "CollapsibleSection (@cosmicdrift/kumiko-renderer/web)",
|
|
60
|
-
summary: "CollapsibleSection (@cosmicdrift/kumiko-renderer/web)",
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
function isBannedTag(tag: string): tag is BannedTag {
|
|
64
|
-
return Object.hasOwn(REPLACEMENT, tag);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export type Finding = {
|
|
68
|
-
readonly file: string;
|
|
69
|
-
readonly line: number;
|
|
70
|
-
readonly tag: BannedTag;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
function scan(files: readonly SourceFile[]): Finding[] {
|
|
74
|
-
const findings: Finding[] = [];
|
|
75
|
-
for (const sf of files) {
|
|
76
|
-
if (EXCLUDE.test(sf.getFilePath())) continue;
|
|
77
|
-
if (!hasPrimitivesAccess(sf)) continue;
|
|
78
|
-
const elements = [
|
|
79
|
-
...sf.getDescendantsOfKind(SyntaxKind.JsxOpeningElement),
|
|
80
|
-
...sf.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement),
|
|
81
|
-
];
|
|
82
|
-
for (const el of elements) {
|
|
83
|
-
const tag = el.getTagNameNode().getText();
|
|
84
|
-
if (!isBannedTag(tag)) continue;
|
|
85
|
-
if (hasIgnoreTag(el, IGNORE_TAG)) continue;
|
|
86
|
-
const file = path.relative(ROOT, sf.getFilePath());
|
|
87
|
-
const line = el.getStartLineNumber();
|
|
88
|
-
findings.push({ file, line, tag });
|
|
89
|
-
console.warn(
|
|
90
|
-
` [raw-interactive-elements WARN] ${file}:${line} raw <${tag}> — use ${REPLACEMENT[tag]}`,
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return findings;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export function baselineCounts(findings: readonly Finding[]): Record<string, number> {
|
|
98
|
-
const counts: Record<string, number> = {};
|
|
99
|
-
for (const f of findings) counts[f.file] = (counts[f.file] ?? 0) + 1;
|
|
100
|
-
return counts;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const BASELINE_FILE = ".kumiko-raw-interactive-elements-baseline.json";
|
|
104
|
-
const createBaseline = (baselineRoot: string) =>
|
|
105
|
-
baselineRatchet({
|
|
106
|
-
file: path.join(baselineRoot, BASELINE_FILE),
|
|
107
|
-
formatVersion: 1,
|
|
108
|
-
unit: "raw interactive HTML finding(s)",
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
const REMEDIATION =
|
|
112
|
-
"Framework replacement: <a> → usePrimitives().Link, <details>/<summary> → CollapsibleSection " +
|
|
113
|
-
`(@cosmicdrift/kumiko-renderer/web). Genuine exception: // ${IGNORE_TAG} <reason>`;
|
|
114
|
-
|
|
115
|
-
function analyse(
|
|
116
|
-
files: readonly SourceFile[],
|
|
117
|
-
compareBaseline: boolean,
|
|
118
|
-
baselineRoot: string,
|
|
119
|
-
): GuardOutcome {
|
|
120
|
-
const findings = scan(files);
|
|
121
|
-
if (!compareBaseline) {
|
|
122
|
-
console.log(" Baseline comparison skipped (--no-baseline).");
|
|
123
|
-
return { violations: [] };
|
|
124
|
-
}
|
|
125
|
-
const resolveLine = (file: string): number => findings.find((f) => f.file === file)?.line ?? 1;
|
|
126
|
-
return {
|
|
127
|
-
violations: createBaseline(baselineRoot).check(baselineCounts(findings), REMEDIATION, {
|
|
128
|
-
formatDriftRemediation:
|
|
129
|
-
"Run `bun guards/guard-raw-interactive-elements.ts --write-baseline` once.",
|
|
130
|
-
resolveLine,
|
|
131
|
-
}),
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export function createGuard(options: { readonly baselineRoot?: string } = {}): AstGuard {
|
|
136
|
-
const baselineRoot = options.baselineRoot ?? ROOT;
|
|
137
|
-
return {
|
|
138
|
-
name: "Raw-Interactive-Elements Guard (App-Repos)",
|
|
139
|
-
scan: SCAN,
|
|
140
|
-
hint: REMEDIATION,
|
|
141
|
-
run: (files: readonly SourceFile[]) => analyse(files, true, baselineRoot),
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export const guard: AstGuard = createGuard();
|
|
146
|
-
|
|
147
|
-
// Flags are read ONLY here, not in run() — the shared runner
|
|
148
|
-
// (run-ui-guards.ts) runs every guard with the same argv, a
|
|
149
|
-
// --write-baseline there must not silently rewrite the baseline.
|
|
150
|
-
if (import.meta.main) {
|
|
151
|
-
const args = process.argv.slice(2);
|
|
152
|
-
if (args.includes("--write-baseline")) {
|
|
153
|
-
const project = buildSharedProject([guard]);
|
|
154
|
-
const findings = scan(filesForGuard(project, guard));
|
|
155
|
-
createBaseline(ROOT).write(baselineCounts(findings));
|
|
156
|
-
process.exit(0);
|
|
157
|
-
}
|
|
158
|
-
if (args.includes("--no-baseline")) {
|
|
159
|
-
const project = buildSharedProject([guard]);
|
|
160
|
-
analyse(filesForGuard(project, guard), false, ROOT);
|
|
161
|
-
process.exit(0);
|
|
162
|
-
}
|
|
163
|
-
runStandalone(guard);
|
|
164
|
-
}
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
/**
|
|
3
|
-
* Guard: finds imports/re-exports of the unscoped stream primitives outside
|
|
4
|
-
* an allowlist.
|
|
5
|
-
*
|
|
6
|
-
* `getUnscopedAggregateStreamMaxVersion` / `getUnscopedAggregateStreamTenant`
|
|
7
|
-
* (packages/framework/src/event-store/event-store.ts) have no tenant
|
|
8
|
-
* filter — they are an existence oracle for foreign tenants. Legitimate only
|
|
9
|
-
* for seed-/system-internal code that deliberately works across tenants.
|
|
10
|
-
*
|
|
11
|
-
* Usage:
|
|
12
|
-
* bun guards/guard-restricted-symbols.ts
|
|
13
|
-
*
|
|
14
|
-
* Exit 1 on violations in non-allowlisted files, 0 when clean.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { type SourceFile, SyntaxKind } from "ts-morph";
|
|
18
|
-
import {
|
|
19
|
-
ALL_REPO_KINDS,
|
|
20
|
-
type AstGuard,
|
|
21
|
-
isAllowlisted,
|
|
22
|
-
relFromRepoRoot,
|
|
23
|
-
runStandalone,
|
|
24
|
-
type ScanSpec,
|
|
25
|
-
} from "./_lib/guard-kit";
|
|
26
|
-
import { resolveRepoRoots } from "./_lib/roots";
|
|
27
|
-
|
|
28
|
-
const SCAN: ScanSpec = {
|
|
29
|
-
kinds: ALL_REPO_KINDS,
|
|
30
|
-
scope: "source",
|
|
31
|
-
extensions: ["ts"],
|
|
32
|
-
frameworkWithin: ["packages/*/src/**", "samples/**"],
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const EXCLUDE = /(__tests__|\.test\.ts$|\.integration\.ts$|\.d\.ts$)/;
|
|
36
|
-
|
|
37
|
-
const RESTRICTED_SYMBOLS = new Set([
|
|
38
|
-
"getUnscopedAggregateStreamMaxVersion",
|
|
39
|
-
"getUnscopedAggregateStreamTenant",
|
|
40
|
-
// Appends any event type without the feature-ownership check of ctx.events.append and without
|
|
41
|
-
// an audited unsafeRaw escape hatch, so new callers need an explicit allowlist entry.
|
|
42
|
-
"appendEventInTenantDb",
|
|
43
|
-
"getStreamVersionInTenantDb",
|
|
44
|
-
// Grants the ES-write capability on the tenant being destroyed; only the destroy stage and its definition may use it.
|
|
45
|
-
"createTenantDataDestroyDb",
|
|
46
|
-
// Test seam that writes event-fed tables without events; runtime code importing it would be a back door.
|
|
47
|
-
"withExecutorManagedWritesForTests",
|
|
48
|
-
]);
|
|
49
|
-
|
|
50
|
-
const ALLOWLIST = [
|
|
51
|
-
/^packages\/framework\/src\/event-store\/event-store\.ts$/,
|
|
52
|
-
/^packages\/framework\/src\/event-store\/index\.ts$/,
|
|
53
|
-
/^packages\/framework\/src\/event-store\/tenant-db-append\.ts$/,
|
|
54
|
-
/^packages\/bundled-features\/src\/cap-counter\/cap-reservation\.ts$/,
|
|
55
|
-
// Catch-up of a missing terminal extract event in the caller tenant's own stream; JobContext has no events.unsafeAppend.
|
|
56
|
-
/^packages\/bundled-features\/src\/document-ingest-foundation\/append-missing-extract-event\.ts$/,
|
|
57
|
-
/^packages\/bundled-features\/src\/user-data-rights-defaults\/hooks\/delivery-attempt\.userdata-hook\.ts$/,
|
|
58
|
-
/^packages\/framework\/src\/db\/tenant-db\.ts$/,
|
|
59
|
-
/^packages\/framework\/src\/db\/index\.ts$/,
|
|
60
|
-
/^packages\/bundled-features\/src\/tenant-lifecycle\/stages\.ts$/,
|
|
61
|
-
/^packages\/testing\/src\/helpers\/(seed|db-cleanup)\.ts$/,
|
|
62
|
-
/^packages\/bundled-features\/src\/tenant\/seeding\.ts$/,
|
|
63
|
-
/^packages\/bundled-features\/src\/tier-engine\/feature\.ts$/,
|
|
64
|
-
];
|
|
65
|
-
|
|
66
|
-
interface Violation {
|
|
67
|
-
line: number;
|
|
68
|
-
symbol: string;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Namespace import (`import * as es from "...event-store"; es.getUnscoped...()`)
|
|
72
|
-
// doesn't bind a named import declaration, so it slips past
|
|
73
|
-
// findRestrictedSymbolReferences() below — catch `<namespace>.<restrictedSymbol>`
|
|
74
|
-
// property access instead. `export *` re-exports remain an unaddressed gap
|
|
75
|
-
// (see the guard's `hint` for the known limitation).
|
|
76
|
-
function findNamespacedSymbolAccesses(sf: SourceFile): Violation[] {
|
|
77
|
-
const violations: Violation[] = [];
|
|
78
|
-
|
|
79
|
-
const namespaceNames = new Set(
|
|
80
|
-
sf
|
|
81
|
-
.getImportDeclarations()
|
|
82
|
-
.map((d) => d.getNamespaceImport())
|
|
83
|
-
.filter((ns): ns is NonNullable<typeof ns> => ns !== undefined)
|
|
84
|
-
.map((ns) => ns.getText()),
|
|
85
|
-
);
|
|
86
|
-
if (namespaceNames.size === 0) return violations;
|
|
87
|
-
|
|
88
|
-
for (const access of sf.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression)) {
|
|
89
|
-
const objText = access.getExpression().getText();
|
|
90
|
-
const propName = access.getNameNode().getText();
|
|
91
|
-
if (namespaceNames.has(objText) && RESTRICTED_SYMBOLS.has(propName)) {
|
|
92
|
-
violations.push({ line: access.getStartLineNumber(), symbol: propName });
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return violations;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Named imports (`import { X } from ...`) and re-exports (`export { X } from
|
|
100
|
-
// ...`) both cross a module boundary — either shape counts as an unguarded
|
|
101
|
-
// reference. getName() returns the imported/exported symbol's original name,
|
|
102
|
-
// not a local alias, so `as`-renamed references are still caught.
|
|
103
|
-
function findRestrictedSymbolReferences(sf: SourceFile): Violation[] {
|
|
104
|
-
const violations: Violation[] = [];
|
|
105
|
-
|
|
106
|
-
for (const importDecl of sf.getImportDeclarations()) {
|
|
107
|
-
for (const named of importDecl.getNamedImports()) {
|
|
108
|
-
const name = named.getName();
|
|
109
|
-
if (RESTRICTED_SYMBOLS.has(name)) {
|
|
110
|
-
violations.push({ line: named.getStartLineNumber(), symbol: name });
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
for (const exportDecl of sf.getExportDeclarations()) {
|
|
116
|
-
if (!exportDecl.getModuleSpecifier()) continue;
|
|
117
|
-
for (const named of exportDecl.getNamedExports()) {
|
|
118
|
-
const name = named.getName();
|
|
119
|
-
if (RESTRICTED_SYMBOLS.has(name)) {
|
|
120
|
-
violations.push({ line: named.getStartLineNumber(), symbol: name });
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
violations.push(...findNamespacedSymbolAccesses(sf));
|
|
126
|
-
|
|
127
|
-
return violations;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export const guard: AstGuard = {
|
|
131
|
-
name: "Restricted-Symbols Guard",
|
|
132
|
-
scan: SCAN,
|
|
133
|
-
hint: 'getUnscopedAggregateStream{MaxVersion,Tenant} is an existence oracle for foreign tenants — only seed-/system-internal code may reference them. appendEventInTenantDb/getStreamVersionInTenantDb bypass the feature-ownership check of ctx.events.append — only framework-internal coordination code may use them. createTenantDataDestroyDb grants direct writes on event-fed tables and belongs to the tenant-destroy stage only. withExecutorManagedWritesForTests is the test-seed seam behind seedRow/deleteRows and must not reach runtime code. New caller needed? Extend the allowlist in guard-restricted-symbols.ts, with a reason. Known gap: `export * from "...event-store"` is not detected (named imports, re-exports, and namespace property access are covered).',
|
|
134
|
-
run(files, roots = resolveRepoRoots()) {
|
|
135
|
-
const violations: Array<{ file: string; line: number; message: string }> = [];
|
|
136
|
-
|
|
137
|
-
for (const sf of files) {
|
|
138
|
-
const file = sf.getFilePath();
|
|
139
|
-
const rel = relFromRepoRoot(file, roots);
|
|
140
|
-
if (EXCLUDE.test(rel)) continue;
|
|
141
|
-
if (isAllowlisted(rel, ALLOWLIST)) continue;
|
|
142
|
-
for (const v of findRestrictedSymbolReferences(sf)) {
|
|
143
|
-
violations.push({
|
|
144
|
-
file: rel,
|
|
145
|
-
line: v.line,
|
|
146
|
-
message: `[${v.symbol}] restricted event-store primitive referenced outside allowlist`,
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return { violations };
|
|
152
|
-
},
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
if (import.meta.main) runStandalone(guard);
|
package/src/guard-silent-skip.ts
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
/**
|
|
3
|
-
* Guard: finds "silent skip" spots in production code.
|
|
4
|
-
*
|
|
5
|
-
* A "silent skip" is a bare `return;` (no value) in code that skips logic
|
|
6
|
-
* unnoticed — typical in hooks, handlers, middleware.
|
|
7
|
-
*
|
|
8
|
-
* Usage:
|
|
9
|
-
* bun guards/guard-silent-skip.ts
|
|
10
|
-
*
|
|
11
|
-
* Exit 1 on violations, 0 when clean.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import * as path from "node:path";
|
|
15
|
-
import { type Node, type ReturnStatement, type SourceFile, SyntaxKind } from "ts-morph";
|
|
16
|
-
import { type AstGuard, runStandalone, type ScanSpec } from "./_lib/guard-kit";
|
|
17
|
-
|
|
18
|
-
const ROOT = process.cwd();
|
|
19
|
-
|
|
20
|
-
const SCAN: ScanSpec = {
|
|
21
|
-
scope: "source",
|
|
22
|
-
extensions: ["ts"],
|
|
23
|
-
frameworkWithin: ["packages/*/src/**"],
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const EXCLUDE = /(__tests__|\.test\.ts$|\.integration\.ts$|\.d\.ts$)/;
|
|
27
|
-
|
|
28
|
-
export interface SkipSite {
|
|
29
|
-
file: string;
|
|
30
|
-
line: number;
|
|
31
|
-
enclosingFunction: string;
|
|
32
|
-
enclosingKind: "hook" | "handler" | "middleware" | "function" | "arrow" | "method";
|
|
33
|
-
precedingText: string;
|
|
34
|
-
commentsAbove?: string;
|
|
35
|
-
snippet: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Builds the context for a bare return. Pure data extraction — no policy.
|
|
40
|
-
* The allow/deny decision happens in isAllowed().
|
|
41
|
-
*/
|
|
42
|
-
function describeSite(ret: ReturnStatement, sourceFile: SourceFile): SkipSite {
|
|
43
|
-
const enclosing = findEnclosingFunction(ret);
|
|
44
|
-
const fnName = enclosing.name;
|
|
45
|
-
const kind = classifyEnclosing(ret, enclosing.node);
|
|
46
|
-
|
|
47
|
-
const prev = ret.getPreviousSibling();
|
|
48
|
-
const precedingText = prev?.getText() ?? "";
|
|
49
|
-
|
|
50
|
-
// Collect comments from multiple possible locations:
|
|
51
|
-
// 1. Leading trivia on the return itself (`// skip:` on the line above a standalone return)
|
|
52
|
-
// 2. Leading trivia on the enclosing IfStatement (`// skip:` before `if (x) return;`)
|
|
53
|
-
// 3. Trailing comment on the previous sibling statement
|
|
54
|
-
const comments: string[] = [];
|
|
55
|
-
comments.push(...ret.getLeadingCommentRanges().map((r) => r.getText()));
|
|
56
|
-
const enclosingIf = ret.getFirstAncestorByKind(SyntaxKind.IfStatement);
|
|
57
|
-
if (enclosingIf && enclosingIf.getStartLineNumber() >= ret.getStartLineNumber() - 2) {
|
|
58
|
-
comments.push(...enclosingIf.getLeadingCommentRanges().map((r) => r.getText()));
|
|
59
|
-
}
|
|
60
|
-
if (prev?.getTrailingCommentRanges) {
|
|
61
|
-
comments.push(...prev.getTrailingCommentRanges().map((r) => r.getText()));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return {
|
|
65
|
-
file: path.relative(ROOT, sourceFile.getFilePath()),
|
|
66
|
-
line: ret.getStartLineNumber(),
|
|
67
|
-
enclosingFunction: fnName,
|
|
68
|
-
enclosingKind: kind,
|
|
69
|
-
precedingText,
|
|
70
|
-
commentsAbove: comments.join("\n"),
|
|
71
|
-
snippet: ret.getText(),
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function findEnclosingFunction(node: Node): { node: Node; name: string } {
|
|
76
|
-
let cur: Node | undefined = node.getParent();
|
|
77
|
-
while (cur) {
|
|
78
|
-
if (
|
|
79
|
-
cur.isKind(SyntaxKind.FunctionDeclaration) ||
|
|
80
|
-
cur.isKind(SyntaxKind.FunctionExpression) ||
|
|
81
|
-
cur.isKind(SyntaxKind.ArrowFunction) ||
|
|
82
|
-
cur.isKind(SyntaxKind.MethodDeclaration)
|
|
83
|
-
) {
|
|
84
|
-
return { node: cur, name: guessName(cur) };
|
|
85
|
-
}
|
|
86
|
-
cur = cur.getParent();
|
|
87
|
-
}
|
|
88
|
-
return { node, name: "<top-level>" };
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function guessName(fn: Node): string {
|
|
92
|
-
if (fn.isKind(SyntaxKind.FunctionDeclaration) || fn.isKind(SyntaxKind.MethodDeclaration)) {
|
|
93
|
-
return fn.getName() ?? "<anonymous>";
|
|
94
|
-
}
|
|
95
|
-
const parent = fn.getParent();
|
|
96
|
-
if (parent?.isKind(SyntaxKind.VariableDeclaration)) return parent.getName();
|
|
97
|
-
if (parent?.isKind(SyntaxKind.PropertyAssignment)) return parent.getName();
|
|
98
|
-
const callExpr = parent?.isKind(SyntaxKind.CallExpression) ? parent : undefined;
|
|
99
|
-
if (callExpr) return `${callExpr.getExpression().getText()}(...)`;
|
|
100
|
-
return "<anonymous>";
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function classifyEnclosing(_ret: ReturnStatement, fn: Node): SkipSite["enclosingKind"] {
|
|
104
|
-
const text = fn.getParent()?.getText() ?? "";
|
|
105
|
-
if (/\bhook\s*\(|postSave|preSave|validation/.test(text)) return "hook";
|
|
106
|
-
if (/Handler\b|writeHandler|queryHandler/.test(text)) return "handler";
|
|
107
|
-
if (/middleware|Middleware/.test(text)) return "middleware";
|
|
108
|
-
if (fn.isKind(SyntaxKind.MethodDeclaration)) return "method";
|
|
109
|
-
if (fn.isKind(SyntaxKind.ArrowFunction)) return "arrow";
|
|
110
|
-
return "function";
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// ---------------------------------------------------------------------------
|
|
114
|
-
// POLICY
|
|
115
|
-
// ---------------------------------------------------------------------------
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Policy: a bare `return;` needs one of the following markers right before it
|
|
119
|
-
* — otherwise it is flagged.
|
|
120
|
-
*
|
|
121
|
-
* (a) Log call: ctx.obs.log.debug(...), logger.debug(...), context.log?.info(...)
|
|
122
|
-
* (b) Skip comment: // skip: <reason>
|
|
123
|
-
* (c) Throw escape: preceded by a throw (the return is dead code, should go)
|
|
124
|
-
*
|
|
125
|
-
* (a) is for pipeline code with ctx in scope — the skip is visible at
|
|
126
|
-
* runtime. (b) is for utils without logger access — documentation in code.
|
|
127
|
-
* (c) catches the rare cases where TS narrowing leaves a dead return behind.
|
|
128
|
-
*/
|
|
129
|
-
function isAllowed(site: SkipSite): boolean {
|
|
130
|
-
const preceding = site.precedingText;
|
|
131
|
-
|
|
132
|
-
// (a) log call on ctx/context/opts.context or module-level logger
|
|
133
|
-
if (/\b(?:\w+\.)?(?:log|logger)\??\.(?:debug|info|warn|error|trace)\s*\(/.test(preceding)) {
|
|
134
|
-
return true;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// (b) explicit // skip: marker — check trailing comment on preceding OR leading on return
|
|
138
|
-
if (/\/\/\s*skip:/i.test(preceding)) return true;
|
|
139
|
-
if (hasLeadingSkipComment(site)) return true;
|
|
140
|
-
|
|
141
|
-
// (c) preceding is a throw
|
|
142
|
-
if (/^\s*throw\b/.test(preceding)) return true;
|
|
143
|
-
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function hasLeadingSkipComment(site: SkipSite): boolean {
|
|
148
|
-
// The ts-morph scanner captures the previous sibling as precedingText, but
|
|
149
|
-
// a `// skip:` comment directly above the return is leading trivia on the
|
|
150
|
-
// return statement itself — handled by describeSite via `commentsAbove`.
|
|
151
|
-
return /\/\/\s*skip:/i.test(site.commentsAbove ?? "");
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// ---------------------------------------------------------------------------
|
|
155
|
-
// Scanner + reporter
|
|
156
|
-
// ---------------------------------------------------------------------------
|
|
157
|
-
|
|
158
|
-
export const guard: AstGuard = {
|
|
159
|
-
name: "Silent-Skip Guard",
|
|
160
|
-
scan: SCAN,
|
|
161
|
-
hint: "A bare `return;` needs one of these before it: a log call, a `// skip: <reason>` comment, or a preceding throw.",
|
|
162
|
-
run(files) {
|
|
163
|
-
const violations: Array<{ file: string; line: number; message: string }> = [];
|
|
164
|
-
|
|
165
|
-
for (const sf of files) {
|
|
166
|
-
const file = sf.getFilePath();
|
|
167
|
-
if (EXCLUDE.test(file)) continue;
|
|
168
|
-
|
|
169
|
-
const returns = sf.getDescendantsOfKind(SyntaxKind.ReturnStatement);
|
|
170
|
-
for (const ret of returns) {
|
|
171
|
-
if (ret.getExpression()) continue; // has a value, not "silent"
|
|
172
|
-
const site = describeSite(ret, sf);
|
|
173
|
-
if (isAllowed(site)) continue;
|
|
174
|
-
violations.push({
|
|
175
|
-
file: site.file,
|
|
176
|
-
line: site.line,
|
|
177
|
-
message: `[${site.enclosingKind}] in ${site.enclosingFunction}`,
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return { violations };
|
|
183
|
-
},
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
if (import.meta.main) runStandalone(guard);
|