-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[wrangler] Categorise deploy positional path in command telemetry #14587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MattieTK
wants to merge
1
commit into
main
Choose a base branch
from
telemetry-deploy-positional-arg-category
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "wrangler": minor | ||
| --- | ||
|
|
||
| Categorise the positional path argument to `wrangler deploy` and `wrangler versions upload` in command telemetry | ||
|
|
||
| Command telemetry now records a coarse category for the entry-point/assets positional (`wrangler deploy <path>`) under `sanitizedArgs.path`, so we can understand whether people pass a file, a directory, or a relational reference such as `.` or `../example`. The possible values are `file`, `directory`, `current-dir`, `parent-relative`, and `not-found`, or `null` when no positional is provided. The raw path is never sent — only the category. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { statSync } from "node:fs"; | ||
| import { UserError } from "@cloudflare/workers-utils"; | ||
| import { getErrorType } from "../core/handle-errors"; | ||
|
|
||
|
|
@@ -30,22 +31,64 @@ export function sanitizeArgKeys( | |
| export const REDACT = Symbol("REDACT"); | ||
| /** Allow all values for this arg. */ | ||
| export const ALLOW = Symbol("ALLOW"); | ||
| /** | ||
| * Transform this arg's value into a coarse, non-identifying category label | ||
| * instead of logging it raw. Return `null` to record that the arg was absent, | ||
| * or `undefined` to omit the arg entirely. | ||
| * | ||
| * Categorisers are the safe way to gather telemetry about high-cardinality or | ||
| * potentially sensitive values (such as file paths) — only the returned label | ||
| * is ever sent, never the original value. Unlike the other allow-list values, | ||
| * categorisers are also applied to positional args (see `categoriseArgs`). | ||
| */ | ||
| export type Categoriser = (value: unknown) => string | null | undefined; | ||
| export type AllowedArgs = { | ||
| [arg: string]: unknown[] | typeof REDACT | typeof ALLOW; | ||
| [arg: string]: unknown[] | typeof REDACT | typeof ALLOW | Categoriser; | ||
| }; | ||
| export type AllowList = Record<string, AllowedArgs>; | ||
|
|
||
| /** | ||
| * Categorises a positional path argument (e.g. the entry-point or assets path | ||
| * passed to `wrangler deploy` / `wrangler versions upload`) into a coarse, | ||
| * non-identifying label. | ||
| * | ||
| * The raw value is never returned — paths can leak sensitive information and | ||
| * are too high-cardinality to analyse. Relational references (`.`, `..`) are | ||
| * surfaced as their own categories; anything else is resolved against the | ||
| * filesystem to distinguish files from directories, mirroring how the deploy | ||
| * path positional is actually interpreted. Returns `null` when no positional | ||
| * was provided so the property is always present in telemetry. | ||
| */ | ||
| export function categorisePositionalPath(value: unknown): string | null { | ||
| if (typeof value !== "string" || value.length === 0) { | ||
| return null; | ||
| } | ||
| if (value === "." || value === "./" || value === ".\\") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth also categorising real paths that happen to be the current directory in this way too? |
||
| return "current-dir"; | ||
| } | ||
| if (value === ".." || value.startsWith("../") || value.startsWith("..\\")) { | ||
| return "parent-relative"; | ||
| } | ||
| try { | ||
| return statSync(value).isDirectory() ? "directory" : "file"; | ||
| } catch { | ||
| return "not-found"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A list of all the command args that are allowed. | ||
| * | ||
| * A wildcard "<command> *" applies to all sub-commands of `<command>`. | ||
| * The top level "*" applies to all commands. | ||
| * Specific commands can override or add to the allow list. | ||
| * | ||
| * Each arg can have one of three values: | ||
| * Each arg can have one of four values: | ||
| * - an array of strings: only those specific values are allowed | ||
| * - REDACT: the arg value will always be redacted | ||
| * - ALLOW: all values for that arg are allowed | ||
| * - a Categoriser function: the value is transformed into a coarse category | ||
| * label (also applied to positional args) | ||
| */ | ||
| export const COMMAND_ARG_ALLOW_LIST: AllowList = { | ||
| // * applies to all commands. | ||
|
|
@@ -98,6 +141,12 @@ export const COMMAND_ARG_ALLOW_LIST: AllowList = { | |
| }, | ||
| deploy: { | ||
| containersRollout: ["immediate", "gradual"], | ||
| // Categorise the entry-point/assets positional without logging the raw path. | ||
| path: categorisePositionalPath, | ||
| }, | ||
| "versions upload": { | ||
| // `versions upload` shares the same positional path semantics as `deploy`. | ||
| path: categorisePositionalPath, | ||
| }, | ||
| }; | ||
|
|
||
|
|
@@ -155,6 +204,33 @@ export function sanitizeArgValues( | |
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Applies categoriser transforms from the allow-list to the raw args. | ||
| * | ||
| * Unlike {@link sanitizeArgValues}, this reads from the *full* args object | ||
| * rather than the argv-filtered set produced by {@link sanitizeArgKeys}. That | ||
| * is what allows positional args — which are otherwise deliberately excluded | ||
| * from telemetry — to be safely categorised into a fixed label. Only args whose | ||
| * allow-list entry is a {@link Categoriser} are considered, and only the label | ||
| * it returns (never the raw value) is emitted. A `null` result is recorded as-is | ||
| * (the arg was absent); an `undefined` result omits the arg entirely. | ||
| */ | ||
| export function categoriseArgs( | ||
| args: Record<string, unknown>, | ||
| allowedArgs: AllowedArgs | ||
| ): Record<string, string | null> { | ||
| const result: Record<string, string | null> = {}; | ||
| for (const [key, allowed] of Object.entries(allowedArgs)) { | ||
| if (typeof allowed === "function") { | ||
| const category = allowed(args[key]); | ||
| if (category !== undefined) { | ||
| result[key] = category; | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the canonical argument name for metrics reporting. | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: This is a little brittle since the slightly different text in the debug output would still pass -
"path": "index.js"for example.