Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/preview-alias-validate-manual-input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

`wrangler versions upload --preview-alias` now validates the alias client-side and returns a clear error with a sanitized suggestion when the value contains invalid characters (e.g. slashes from branch names like `feature/my-feature`) or exceeds 63 characters.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { logger } from "../../shared/context";

const MAX_DNS_LABEL_LENGTH = 63;
const HASH_LENGTH = 4;
const ALIAS_VALIDATION_REGEX = /^[a-z](?:[a-z0-9-]*[a-z0-9])?$/i;
export const ALIAS_VALIDATION_REGEX = /^[a-z](?:[a-z0-9-]*[a-z0-9])?$/i;
export const MAX_PREVIEW_ALIAS_LENGTH = MAX_DNS_LABEL_LENGTH;

/**
* Sanitizes a branch name to create a valid DNS label alias.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,41 @@ describe.each([
expect(metadata.annotations).toEqual({ "workers/alias": "my-alias" });
});

it("--preview-alias rejects aliases with slashes", async ({ expect }) => {
writeWranglerConfig({ main: "./index.js" });
writeWorkerSource();
await expect(
runWrangler("versions upload --preview-alias feature/my-feature")
).rejects.toThrow(
`Preview alias "feature/my-feature" is invalid. Aliases must start with a letter and contain only lowercase letters, numbers, and hyphens. Did you mean "feature-my-feature"?`
);
});

it("--preview-alias rejects aliases that are too long", async ({
expect,
}) => {
writeWranglerConfig({ main: "./index.js" });
writeWorkerSource();
const longAlias = "a".repeat(64);
await expect(
runWrangler(`versions upload --preview-alias ${longAlias}`)
).rejects.toThrow(
`Preview alias "${longAlias}" is too long (64 characters). Aliases must be at most 63 characters.`
);
});

it("--preview-alias rejects aliases starting with a number", async ({
expect,
}) => {
writeWranglerConfig({ main: "./index.js" });
writeWorkerSource();
await expect(
runWrangler("versions upload --preview-alias 123abc")
).rejects.toThrow(
`Preview alias "123abc" is invalid. Aliases must start with a letter and contain only lowercase letters, numbers, and hyphens.`
);
});

it("annotations default to undefined when no flags provided", async ({
expect,
}) => {
Expand Down
29 changes: 28 additions & 1 deletion packages/wrangler/src/deployment-bundle/merge-config-args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { generatePreviewAlias } from "@cloudflare/deploy-helpers";
import {
ALIAS_VALIDATION_REGEX,
MAX_PREVIEW_ALIAS_LENGTH,
generatePreviewAlias,
sanitizeBranchName,
} from "@cloudflare/deploy-helpers";
import {
getCIGeneratePreviewAlias,
getCIOverrideName,
Expand Down Expand Up @@ -183,6 +188,28 @@ export async function mergeVersionsUploadConfigArgs(
? generatePreviewAlias(shared.name ?? "")
: undefined);

if (args.previewAlias !== undefined) {
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: "versions upload preview alias too long" }
);
Comment on lines +192 to +196

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚩 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.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

}
if (!ALIAS_VALIDATION_REGEX.test(args.previewAlias)) {
const suggestion = sanitizeBranchName(args.previewAlias);
const validSuggestion =
suggestion !== args.previewAlias &&
ALIAS_VALIDATION_REGEX.test(suggestion)
? suggestion
: undefined;
throw new UserError(
`Preview alias "${args.previewAlias}" is invalid. Aliases must start with a letter and contain only lowercase letters, numbers, and hyphens.` +
(validSuggestion ? ` Did you mean "${validSuggestion}"?` : ""),
{ telemetryMessage: "versions upload preview alias invalid" }
);
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
}
}

return {
props: {
...shared,
Expand Down
Loading