@pegasimed.com/pegasi-sail-connect-shared 0.1.0-dev.0 → 0.2.0-dev.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 +398 -125
- package/dist/authorization/index.d.ts +3 -0
- package/dist/authorization/index.d.ts.map +1 -0
- package/dist/authorization/index.js +3 -0
- package/dist/authorization/index.js.map +1 -0
- package/dist/authorization/internal-authorization.d.ts +105 -0
- package/dist/authorization/internal-authorization.d.ts.map +1 -0
- package/dist/authorization/internal-authorization.js +287 -0
- package/dist/authorization/internal-authorization.js.map +1 -0
- package/dist/capabilities/catalog-data.d.ts +3917 -0
- package/dist/capabilities/catalog-data.d.ts.map +1 -0
- package/dist/capabilities/catalog-data.js +4244 -0
- package/dist/capabilities/catalog-data.js.map +1 -0
- package/dist/capabilities/catalog.d.ts +38 -0
- package/dist/capabilities/catalog.d.ts.map +1 -0
- package/dist/capabilities/catalog.js +91 -0
- package/dist/capabilities/catalog.js.map +1 -0
- package/dist/capabilities/index.d.ts +8 -0
- package/dist/capabilities/index.d.ts.map +1 -0
- package/dist/capabilities/index.js +8 -0
- package/dist/capabilities/index.js.map +1 -0
- package/dist/capabilities/list.d.ts +46 -0
- package/dist/capabilities/list.d.ts.map +1 -0
- package/dist/capabilities/list.js +80 -0
- package/dist/capabilities/list.js.map +1 -0
- package/dist/capabilities/match.d.ts +42 -0
- package/dist/capabilities/match.d.ts.map +1 -0
- package/dist/capabilities/match.js +139 -0
- package/dist/capabilities/match.js.map +1 -0
- package/dist/capabilities/migration-data.d.ts +2424 -0
- package/dist/capabilities/migration-data.d.ts.map +1 -0
- package/dist/capabilities/migration-data.js +2646 -0
- package/dist/capabilities/migration-data.js.map +1 -0
- package/dist/capabilities/migration.d.ts +176 -0
- package/dist/capabilities/migration.d.ts.map +1 -0
- package/dist/capabilities/migration.js +209 -0
- package/dist/capabilities/migration.js.map +1 -0
- package/dist/capabilities/types.d.ts +124 -0
- package/dist/capabilities/types.d.ts.map +1 -0
- package/dist/capabilities/types.js +2 -0
- package/dist/capabilities/types.js.map +1 -0
- package/dist/capabilities/validate.d.ts +19 -0
- package/dist/capabilities/validate.d.ts.map +1 -0
- package/dist/capabilities/validate.js +235 -0
- package/dist/capabilities/validate.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +111 -102
- package/scripts/check-service-keys.mjs +250 -0
- package/scripts/lint-phi.mjs +266 -266
package/scripts/lint-phi.mjs
CHANGED
|
@@ -1,266 +1,266 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// PEGASI SAIL/CONNECT — anti-PHI lint check
|
|
3
|
-
//
|
|
4
|
-
// Runs as a CI gate to prevent accidental PHI/PII leakage into stdout.
|
|
5
|
-
//
|
|
6
|
-
// Detects three classes of violation:
|
|
7
|
-
//
|
|
8
|
-
// 1. Direct console.* calls (console.log/info/warn/error/debug). These
|
|
9
|
-
// bypass Pino entirely, which means the PHI_LOG_REDACT_PATHS
|
|
10
|
-
// configured on the Fastify logger doesn't apply. Forbidden in
|
|
11
|
-
// service src/ — use `request.log` or `app.log` instead.
|
|
12
|
-
//
|
|
13
|
-
// 2. Direct process.stdout.write / process.stderr.write calls. Same
|
|
14
|
-
// reason — these bypass Pino. Forbidden in service src/.
|
|
15
|
-
//
|
|
16
|
-
// 3. Pino logger configs (a `redact:` field on a logger options object)
|
|
17
|
-
// that don't pull in PHI_LOG_REDACT_PATHS from the shared library.
|
|
18
|
-
// A service authoring its own redact list is drift waiting to happen.
|
|
19
|
-
//
|
|
20
|
-
// Each violation reports file:line:column with the offending source line
|
|
21
|
-
// and a short remediation hint, then exits non-zero so CI fails the build.
|
|
22
|
-
//
|
|
23
|
-
// Override mechanism: a violation can be silenced by adding a comment
|
|
24
|
-
// // allow-phi-lint: <human-readable reason>
|
|
25
|
-
// either on the same line or directly above. This keeps overrides
|
|
26
|
-
// auditable in code review (every exception has a written justification).
|
|
27
|
-
//
|
|
28
|
-
// Usage:
|
|
29
|
-
// npx lint-phi src/ # lint a directory
|
|
30
|
-
// npx lint-phi src/ test/ # multiple roots
|
|
31
|
-
// npx lint-phi --help
|
|
32
|
-
|
|
33
|
-
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
34
|
-
import { join, relative, resolve, sep } from "node:path";
|
|
35
|
-
import { argv, exit, cwd } from "node:process";
|
|
36
|
-
|
|
37
|
-
const DEFAULT_EXTENSIONS = new Set([".ts", ".mjs", ".js", ".cjs"]);
|
|
38
|
-
const SKIP_DIRS = new Set([
|
|
39
|
-
"node_modules",
|
|
40
|
-
"dist",
|
|
41
|
-
"build",
|
|
42
|
-
"coverage",
|
|
43
|
-
".git",
|
|
44
|
-
".next",
|
|
45
|
-
".turbo"
|
|
46
|
-
]);
|
|
47
|
-
|
|
48
|
-
// Test files run as one-off processes and may legitimately need
|
|
49
|
-
// console.log for vitest debug output. We still scan them but treat
|
|
50
|
-
// console.* as warnings, not errors. The redact-config check still
|
|
51
|
-
// applies (test code shouldn't define a competing redact list).
|
|
52
|
-
const TEST_FILE_PATTERNS = [
|
|
53
|
-
/\.test\.[mc]?[jt]s$/i,
|
|
54
|
-
/\.spec\.[mc]?[jt]s$/i,
|
|
55
|
-
/[\\/]test[\\/]/i,
|
|
56
|
-
/[\\/]tests[\\/]/i,
|
|
57
|
-
/[\\/]__tests__[\\/]/i
|
|
58
|
-
];
|
|
59
|
-
|
|
60
|
-
const FORBIDDEN_CONSOLE = /\bconsole\s*\.\s*(log|info|warn|error|debug)\s*\(/g;
|
|
61
|
-
const FORBIDDEN_PROCESS_WRITE = /\bprocess\s*\.\s*(stdout|stderr)\s*\.\s*write\s*\(/g;
|
|
62
|
-
const REDACT_FIELD_DECL = /\bredact\s*:\s*\{/g;
|
|
63
|
-
const PHI_PATHS_IMPORT = /PHI_LOG_REDACT_PATHS/;
|
|
64
|
-
|
|
65
|
-
const ALLOW_COMMENT = /\/\/\s*allow-phi-lint\s*:\s*(.+)$/i;
|
|
66
|
-
|
|
67
|
-
function isTestFile(file) {
|
|
68
|
-
return TEST_FILE_PATTERNS.some((rx) => rx.test(file));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function* walk(root) {
|
|
72
|
-
let entries;
|
|
73
|
-
try {
|
|
74
|
-
entries = readdirSync(root, { withFileTypes: true });
|
|
75
|
-
} catch (err) {
|
|
76
|
-
if (err.code === "ENOENT") {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
throw err;
|
|
80
|
-
}
|
|
81
|
-
for (const entry of entries) {
|
|
82
|
-
const full = join(root, entry.name);
|
|
83
|
-
if (entry.isDirectory()) {
|
|
84
|
-
if (SKIP_DIRS.has(entry.name)) {
|
|
85
|
-
continue;
|
|
86
|
-
}
|
|
87
|
-
yield* walk(full);
|
|
88
|
-
} else if (entry.isFile()) {
|
|
89
|
-
const ext = entry.name.slice(entry.name.lastIndexOf("."));
|
|
90
|
-
if (DEFAULT_EXTENSIONS.has(ext)) {
|
|
91
|
-
yield full;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function lineColFromIndex(source, index) {
|
|
98
|
-
let line = 1;
|
|
99
|
-
let col = 1;
|
|
100
|
-
for (let i = 0; i < index; i += 1) {
|
|
101
|
-
if (source[i] === "\n") {
|
|
102
|
-
line += 1;
|
|
103
|
-
col = 1;
|
|
104
|
-
} else {
|
|
105
|
-
col += 1;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
return { line, col };
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function getLineText(source, lineNumber) {
|
|
112
|
-
const lines = source.split("\n");
|
|
113
|
-
return lines[lineNumber - 1] ?? "";
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function isAllowed(source, lineNumber) {
|
|
117
|
-
const lines = source.split("\n");
|
|
118
|
-
const same = lines[lineNumber - 1] ?? "";
|
|
119
|
-
if (ALLOW_COMMENT.test(same)) {
|
|
120
|
-
return true;
|
|
121
|
-
}
|
|
122
|
-
// Also accept the comment immediately above the offending line.
|
|
123
|
-
const above = lines[lineNumber - 2] ?? "";
|
|
124
|
-
return ALLOW_COMMENT.test(above.trimStart());
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function lintFile(filePath, displayPath) {
|
|
128
|
-
const source = readFileSync(filePath, "utf8");
|
|
129
|
-
const violations = [];
|
|
130
|
-
const isTest = isTestFile(filePath);
|
|
131
|
-
|
|
132
|
-
// Rule 1: console.*
|
|
133
|
-
for (const match of source.matchAll(FORBIDDEN_CONSOLE)) {
|
|
134
|
-
const { line, col } = lineColFromIndex(source, match.index);
|
|
135
|
-
if (isAllowed(source, line)) continue;
|
|
136
|
-
violations.push({
|
|
137
|
-
severity: isTest ? "warn" : "error",
|
|
138
|
-
rule: "no-console",
|
|
139
|
-
file: displayPath,
|
|
140
|
-
line,
|
|
141
|
-
col,
|
|
142
|
-
text: getLineText(source, line).trim(),
|
|
143
|
-
hint: "use request.log / app.log (Fastify Pino) so PHI_LOG_REDACT_PATHS applies"
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Rule 2: process.stdout/stderr.write
|
|
148
|
-
for (const match of source.matchAll(FORBIDDEN_PROCESS_WRITE)) {
|
|
149
|
-
const { line, col } = lineColFromIndex(source, match.index);
|
|
150
|
-
if (isAllowed(source, line)) continue;
|
|
151
|
-
violations.push({
|
|
152
|
-
severity: "error",
|
|
153
|
-
rule: "no-direct-write",
|
|
154
|
-
file: displayPath,
|
|
155
|
-
line,
|
|
156
|
-
col,
|
|
157
|
-
text: getLineText(source, line).trim(),
|
|
158
|
-
hint: "direct process.stdout/stderr.write bypasses Pino redact — use the Fastify logger"
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Rule 3: redact: {...} declared but PHI_LOG_REDACT_PATHS not imported
|
|
163
|
-
// anywhere in the file. We accept the broader file-level reference
|
|
164
|
-
// because a service's logger config can be split across files (e.g.
|
|
165
|
-
// logger-options.ts importing the constant, then platform.ts using it).
|
|
166
|
-
// For service-internal split, run the lint against the whole src/.
|
|
167
|
-
const hasRedactDecl = REDACT_FIELD_DECL.test(source);
|
|
168
|
-
// reset lastIndex because the regex is global
|
|
169
|
-
REDACT_FIELD_DECL.lastIndex = 0;
|
|
170
|
-
if (hasRedactDecl && !PHI_PATHS_IMPORT.test(source)) {
|
|
171
|
-
const match = source.match(REDACT_FIELD_DECL);
|
|
172
|
-
const idx = source.indexOf(match[0]);
|
|
173
|
-
const { line, col } = lineColFromIndex(source, idx);
|
|
174
|
-
if (!isAllowed(source, line)) {
|
|
175
|
-
violations.push({
|
|
176
|
-
severity: "error",
|
|
177
|
-
rule: "use-shared-redact",
|
|
178
|
-
file: displayPath,
|
|
179
|
-
line,
|
|
180
|
-
col,
|
|
181
|
-
text: getLineText(source, line).trim(),
|
|
182
|
-
hint: "import PHI_LOG_REDACT_PATHS from @pegasimed.com/pegasi-sail-connect-shared instead of authoring a local redact list"
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return violations;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function printHelp() {
|
|
191
|
-
// eslint-disable-next-line no-console -- CLI usage output
|
|
192
|
-
console.log(`Usage: lint-phi <path> [<path>...]
|
|
193
|
-
|
|
194
|
-
Scans the given paths for forbidden logging patterns that could leak PHI:
|
|
195
|
-
|
|
196
|
-
no-console console.log/info/warn/error/debug calls bypass Pino redact
|
|
197
|
-
no-direct-write process.stdout/stderr.write bypasses Pino redact
|
|
198
|
-
use-shared-redact a Pino redact: {...} declaration without importing
|
|
199
|
-
PHI_LOG_REDACT_PATHS from @pegasimed.com/pegasi-sail-connect-shared
|
|
200
|
-
|
|
201
|
-
Override a single violation by writing a justification comment:
|
|
202
|
-
|
|
203
|
-
foo(); // allow-phi-lint: bootstrap-only, runs before logger init
|
|
204
|
-
|
|
205
|
-
Test files (*.test.ts, *.spec.ts, **/test/**, **/__tests__/**) get a warning
|
|
206
|
-
for no-console rather than an error — the redact rules still error.
|
|
207
|
-
|
|
208
|
-
Exit code: 0 if no errors (warnings allowed), 1 if any error found.`);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function main() {
|
|
212
|
-
const args = argv.slice(2);
|
|
213
|
-
if (args.includes("--help") || args.includes("-h")) {
|
|
214
|
-
printHelp();
|
|
215
|
-
return 0;
|
|
216
|
-
}
|
|
217
|
-
if (args.length === 0) {
|
|
218
|
-
printHelp();
|
|
219
|
-
return 1;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const allViolations = [];
|
|
223
|
-
for (const arg of args) {
|
|
224
|
-
const root = resolve(cwd(), arg);
|
|
225
|
-
let stat;
|
|
226
|
-
try {
|
|
227
|
-
stat = statSync(root);
|
|
228
|
-
} catch (err) {
|
|
229
|
-
if (err.code === "ENOENT") {
|
|
230
|
-
// eslint-disable-next-line no-console -- CLI diagnostic
|
|
231
|
-
console.error(`lint-phi: path not found: ${arg}`);
|
|
232
|
-
return 1;
|
|
233
|
-
}
|
|
234
|
-
throw err;
|
|
235
|
-
}
|
|
236
|
-
const files = stat.isDirectory() ? Array.from(walk(root)) : [root];
|
|
237
|
-
for (const file of files) {
|
|
238
|
-
const display = relative(cwd(), file).split(sep).join("/");
|
|
239
|
-
allViolations.push(...lintFile(file, display));
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
if (allViolations.length === 0) {
|
|
244
|
-
// eslint-disable-next-line no-console -- CLI summary
|
|
245
|
-
console.log("lint-phi: 0 violations");
|
|
246
|
-
return 0;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const errors = allViolations.filter((v) => v.severity === "error");
|
|
250
|
-
const warnings = allViolations.filter((v) => v.severity === "warn");
|
|
251
|
-
|
|
252
|
-
for (const v of allViolations) {
|
|
253
|
-
const tag = v.severity === "error" ? "ERROR" : "WARN ";
|
|
254
|
-
// eslint-disable-next-line no-console -- CLI report
|
|
255
|
-
console.error(
|
|
256
|
-
`${tag} ${v.file}:${v.line}:${v.col} [${v.rule}]\n ${v.text}\n hint: ${v.hint}`
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
// eslint-disable-next-line no-console -- CLI summary
|
|
260
|
-
console.error(
|
|
261
|
-
`\nlint-phi: ${errors.length} error(s), ${warnings.length} warning(s)`
|
|
262
|
-
);
|
|
263
|
-
return errors.length > 0 ? 1 : 0;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
exit(main());
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// PEGASI SAIL/CONNECT — anti-PHI lint check
|
|
3
|
+
//
|
|
4
|
+
// Runs as a CI gate to prevent accidental PHI/PII leakage into stdout.
|
|
5
|
+
//
|
|
6
|
+
// Detects three classes of violation:
|
|
7
|
+
//
|
|
8
|
+
// 1. Direct console.* calls (console.log/info/warn/error/debug). These
|
|
9
|
+
// bypass Pino entirely, which means the PHI_LOG_REDACT_PATHS
|
|
10
|
+
// configured on the Fastify logger doesn't apply. Forbidden in
|
|
11
|
+
// service src/ — use `request.log` or `app.log` instead.
|
|
12
|
+
//
|
|
13
|
+
// 2. Direct process.stdout.write / process.stderr.write calls. Same
|
|
14
|
+
// reason — these bypass Pino. Forbidden in service src/.
|
|
15
|
+
//
|
|
16
|
+
// 3. Pino logger configs (a `redact:` field on a logger options object)
|
|
17
|
+
// that don't pull in PHI_LOG_REDACT_PATHS from the shared library.
|
|
18
|
+
// A service authoring its own redact list is drift waiting to happen.
|
|
19
|
+
//
|
|
20
|
+
// Each violation reports file:line:column with the offending source line
|
|
21
|
+
// and a short remediation hint, then exits non-zero so CI fails the build.
|
|
22
|
+
//
|
|
23
|
+
// Override mechanism: a violation can be silenced by adding a comment
|
|
24
|
+
// // allow-phi-lint: <human-readable reason>
|
|
25
|
+
// either on the same line or directly above. This keeps overrides
|
|
26
|
+
// auditable in code review (every exception has a written justification).
|
|
27
|
+
//
|
|
28
|
+
// Usage:
|
|
29
|
+
// npx lint-phi src/ # lint a directory
|
|
30
|
+
// npx lint-phi src/ test/ # multiple roots
|
|
31
|
+
// npx lint-phi --help
|
|
32
|
+
|
|
33
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
34
|
+
import { join, relative, resolve, sep } from "node:path";
|
|
35
|
+
import { argv, exit, cwd } from "node:process";
|
|
36
|
+
|
|
37
|
+
const DEFAULT_EXTENSIONS = new Set([".ts", ".mjs", ".js", ".cjs"]);
|
|
38
|
+
const SKIP_DIRS = new Set([
|
|
39
|
+
"node_modules",
|
|
40
|
+
"dist",
|
|
41
|
+
"build",
|
|
42
|
+
"coverage",
|
|
43
|
+
".git",
|
|
44
|
+
".next",
|
|
45
|
+
".turbo"
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
// Test files run as one-off processes and may legitimately need
|
|
49
|
+
// console.log for vitest debug output. We still scan them but treat
|
|
50
|
+
// console.* as warnings, not errors. The redact-config check still
|
|
51
|
+
// applies (test code shouldn't define a competing redact list).
|
|
52
|
+
const TEST_FILE_PATTERNS = [
|
|
53
|
+
/\.test\.[mc]?[jt]s$/i,
|
|
54
|
+
/\.spec\.[mc]?[jt]s$/i,
|
|
55
|
+
/[\\/]test[\\/]/i,
|
|
56
|
+
/[\\/]tests[\\/]/i,
|
|
57
|
+
/[\\/]__tests__[\\/]/i
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
const FORBIDDEN_CONSOLE = /\bconsole\s*\.\s*(log|info|warn|error|debug)\s*\(/g;
|
|
61
|
+
const FORBIDDEN_PROCESS_WRITE = /\bprocess\s*\.\s*(stdout|stderr)\s*\.\s*write\s*\(/g;
|
|
62
|
+
const REDACT_FIELD_DECL = /\bredact\s*:\s*\{/g;
|
|
63
|
+
const PHI_PATHS_IMPORT = /PHI_LOG_REDACT_PATHS/;
|
|
64
|
+
|
|
65
|
+
const ALLOW_COMMENT = /\/\/\s*allow-phi-lint\s*:\s*(.+)$/i;
|
|
66
|
+
|
|
67
|
+
function isTestFile(file) {
|
|
68
|
+
return TEST_FILE_PATTERNS.some((rx) => rx.test(file));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function* walk(root) {
|
|
72
|
+
let entries;
|
|
73
|
+
try {
|
|
74
|
+
entries = readdirSync(root, { withFileTypes: true });
|
|
75
|
+
} catch (err) {
|
|
76
|
+
if (err.code === "ENOENT") {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
throw err;
|
|
80
|
+
}
|
|
81
|
+
for (const entry of entries) {
|
|
82
|
+
const full = join(root, entry.name);
|
|
83
|
+
if (entry.isDirectory()) {
|
|
84
|
+
if (SKIP_DIRS.has(entry.name)) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
yield* walk(full);
|
|
88
|
+
} else if (entry.isFile()) {
|
|
89
|
+
const ext = entry.name.slice(entry.name.lastIndexOf("."));
|
|
90
|
+
if (DEFAULT_EXTENSIONS.has(ext)) {
|
|
91
|
+
yield full;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function lineColFromIndex(source, index) {
|
|
98
|
+
let line = 1;
|
|
99
|
+
let col = 1;
|
|
100
|
+
for (let i = 0; i < index; i += 1) {
|
|
101
|
+
if (source[i] === "\n") {
|
|
102
|
+
line += 1;
|
|
103
|
+
col = 1;
|
|
104
|
+
} else {
|
|
105
|
+
col += 1;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return { line, col };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function getLineText(source, lineNumber) {
|
|
112
|
+
const lines = source.split("\n");
|
|
113
|
+
return lines[lineNumber - 1] ?? "";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function isAllowed(source, lineNumber) {
|
|
117
|
+
const lines = source.split("\n");
|
|
118
|
+
const same = lines[lineNumber - 1] ?? "";
|
|
119
|
+
if (ALLOW_COMMENT.test(same)) {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
// Also accept the comment immediately above the offending line.
|
|
123
|
+
const above = lines[lineNumber - 2] ?? "";
|
|
124
|
+
return ALLOW_COMMENT.test(above.trimStart());
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function lintFile(filePath, displayPath) {
|
|
128
|
+
const source = readFileSync(filePath, "utf8");
|
|
129
|
+
const violations = [];
|
|
130
|
+
const isTest = isTestFile(filePath);
|
|
131
|
+
|
|
132
|
+
// Rule 1: console.*
|
|
133
|
+
for (const match of source.matchAll(FORBIDDEN_CONSOLE)) {
|
|
134
|
+
const { line, col } = lineColFromIndex(source, match.index);
|
|
135
|
+
if (isAllowed(source, line)) continue;
|
|
136
|
+
violations.push({
|
|
137
|
+
severity: isTest ? "warn" : "error",
|
|
138
|
+
rule: "no-console",
|
|
139
|
+
file: displayPath,
|
|
140
|
+
line,
|
|
141
|
+
col,
|
|
142
|
+
text: getLineText(source, line).trim(),
|
|
143
|
+
hint: "use request.log / app.log (Fastify Pino) so PHI_LOG_REDACT_PATHS applies"
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Rule 2: process.stdout/stderr.write
|
|
148
|
+
for (const match of source.matchAll(FORBIDDEN_PROCESS_WRITE)) {
|
|
149
|
+
const { line, col } = lineColFromIndex(source, match.index);
|
|
150
|
+
if (isAllowed(source, line)) continue;
|
|
151
|
+
violations.push({
|
|
152
|
+
severity: "error",
|
|
153
|
+
rule: "no-direct-write",
|
|
154
|
+
file: displayPath,
|
|
155
|
+
line,
|
|
156
|
+
col,
|
|
157
|
+
text: getLineText(source, line).trim(),
|
|
158
|
+
hint: "direct process.stdout/stderr.write bypasses Pino redact — use the Fastify logger"
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Rule 3: redact: {...} declared but PHI_LOG_REDACT_PATHS not imported
|
|
163
|
+
// anywhere in the file. We accept the broader file-level reference
|
|
164
|
+
// because a service's logger config can be split across files (e.g.
|
|
165
|
+
// logger-options.ts importing the constant, then platform.ts using it).
|
|
166
|
+
// For service-internal split, run the lint against the whole src/.
|
|
167
|
+
const hasRedactDecl = REDACT_FIELD_DECL.test(source);
|
|
168
|
+
// reset lastIndex because the regex is global
|
|
169
|
+
REDACT_FIELD_DECL.lastIndex = 0;
|
|
170
|
+
if (hasRedactDecl && !PHI_PATHS_IMPORT.test(source)) {
|
|
171
|
+
const match = source.match(REDACT_FIELD_DECL);
|
|
172
|
+
const idx = source.indexOf(match[0]);
|
|
173
|
+
const { line, col } = lineColFromIndex(source, idx);
|
|
174
|
+
if (!isAllowed(source, line)) {
|
|
175
|
+
violations.push({
|
|
176
|
+
severity: "error",
|
|
177
|
+
rule: "use-shared-redact",
|
|
178
|
+
file: displayPath,
|
|
179
|
+
line,
|
|
180
|
+
col,
|
|
181
|
+
text: getLineText(source, line).trim(),
|
|
182
|
+
hint: "import PHI_LOG_REDACT_PATHS from @pegasimed.com/pegasi-sail-connect-shared instead of authoring a local redact list"
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return violations;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function printHelp() {
|
|
191
|
+
// eslint-disable-next-line no-console -- CLI usage output
|
|
192
|
+
console.log(`Usage: lint-phi <path> [<path>...]
|
|
193
|
+
|
|
194
|
+
Scans the given paths for forbidden logging patterns that could leak PHI:
|
|
195
|
+
|
|
196
|
+
no-console console.log/info/warn/error/debug calls bypass Pino redact
|
|
197
|
+
no-direct-write process.stdout/stderr.write bypasses Pino redact
|
|
198
|
+
use-shared-redact a Pino redact: {...} declaration without importing
|
|
199
|
+
PHI_LOG_REDACT_PATHS from @pegasimed.com/pegasi-sail-connect-shared
|
|
200
|
+
|
|
201
|
+
Override a single violation by writing a justification comment:
|
|
202
|
+
|
|
203
|
+
foo(); // allow-phi-lint: bootstrap-only, runs before logger init
|
|
204
|
+
|
|
205
|
+
Test files (*.test.ts, *.spec.ts, **/test/**, **/__tests__/**) get a warning
|
|
206
|
+
for no-console rather than an error — the redact rules still error.
|
|
207
|
+
|
|
208
|
+
Exit code: 0 if no errors (warnings allowed), 1 if any error found.`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function main() {
|
|
212
|
+
const args = argv.slice(2);
|
|
213
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
214
|
+
printHelp();
|
|
215
|
+
return 0;
|
|
216
|
+
}
|
|
217
|
+
if (args.length === 0) {
|
|
218
|
+
printHelp();
|
|
219
|
+
return 1;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const allViolations = [];
|
|
223
|
+
for (const arg of args) {
|
|
224
|
+
const root = resolve(cwd(), arg);
|
|
225
|
+
let stat;
|
|
226
|
+
try {
|
|
227
|
+
stat = statSync(root);
|
|
228
|
+
} catch (err) {
|
|
229
|
+
if (err.code === "ENOENT") {
|
|
230
|
+
// eslint-disable-next-line no-console -- CLI diagnostic
|
|
231
|
+
console.error(`lint-phi: path not found: ${arg}`);
|
|
232
|
+
return 1;
|
|
233
|
+
}
|
|
234
|
+
throw err;
|
|
235
|
+
}
|
|
236
|
+
const files = stat.isDirectory() ? Array.from(walk(root)) : [root];
|
|
237
|
+
for (const file of files) {
|
|
238
|
+
const display = relative(cwd(), file).split(sep).join("/");
|
|
239
|
+
allViolations.push(...lintFile(file, display));
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (allViolations.length === 0) {
|
|
244
|
+
// eslint-disable-next-line no-console -- CLI summary
|
|
245
|
+
console.log("lint-phi: 0 violations");
|
|
246
|
+
return 0;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const errors = allViolations.filter((v) => v.severity === "error");
|
|
250
|
+
const warnings = allViolations.filter((v) => v.severity === "warn");
|
|
251
|
+
|
|
252
|
+
for (const v of allViolations) {
|
|
253
|
+
const tag = v.severity === "error" ? "ERROR" : "WARN ";
|
|
254
|
+
// eslint-disable-next-line no-console -- CLI report
|
|
255
|
+
console.error(
|
|
256
|
+
`${tag} ${v.file}:${v.line}:${v.col} [${v.rule}]\n ${v.text}\n hint: ${v.hint}`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
// eslint-disable-next-line no-console -- CLI summary
|
|
260
|
+
console.error(
|
|
261
|
+
`\nlint-phi: ${errors.length} error(s), ${warnings.length} warning(s)`
|
|
262
|
+
);
|
|
263
|
+
return errors.length > 0 ? 1 : 0;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
exit(main());
|