@pikku/inspector 0.12.66 → 0.12.68

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.
@@ -1,148 +0,0 @@
1
- import * as ts from 'typescript'
2
- import { findPiiPaths } from './check-pii-output.js'
3
- import { doesTypeExtendsCore } from './does-type-extend-core-type.js'
4
-
5
- export type SecretSinkViolation = {
6
- /** How the value reaches the outside world, e.g. `logger.info`. */
7
- sink: string
8
- /** Dotted path to the secret inside the argument, or `<argument>` when bare. */
9
- path: string
10
- /** Position of the offending argument, for the diagnostic message. */
11
- line: number
12
- }
13
-
14
- /**
15
- * A method that writes its arguments somewhere a secret must never land, keyed
16
- * by the core interface that owns it. `'*'` means every method on the type is a
17
- * sink, which is true of a logger.
18
- */
19
- const SINKS: Array<{ coreType: string; methods: '*' | string[] }> = [
20
- { coreType: 'Logger', methods: '*' },
21
- { coreType: 'QueueService', methods: ['add'] },
22
- { coreType: 'EmailService', methods: ['send'] },
23
- { coreType: 'WebhookService', methods: ['send'] },
24
- { coreType: 'AuditService', methods: ['audit', 'write'] },
25
- { coreType: 'AuditLog', methods: ['write'] },
26
- ]
27
-
28
- /**
29
- * Finds revealed vault secrets that flow into a sink.
30
- *
31
- * `SecretValue` is nominally typed, so the type system alone stops it reaching
32
- * any of these. `.reveal()` is the deliberate escape hatch, and it hands back
33
- * `Secret<T>` — the erasable classification brand. That brand is what this scan
34
- * follows, which means it catches the revealed value both inline
35
- * (`logger.info(s.reveal())`) and through a local binding, because the local's
36
- * inferred type carries the brand too.
37
- *
38
- * The brand is optional by design, so an explicit annotation (`const t: string =
39
- * s.reveal()`) erases it and this scan goes quiet. That is the documented limit
40
- * of a structural marker: it catches the mistake, not someone determined to
41
- * silence it.
42
- *
43
- * Sinks are matched on the receiver's resolved core interface, which means the
44
- * scan needs the services parameter to be typed. Real projects get that from
45
- * the generated `#pikku` factory; a hand-rolled file that calls the raw core
46
- * factory without annotating its services leaves the parameter `any`, and an
47
- * `any` receiver matches nothing.
48
- */
49
- export function findRevealedSecretSinks(
50
- checker: ts.TypeChecker,
51
- handler: ts.Node
52
- ): SecretSinkViolation[] {
53
- const violations: SecretSinkViolation[] = []
54
-
55
- const visit = (node: ts.Node): void => {
56
- if (
57
- ts.isCallExpression(node) &&
58
- ts.isPropertyAccessExpression(node.expression)
59
- ) {
60
- const sink = describeSink(checker, node.expression)
61
- if (sink) {
62
- for (const arg of node.arguments) {
63
- const found = findSecretExpression(checker, arg)
64
- if (found) {
65
- violations.push({
66
- sink,
67
- path: found.getText(),
68
- line:
69
- found
70
- .getSourceFile()
71
- .getLineAndCharacterOfPosition(found.getStart()).line + 1,
72
- })
73
- }
74
- }
75
- }
76
- }
77
- ts.forEachChild(node, visit)
78
- }
79
- visit(handler)
80
-
81
- return violations
82
- }
83
-
84
- /**
85
- * Finds the first expression inside a sink argument whose own type carries the
86
- * secret brand, and returns it so the diagnostic can quote it.
87
- *
88
- * This walks the argument rather than typing it whole, because an object
89
- * literal in an argument position is contextually typed by the parameter — for
90
- * a guarded sink that parameter is `Safe<M>`, and resolving the literal against
91
- * it drops the optional brand before it can be read. Each inner expression
92
- * (`token.reveal()`, or a reference to a local holding it) carries its own type
93
- * regardless of the surrounding context, so reading those is what actually
94
- * sees the secret.
95
- */
96
- function findSecretExpression(
97
- checker: ts.TypeChecker,
98
- node: ts.Node
99
- ): ts.Expression | undefined {
100
- // Descend first, so an object literal wrapping the secret reports the
101
- // expression that actually holds it (`token.reveal()`) rather than the whole
102
- // literal. Nothing inside a `SecretValue` receiver is itself branded, so a
103
- // bare `token.reveal()` still falls through to the self-check below.
104
- for (const child of node.getChildren()) {
105
- // A property name is not a value, and typing it re-resolves the property.
106
- if (ts.isPropertyAssignment(node) && child === node.name) continue
107
- const found = findSecretExpression(checker, child)
108
- if (found) return found
109
- }
110
-
111
- if (ts.isExpression(node) && !ts.isStringLiteralLike(node)) {
112
- const type = checker.getTypeAtLocation(node)
113
- const isSecret = findPiiPaths(checker, type).some(
114
- (f) => f.classification === 'secret'
115
- )
116
- if (isSecret) return node
117
- }
118
-
119
- return undefined
120
- }
121
-
122
- /**
123
- * Names the sink a call writes to, or undefined when the call is not one.
124
- * `console` is matched by identifier because it is a global with no Pikku type;
125
- * everything else is matched on its resolved core interface so a renamed
126
- * service or a decorator still counts.
127
- */
128
- function describeSink(
129
- checker: ts.TypeChecker,
130
- callee: ts.PropertyAccessExpression
131
- ): string | undefined {
132
- const method = callee.name.text
133
- const receiver = callee.expression
134
-
135
- if (ts.isIdentifier(receiver) && receiver.text === 'console') {
136
- return `console.${method}`
137
- }
138
-
139
- const receiverType = checker.getTypeAtLocation(receiver)
140
- for (const { coreType, methods } of SINKS) {
141
- if (methods !== '*' && !methods.includes(method)) continue
142
- if (doesTypeExtendsCore(receiverType, checker, new Set(), coreType)) {
143
- return `${receiver.getText()}.${method}`
144
- }
145
- }
146
-
147
- return undefined
148
- }