Skip to content

Commit 1aa2a91

Browse files
authored
feat: allowlist some types defaults (#8130)
* allowlist some types defaults * changeset * fixup * fixups
1 parent 6fe4a67 commit 1aa2a91

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

.changeset/wet-starfishes-drum.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Include default values for wrangler types --path and --x-include-runtime in telemetry
6+
7+
User provided strings are still left redacted as always.

packages/wrangler/src/__tests__/metrics.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,22 +582,31 @@ describe("metrics", () => {
582582
const args = {
583583
default: false,
584584
array: ["beep", "boop"],
585-
secretArray: ["beep", "boop"],
586-
// Note how
585+
// Note how this is normalised
587586
"secret-array": ["beep", "boop"],
588587
number: 42,
589588
string: "secret",
590589
secretString: "secret",
590+
flagOne: "default",
591+
// Note how this is normalised
592+
experimentalIncludeRuntime: "",
591593
};
592594

593-
const redacted = redactArgValues(args, ["string", "array"]);
595+
const redacted = redactArgValues(args, {
596+
string: "*",
597+
array: "*",
598+
flagOne: ["default"],
599+
xIncludeRuntime: [".wrangler/types/runtime.d.ts"],
600+
});
594601
expect(redacted).toEqual({
595602
default: false,
596603
array: ["beep", "boop"],
597604
secretArray: ["<REDACTED>", "<REDACTED>"],
598605
number: 42,
599606
string: "secret",
600607
secretString: "<REDACTED>",
608+
flagOne: "default",
609+
xIncludeRuntime: ".wrangler/types/runtime.d.ts",
601610
});
602611
});
603612
});

packages/wrangler/src/metrics/metrics-dispatcher.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
3232
let amplitude_event_id = 0;
3333

3434
/** We redact strings in arg values, unless they are named here */
35-
const allowList = {
35+
const allowList: Record<string, AllowedValues> & { "*": AllowedValues } = {
3636
// applies to all commands
3737
// use camelCase version
38-
"*": ["format", "logLevel"],
39-
// specific commands
40-
tail: ["status"],
38+
"*": { format: "*", logLevel: "*" },
39+
"wrangler tail": { status: "*" },
40+
"wrangler types": {
41+
xIncludeRuntime: [".wrangler/types/runtime.d.ts"],
42+
path: ["worker-configuration.d.ts"],
43+
},
4144
};
4245

4346
return {
@@ -253,35 +256,50 @@ const sanitiseUserInput = (
253256
return result;
254257
};
255258

259+
type AllowedValues = Record<string, string[] | "*">;
256260
const getAllowedArgs = (
257-
allowList: Record<string, string[]> & { "*": string[] },
261+
allowList: Record<string, AllowedValues> & { "*": AllowedValues },
258262
key: string
259263
) => {
260264
const commandSpecific = allowList[key] ?? [];
261-
return [...commandSpecific, ...allowList["*"]];
265+
return { ...commandSpecific, ...allowList["*"] };
262266
};
263267
export const redactArgValues = (
264268
args: Record<string, unknown>,
265-
allowedKeys: string[]
269+
allowedValues: AllowedValues
266270
) => {
267271
const result: Record<string, unknown> = {};
268272

269-
for (const [k, value] of Object.entries(args)) {
270-
const key = normalise(k);
273+
for (let [key, value] of Object.entries(args)) {
274+
key = normalise(key);
275+
if (key === "xIncludeRuntime" && value === "") {
276+
value = ".wrangler/types/runtime.d.ts";
277+
}
278+
const allowedValuesForArg = allowedValues[key] ?? [];
271279
if (exclude.has(key)) {
272280
continue;
273281
}
274282
if (
275283
typeof value === "number" ||
276284
typeof value === "boolean" ||
277-
allowedKeys.includes(normalise(key))
285+
allowedValuesForArg.includes(key)
278286
) {
279287
result[key] = value;
280-
} else if (typeof value === "string") {
288+
} else if (
289+
// redact if its a string, unless the value is in the allow list
290+
// * is a special value that allows all values for that arg
291+
typeof value === "string" &&
292+
!(allowedValuesForArg === "*" || allowedValuesForArg.includes(value))
293+
) {
281294
result[key] = "<REDACTED>";
282295
} else if (Array.isArray(value)) {
283296
result[key] = value.map((v) =>
284-
typeof v === "string" ? "<REDACTED>" : v
297+
// redact if its a string, unless the value is in the allow list
298+
// * is a special value that allows all values for that arg
299+
typeof v === "string" &&
300+
!(allowedValuesForArg === "*" || allowedValuesForArg.includes(v))
301+
? "<REDACTED>"
302+
: v
285303
);
286304
} else {
287305
result[key] = value;

0 commit comments

Comments
 (0)