[wrangler] fix: validate --preview-alias client-side and suggest sanitized value#14408
[wrangler] fix: validate --preview-alias client-side and suggest sanitized value#14408matingathani wants to merge 4 commits into
Conversation
…tized value Fixes cloudflare#14345. Manually-supplied preview aliases (via `--preview-alias`) were not validated, so values like `feature/my-feature` passed through to the API and returned an opaque error. Now wrangler validates the alias against DNS label rules before upload and throws a clear UserError — including a `Did you mean "..."?` suggestion built with `sanitizeBranchName`. Also exports `ALIAS_VALIDATION_REGEX` and `MAX_PREVIEW_ALIAS_LENGTH` from `@cloudflare/deploy-helpers` so callers can reference the same constraints.
🦋 Changeset detectedLatest commit: 56a14eb The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
@cloudflare/autoconfig
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-auth
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
| if (args.previewAlias.length > MAX_PREVIEW_ALIAS_LENGTH) { | ||
| throw new UserError( | ||
| `Preview alias "${args.previewAlias}" is too long (${args.previewAlias.length} characters). Aliases must be at most ${MAX_PREVIEW_ALIAS_LENGTH} characters.`, | ||
| { telemetryMessage: true } | ||
| ); |
There was a problem hiding this comment.
🚩 Manual alias length validation uses MAX_DNS_LABEL_LENGTH (63) without accounting for script name
The auto-generated alias in generatePreviewAlias (preview-alias.ts:93) computes available space as MAX_DNS_LABEL_LENGTH - scriptName.length - 1, accounting for the fact that the final DNS label combines alias + separator + script name. However, the manual validation here only checks args.previewAlias.length > MAX_PREVIEW_ALIAS_LENGTH (63 characters) without considering the script name. This means a user could provide a 60-character alias that passes client-side validation but might still exceed the DNS label limit when combined with the script name on the server side. This may be intentional (the server handles the full label check), but it's an asymmetry worth noting.
Was this helpful? React with 👍 or 👎 to provide feedback.
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… before display - Replace `telemetryMessage: true` with static strings to avoid sending raw user input as telemetry data - Only show the 'Did you mean' suggestion when sanitizeBranchName actually produces a value that differs from the original input AND passes ALIAS_VALIDATION_REGEX (e.g. digit-prefixed inputs like '123abc' no longer suggest themselves as a fix)
Fixes #14345.
Currently
wrangler versions upload --preview-alias <value>passes the value through to the API without any client-side validation. When a user passes a branch name directly (e.g.feature/my-feature), they get an opaque API error rather than a clear message about why the value is invalid.This PR adds client-side validation of manually-supplied preview aliases before the upload request is made:
ALIAS_VALIDATION_REGEX(/^[a-z](?:[a-z0-9-]*[a-z0-9])?$/i) with a clear message and aDid you mean "..."?suggestion built viasanitizeBranchName.ALIAS_VALIDATION_REGEXandMAX_PREVIEW_ALIAS_LENGTHfrom@cloudflare/deploy-helpersso consumers share the same constants.Auto-generated aliases (via
getCIGeneratePreviewAlias()) are unaffected — those already go throughsanitizeBranchNameinsidegeneratePreviewAlias.