Skip to content

Commit a447d67

Browse files
Require generated aliases to meet stricter naming requirements (#9821)
Test alias names with a regex to ensure it meets requirements prior to version upload. When we enable this for WCI we want to ensure that we aren't failing builds because the name is invalid.
1 parent 57d3d01 commit a447d67

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

.changeset/heavy-wasps-switch.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+
Preview Aliases: Force alias generation to meet stricter naming requirements.
6+
7+
For cases where CI is requesting Wrangler to generate the alias based on the branch name, we want a stricter check around the generated alias name in order to avoid version upload failures. If a valid alias name was not able to be generated, we warn and do not provide an alias (avoiding a version upload failure).

packages/wrangler/src/__tests__/versions/versions.upload.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,25 @@ describe("generatePreviewAlias", () => {
420420
const result = generatePreviewAlias("testscript");
421421
expect(result).toBeUndefined();
422422
});
423+
424+
it("Strips leading dashes from branch name", () => {
425+
vi.stubEnv("WORKERS_CI_BRANCH", "-some-branch-name");
426+
427+
const result = generatePreviewAlias("testscript");
428+
expect(result).toBe("some-branch-name");
429+
});
430+
431+
it("Removes concurrent dashes from branch name", () => {
432+
vi.stubEnv("WORKERS_CI_BRANCH", "some----branch-----name");
433+
434+
const result = generatePreviewAlias("testscript");
435+
expect(result).toBe("some-branch-name");
436+
});
437+
438+
it("Does not produce an alias with leading numbers", () => {
439+
vi.stubEnv("WORKERS_CI_BRANCH", "0AF0ED");
440+
441+
const result = generatePreviewAlias("testscript");
442+
expect(result).toBeUndefined();
443+
});
423444
});

packages/wrangler/src/versions/upload.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,13 @@ function formatTime(duration: number) {
902902
* Returns undefined if not in a git directory or requirements cannot be met.
903903
*/
904904
export function generatePreviewAlias(scriptName: string): string | undefined {
905+
const warnAndExit = () => {
906+
logger.warn(
907+
`Preview alias generation requested, but could not be autogenerated.`
908+
);
909+
return undefined;
910+
};
911+
905912
let branchName = getWorkersCIBranchName();
906913
if (!branchName) {
907914
try {
@@ -910,18 +917,12 @@ export function generatePreviewAlias(scriptName: string): string | undefined {
910917
.toString()
911918
.trim();
912919
} catch (err) {
913-
logger.warn(
914-
`Preview alias generation requested, but could not be autogenerated.`
915-
);
916-
return undefined;
920+
return warnAndExit();
917921
}
918922
}
919923

920924
if (!branchName) {
921-
logger.warn(
922-
`Preview alias generation requested, but could not be autogenerated.`
923-
);
924-
return undefined;
925+
return warnAndExit();
925926
}
926927

927928
const sanitizedAlias = branchName
@@ -930,15 +931,24 @@ export function generatePreviewAlias(scriptName: string): string | undefined {
930931
.replace(/^-+|-+$/g, "") // trim dashes
931932
.toLowerCase(); // lowercase the name
932933

934+
// Ensure the alias name meets requirements:
935+
// - only alphanumeric or hyphen characters
936+
// - no trailing slashes
937+
// - begins with letter
938+
// - no longer than 63 total characters
939+
const isValidAlias = /^[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(
940+
sanitizedAlias
941+
);
942+
if (!isValidAlias) {
943+
return warnAndExit();
944+
}
945+
933946
// Dns labels can only have a max of 63 chars. We use preview urls in the form of <alias>-<workerName>
934947
// which means our alias must be shorter than 63-scriptNameLen-1
935948
const maxDnsLabelLength = 63;
936949
const available = maxDnsLabelLength - scriptName.length - 1;
937950
if (sanitizedAlias.length > available) {
938-
logger.warn(
939-
`Preview alias generation requested, but could not be autogenerated.`
940-
);
941-
return undefined;
951+
return warnAndExit();
942952
}
943953

944954
return sanitizedAlias;

0 commit comments

Comments
 (0)