@skrr-ai/cli 0.1.103 → 0.1.104

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.
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Keeping a Hosted Machine preview capability out of telemetry (OSK-16174).
3
+ *
4
+ * `/api/previews/<token>/...` serves a person's machine with NO login: the token in the path is a bearer
5
+ * capability (`pvw_<22 chars>.<expiry>.<22 chars>`, idle TTL ten minutes). Anyone who can read a trace, an error
6
+ * report or a log line that carries it can open the preview, so every sink that records a URL has to run its
7
+ * strings through here first. This file is the ONE place that knows what to look for; the sinks (OpenTelemetry
8
+ * exporters, Sentry hooks, request and CSRF logs, client-log ingest) only decide WHERE to call it.
9
+ *
10
+ * WHAT IT MATCHES. The prefix `pvw_` and then every character a token is made of, however far that goes, rather
11
+ * than the exact 22.digits.22 shape. A truncated token is still part of the secret, a log line cut at 200
12
+ * characters is the ordinary way one gets truncated, and a looser match costs nothing: the marker replaces
13
+ * only what looked like a token. The token alphabet is base64url plus the two dots, or any of those characters
14
+ * written as a percent-escape, once or several times (`encodeURIComponent` of a URL that was itself in a query
15
+ * string). `packages/data-provider/specs/hostedPreview.spec.ts` pins this against the contract's token shape
16
+ * and `api/server/services/HostedPreview/__tests__/telemetryRedaction.spec.js` against tokens the issuer really
17
+ * mints, so a change to either side fails there rather than in a trace nobody reads.
18
+ *
19
+ * WHAT IT DOES NOT. A prefix that is itself percent-encoded (`%70vw_`) is not recognised: nothing in this
20
+ * repository writes one, and a scan that decoded every string first would cost every sink on every record. The
21
+ * request log's own `/[credential]` rewrite (`requestLogger.js`) covers the other credential prefixes and
22
+ * stays as it is.
23
+ *
24
+ * COST. Linear in the input. The only nested repetition is `%(?:25)*XX`, and `XX` is never `25`, so the engine
25
+ * cannot read one run of `25`s two ways; the tests feed it a megabyte of the worst shapes.
26
+ *
27
+ * NEVER THROWS. These run inside telemetry hooks, and a hook that throws takes a request or an error report
28
+ * with it. Whatever cannot be scrubbed is reported to the caller as such (`false`), and the caller drops the
29
+ * record, because a record with the token still in it is exactly what this exists to prevent.
30
+ */
31
+ /** What replaces a token. Fixed, so a trace can say a preview was involved but not which one. */
32
+ export declare const PREVIEW_TOKEN_REDACTION: "pvw_[redacted]";
33
+ /**
34
+ * Replace every preview token in a string with {@link PREVIEW_TOKEN_REDACTION}. The rest of the string is
35
+ * untouched, so a trace is still readable. Anything that is not a string comes back as it came.
36
+ */
37
+ export declare function redactPreviewTokens(value: string): string;
38
+ export declare function redactPreviewTokens<T>(value: T): T;
39
+ export interface PreviewTokenWalkLimits {
40
+ /** Objects and arrays visited before the walk gives up. Default 50 000: a Sentry transaction of a thousand spans. */
41
+ maxNodes?: number;
42
+ /** Nesting followed before the walk gives up. Default 32. */
43
+ maxDepth?: number;
44
+ }
45
+ /**
46
+ * Redact every string inside an object graph, IN PLACE (an error report is rewritten far more cheaply than it is
47
+ * copied). Returns `true` when the whole graph was scrubbed and `false` when some of it could not be: past
48
+ * the bounds, frozen, behind a getter that throws. A `false` means the value may still hold a token and must not
49
+ * be sent. Binary payloads are skipped, not walked, and Error messages and stacks are scrubbed though they are
50
+ * not enumerable.
51
+ */
52
+ export declare function redactPreviewTokensDeep(root: unknown, limits?: PreviewTokenWalkLimits): boolean;
53
+ /**
54
+ * Sentry `init` options with a redacting `beforeSend`, `beforeSendTransaction` and `beforeBreadcrumb`. Those three
55
+ * are where an event's `request.url`, `request.query_string`, `transaction`, spans, breadcrumbs, `extra` and
56
+ * `contexts` pass last, so redacting the whole record there needs no list of fields and covers one added later.
57
+ * An existing hook runs FIRST and keeps its say (it can still drop the record, change it, or be asynchronous);
58
+ * what it returns is scrubbed after it, so nothing it adds escapes. The options object passed in is not modified.
59
+ * Every `Sentry.init` in this repository goes through this, and an architecture spec fails if one is added without.
60
+ */
61
+ export declare function withPreviewTokenRedaction<T extends object>(options: T): T;
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Keeping a Hosted Machine preview capability out of telemetry (OSK-16174).
3
+ *
4
+ * `/api/previews/<token>/...` serves a person's machine with NO login: the token in the path is a bearer
5
+ * capability (`pvw_<22 chars>.<expiry>.<22 chars>`, idle TTL ten minutes). Anyone who can read a trace, an error
6
+ * report or a log line that carries it can open the preview, so every sink that records a URL has to run its
7
+ * strings through here first. This file is the ONE place that knows what to look for; the sinks (OpenTelemetry
8
+ * exporters, Sentry hooks, request and CSRF logs, client-log ingest) only decide WHERE to call it.
9
+ *
10
+ * WHAT IT MATCHES. The prefix `pvw_` and then every character a token is made of, however far that goes, rather
11
+ * than the exact 22.digits.22 shape. A truncated token is still part of the secret, a log line cut at 200
12
+ * characters is the ordinary way one gets truncated, and a looser match costs nothing: the marker replaces
13
+ * only what looked like a token. The token alphabet is base64url plus the two dots, or any of those characters
14
+ * written as a percent-escape, once or several times (`encodeURIComponent` of a URL that was itself in a query
15
+ * string). `packages/data-provider/specs/hostedPreview.spec.ts` pins this against the contract's token shape
16
+ * and `api/server/services/HostedPreview/__tests__/telemetryRedaction.spec.js` against tokens the issuer really
17
+ * mints, so a change to either side fails there rather than in a trace nobody reads.
18
+ *
19
+ * WHAT IT DOES NOT. A prefix that is itself percent-encoded (`%70vw_`) is not recognised: nothing in this
20
+ * repository writes one, and a scan that decoded every string first would cost every sink on every record. The
21
+ * request log's own `/[credential]` rewrite (`requestLogger.js`) covers the other credential prefixes and
22
+ * stays as it is.
23
+ *
24
+ * COST. Linear in the input. The only nested repetition is `%(?:25)*XX`, and `XX` is never `25`, so the engine
25
+ * cannot read one run of `25`s two ways; the tests feed it a megabyte of the worst shapes.
26
+ *
27
+ * NEVER THROWS. These run inside telemetry hooks, and a hook that throws takes a request or an error report
28
+ * with it. Whatever cannot be scrubbed is reported to the caller as such (`false`), and the caller drops the
29
+ * record, because a record with the token still in it is exactly what this exists to prevent.
30
+ */
31
+ /** What replaces a token. Fixed, so a trace can say a preview was involved but not which one. */
32
+ export const PREVIEW_TOKEN_REDACTION = 'pvw_[redacted]';
33
+ /**
34
+ * One character of a token: base64url, a dot, or any of those written as a percent-escape. An escape that was
35
+ * escaped again is `%25` followed by the original two digits (`_` is `%5F`, then `%255F`, then `%25255F`), which is
36
+ * what the `(?:25)*` is for. `25` is not itself a digit pair of a token character, so a run of them can only be
37
+ * read one way.
38
+ */
39
+ const TOKEN_CHAR = '(?:[A-Za-z0-9._-]|%(?:25)*(?:2[DdEe]|3[0-9]|4[1-9A-Fa-f]|5[0-9AaFf]|6[1-9A-Fa-f]|7[0-9Aa]))';
40
+ /**
41
+ * `pvw` then an underscore, which may be written `%5F` (or `%255F`, ...); then at least ONE token character. Not
42
+ * zero: the marker is `pvw_[`, and `[` is not a token character, so a string that was already redacted matches
43
+ * nothing and redacting twice changes nothing.
44
+ */
45
+ const PREVIEW_TOKEN = new RegExp(`pvw(?:_|%(?:25)*5[Ff])${TOKEN_CHAR}+`, 'g');
46
+ export function redactPreviewTokens(value) {
47
+ if (typeof value !== 'string' || value.indexOf('pvw') === -1) {
48
+ return value;
49
+ }
50
+ return value.replace(PREVIEW_TOKEN, PREVIEW_TOKEN_REDACTION);
51
+ }
52
+ const DEFAULT_MAX_NODES = 50_000;
53
+ const DEFAULT_MAX_DEPTH = 32;
54
+ /**
55
+ * Redact every string inside an object graph, IN PLACE (an error report is rewritten far more cheaply than it is
56
+ * copied). Returns `true` when the whole graph was scrubbed and `false` when some of it could not be: past
57
+ * the bounds, frozen, behind a getter that throws. A `false` means the value may still hold a token and must not
58
+ * be sent. Binary payloads are skipped, not walked, and Error messages and stacks are scrubbed though they are
59
+ * not enumerable.
60
+ */
61
+ export function redactPreviewTokensDeep(root, limits = {}) {
62
+ if (root === null || typeof root !== 'object') {
63
+ return true;
64
+ }
65
+ const maxNodes = limits.maxNodes ?? DEFAULT_MAX_NODES;
66
+ const maxDepth = limits.maxDepth ?? DEFAULT_MAX_DEPTH;
67
+ try {
68
+ const seen = new WeakSet();
69
+ const stack = [[root, 0]];
70
+ let visited = 0;
71
+ let complete = true;
72
+ const rewrite = (holder, key, current) => {
73
+ const next = redactPreviewTokens(current);
74
+ if (next === current) {
75
+ return;
76
+ }
77
+ try {
78
+ holder[key] = next;
79
+ }
80
+ catch {
81
+ // a frozen object throws in strict code
82
+ }
83
+ if (holder[key] !== next) {
84
+ // and silently keeps the old value in sloppy code
85
+ complete = false;
86
+ }
87
+ };
88
+ while (stack.length > 0) {
89
+ const [node, depth] = stack.pop();
90
+ if (seen.has(node) || ArrayBuffer.isView(node)) {
91
+ continue;
92
+ }
93
+ seen.add(node);
94
+ visited += 1;
95
+ if (visited > maxNodes) {
96
+ return false;
97
+ }
98
+ const holder = node;
99
+ if (node instanceof Error) {
100
+ for (const key of ['message', 'stack']) {
101
+ if (typeof holder[key] === 'string') {
102
+ rewrite(holder, key, holder[key]);
103
+ }
104
+ }
105
+ }
106
+ for (const key of Object.keys(node)) {
107
+ let child;
108
+ try {
109
+ child = holder[key];
110
+ }
111
+ catch {
112
+ complete = false;
113
+ continue;
114
+ }
115
+ if (typeof child === 'string') {
116
+ rewrite(holder, key, child);
117
+ }
118
+ else if (child !== null && typeof child === 'object') {
119
+ if (depth + 1 > maxDepth) {
120
+ complete = false;
121
+ }
122
+ else {
123
+ stack.push([child, depth + 1]);
124
+ }
125
+ }
126
+ }
127
+ }
128
+ return complete;
129
+ }
130
+ catch {
131
+ return false;
132
+ }
133
+ }
134
+ function isThenable(value) {
135
+ return (value !== null &&
136
+ (typeof value === 'object' || typeof value === 'function') &&
137
+ typeof value.then === 'function');
138
+ }
139
+ /** The record with its tokens redacted, or `null` (dropped) when that could not be done. */
140
+ function scrubRecord(record) {
141
+ if (record === null || record === undefined) {
142
+ return record;
143
+ }
144
+ if (typeof record !== 'object') {
145
+ return null;
146
+ }
147
+ return redactPreviewTokensDeep(record) ? record : null;
148
+ }
149
+ function guard(existing) {
150
+ return (value, hint) => {
151
+ const result = existing ? existing(value, hint) : value;
152
+ return isThenable(result) ? result.then(scrubRecord) : scrubRecord(result);
153
+ };
154
+ }
155
+ /**
156
+ * Sentry `init` options with a redacting `beforeSend`, `beforeSendTransaction` and `beforeBreadcrumb`. Those three
157
+ * are where an event's `request.url`, `request.query_string`, `transaction`, spans, breadcrumbs, `extra` and
158
+ * `contexts` pass last, so redacting the whole record there needs no list of fields and covers one added later.
159
+ * An existing hook runs FIRST and keeps its say (it can still drop the record, change it, or be asynchronous);
160
+ * what it returns is scrubbed after it, so nothing it adds escapes. The options object passed in is not modified.
161
+ * Every `Sentry.init` in this repository goes through this, and an architecture spec fails if one is added without.
162
+ */
163
+ export function withPreviewTokenRedaction(options) {
164
+ const hooks = options;
165
+ return {
166
+ ...options,
167
+ beforeSend: guard(hooks.beforeSend),
168
+ beforeSendTransaction: guard(hooks.beforeSendTransaction),
169
+ beforeBreadcrumb: guard(hooks.beforeBreadcrumb),
170
+ };
171
+ }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skrr-ai/auth-core",
3
- "version": "0.4.22",
3
+ "version": "0.4.23",
4
4
  "main": "dist/cjs/index.js",
5
5
  "types": "dist/esm/index.d.ts",
6
6
  "exports": {
@@ -183,6 +183,16 @@
183
183
  "types": "./dist/cjs/releasePolicy.d.ts",
184
184
  "default": "./dist/cjs/releasePolicy.js"
185
185
  }
186
+ },
187
+ "./telemetry-redaction": {
188
+ "import": {
189
+ "types": "./dist/esm/telemetryRedaction.d.ts",
190
+ "default": "./dist/esm/telemetryRedaction.js"
191
+ },
192
+ "require": {
193
+ "types": "./dist/cjs/telemetryRedaction.d.ts",
194
+ "default": "./dist/cjs/telemetryRedaction.js"
195
+ }
186
196
  }
187
197
  },
188
198
  "dependencies": {