Merge pull request #2479 from github/koesie10/validate-scoped-pack-name
Only allow scoped extension pack names
This commit is contained in:
@@ -19,7 +19,7 @@ const maxStep = 3;
|
||||
|
||||
const packNamePartRegex = /[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
|
||||
const packNameRegex = new RegExp(
|
||||
`^(?:(?<scope>${packNamePartRegex.source})/)?(?<name>${packNamePartRegex.source})$`,
|
||||
`^(?<scope>${packNamePartRegex.source})/(?<name>${packNamePartRegex.source})$`,
|
||||
);
|
||||
const packNameLength = 128;
|
||||
|
||||
@@ -246,11 +246,16 @@ async function pickNewExtensionPack(
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let examplePackName = `${databaseItem.name}-extensions`;
|
||||
if (!examplePackName.includes("/")) {
|
||||
examplePackName = `pack/${examplePackName}`;
|
||||
}
|
||||
|
||||
const packName = await window.showInputBox(
|
||||
{
|
||||
title: "Create new extension pack",
|
||||
prompt: "Enter name of extension pack",
|
||||
placeHolder: `e.g. ${databaseItem.name}-extensions`,
|
||||
placeHolder: `e.g. ${examplePackName}`,
|
||||
validateInput: async (value: string): Promise<string | undefined> => {
|
||||
if (!value) {
|
||||
return "Pack name must not be empty";
|
||||
@@ -262,6 +267,10 @@ async function pickNewExtensionPack(
|
||||
|
||||
const matches = packNameRegex.exec(value);
|
||||
if (!matches?.groups) {
|
||||
if (!value.includes("/")) {
|
||||
return "Invalid package name: a pack name must contain a slash to separate the scope from the pack name";
|
||||
}
|
||||
|
||||
return "Invalid package name: a pack name must contain only lowercase ASCII letters, ASCII digits, and hyphens";
|
||||
}
|
||||
|
||||
@@ -296,7 +305,7 @@ async function pickNewExtensionPack(
|
||||
const extensionPack: ExtensionPack = {
|
||||
path: packPath,
|
||||
yamlPath: packYamlPath,
|
||||
name,
|
||||
name: packName,
|
||||
version: "0.0.0",
|
||||
extensionTargets: {
|
||||
[`codeql/${databaseItem.language}-all`]: "*",
|
||||
|
||||
@@ -232,7 +232,7 @@ describe("pickExtensionPackModelFile", () => {
|
||||
label: "codeql-custom-queries-java",
|
||||
path: tmpDir.path,
|
||||
} as QuickPickItem);
|
||||
showInputBoxSpy.mockResolvedValueOnce("new-extension-pack");
|
||||
showInputBoxSpy.mockResolvedValueOnce("pack/new-extension-pack");
|
||||
showInputBoxSpy.mockResolvedValue("models/my-model.yml");
|
||||
|
||||
expect(
|
||||
@@ -247,7 +247,7 @@ describe("pickExtensionPackModelFile", () => {
|
||||
extensionPack: {
|
||||
path: newPackDir,
|
||||
yamlPath: join(newPackDir, "codeql-pack.yml"),
|
||||
name: "new-extension-pack",
|
||||
name: "pack/new-extension-pack",
|
||||
version: "0.0.0",
|
||||
extensionTargets: {
|
||||
"codeql/java-all": "*",
|
||||
@@ -280,7 +280,7 @@ describe("pickExtensionPackModelFile", () => {
|
||||
expect(
|
||||
loadYaml(await readFile(join(newPackDir, "codeql-pack.yml"), "utf8")),
|
||||
).toEqual({
|
||||
name: "new-extension-pack",
|
||||
name: "pack/new-extension-pack",
|
||||
version: "0.0.0",
|
||||
library: true,
|
||||
extensionTargets: {
|
||||
@@ -303,7 +303,7 @@ describe("pickExtensionPackModelFile", () => {
|
||||
label: "codeql-custom-queries-java",
|
||||
path: tmpDir.path,
|
||||
} as QuickPickItem);
|
||||
showInputBoxSpy.mockResolvedValueOnce("new-extension-pack");
|
||||
showInputBoxSpy.mockResolvedValueOnce("pack/new-extension-pack");
|
||||
showInputBoxSpy.mockResolvedValue("models/my-model.yml");
|
||||
|
||||
expect(
|
||||
@@ -321,7 +321,7 @@ describe("pickExtensionPackModelFile", () => {
|
||||
extensionPack: {
|
||||
path: newPackDir,
|
||||
yamlPath: join(newPackDir, "codeql-pack.yml"),
|
||||
name: "new-extension-pack",
|
||||
name: "pack/new-extension-pack",
|
||||
version: "0.0.0",
|
||||
extensionTargets: {
|
||||
"codeql/csharp-all": "*",
|
||||
@@ -354,7 +354,7 @@ describe("pickExtensionPackModelFile", () => {
|
||||
expect(
|
||||
loadYaml(await readFile(join(newPackDir, "codeql-pack.yml"), "utf8")),
|
||||
).toEqual({
|
||||
name: "new-extension-pack",
|
||||
name: "pack/new-extension-pack",
|
||||
version: "0.0.0",
|
||||
library: true,
|
||||
extensionTargets: {
|
||||
@@ -806,6 +806,12 @@ describe("pickExtensionPackModelFile", () => {
|
||||
"Invalid package name: a pack name must contain only lowercase ASCII letters, ASCII digits, and hyphens",
|
||||
);
|
||||
expect(await validateFile("VSCODE")).toEqual(
|
||||
"Invalid package name: a pack name must contain a slash to separate the scope from the pack name",
|
||||
);
|
||||
expect(await validateFile("github/")).toEqual(
|
||||
"Invalid package name: a pack name must contain only lowercase ASCII letters, ASCII digits, and hyphens",
|
||||
);
|
||||
expect(await validateFile("github/VSCODE")).toEqual(
|
||||
"Invalid package name: a pack name must contain only lowercase ASCII letters, ASCII digits, and hyphens",
|
||||
);
|
||||
expect(await validateFile("github/vscode-codeql-")).toEqual(
|
||||
@@ -814,7 +820,7 @@ describe("pickExtensionPackModelFile", () => {
|
||||
expect(
|
||||
await validateFile("github/vscode-codeql-extensions"),
|
||||
).toBeUndefined();
|
||||
expect(await validateFile("vscode-codeql-extensions")).toBeUndefined();
|
||||
expect(await validateFile("pack/vscode-codeql-extensions")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("validates the file input", async () => {
|
||||
|
||||
Reference in New Issue
Block a user