Improve whitespace handling

This commit is contained in:
Michael B. Gale
2026-06-23 18:42:43 +01:00
parent 00e5a58139
commit 8d69da902e
3 changed files with 70 additions and 12 deletions

16
lib/entry-points.js generated
View File

@@ -151452,21 +151452,25 @@ function parseRemoteFileAddress(env, configFile) {
"^((?<owner>[^/]+)/)?(?<repo>[^/@]+)(/(?<path>[^@]+))?(@(?<ref>.*))?$"
);
const pieces = format.exec(configFile.trim());
if (!pieces?.groups?.repo || pieces.groups.repo.trim().length === 0) {
const repo = pieces?.groups?.repo?.trim();
if (!pieces?.groups || !repo || repo.length === 0) {
throw new ConfigurationError(
getConfigFileRepoFormatInvalidMessage(configFile)
);
}
if (pieces.groups.path?.startsWith("/")) {
const owner = pieces.groups.owner?.trim();
const path29 = pieces.groups.path?.trim();
const ref = pieces.groups.ref?.trim();
if (path29?.startsWith("/")) {
throw new ConfigurationError(
`The path component of '${configFile}' cannot be an absolute path.`
);
}
return {
owner: pieces.groups.owner || getDefaultOwner(env),
repo: pieces.groups.repo.trim(),
path: pieces.groups.path || DEFAULT_CONFIG_FILE_NAME,
ref: pieces.groups.ref || DEFAULT_CONFIG_FILE_REF
owner: owner || getDefaultOwner(env),
repo,
path: path29 || DEFAULT_CONFIG_FILE_NAME,
ref: ref || DEFAULT_CONFIG_FILE_REF
};
}

View File

@@ -22,6 +22,48 @@ test("parseRemoteFileAddress accepts full remote addresses", async (t) => {
ref: "ref",
} satisfies RemoteFileAddress);
t.deepEqual(parseRemoteFileAddress(env, "owner /repo/path@ref"), {
owner: "owner",
repo: "repo",
path: "path",
ref: "ref",
} satisfies RemoteFileAddress);
t.deepEqual(parseRemoteFileAddress(env, "owner/ repo/path@ref"), {
owner: "owner",
repo: "repo",
path: "path",
ref: "ref",
} satisfies RemoteFileAddress);
t.deepEqual(parseRemoteFileAddress(env, "owner/repo /path@ref"), {
owner: "owner",
repo: "repo",
path: "path",
ref: "ref",
} satisfies RemoteFileAddress);
t.deepEqual(parseRemoteFileAddress(env, "owner/repo/ path@ref"), {
owner: "owner",
repo: "repo",
path: "path",
ref: "ref",
} satisfies RemoteFileAddress);
t.deepEqual(parseRemoteFileAddress(env, "owner/repo/path @ref"), {
owner: "owner",
repo: "repo",
path: "path",
ref: "ref",
} satisfies RemoteFileAddress);
t.deepEqual(parseRemoteFileAddress(env, "owner/repo/path@ ref"), {
owner: "owner",
repo: "repo",
path: "path",
ref: "ref",
} satisfies RemoteFileAddress);
t.deepEqual(
parseRemoteFileAddress(env, "owner/repo/path/to/codeql.yml@ref/feature"),
{
@@ -129,4 +171,10 @@ test("parseRemoteFileAddress rejects invalid values", async (t) => {
t.throws(() => parseRemoteFileAddress(env, "repo//absolute"), {
instanceOf: ConfigurationError,
});
t.throws(() => parseRemoteFileAddress(env, "/repo@ref"), {
instanceOf: ConfigurationError,
});
t.throws(() => parseRemoteFileAddress(env, " /repo@ref"), {
instanceOf: ConfigurationError,
});
});

View File

@@ -55,24 +55,30 @@ export function parseRemoteFileAddress(
);
const pieces = format.exec(configFile.trim());
const repo: string | undefined = pieces?.groups?.repo?.trim();
// Check that the regular expression matched and that we have at least the repo name.
if (!pieces?.groups?.repo || pieces.groups.repo.trim().length === 0) {
if (!pieces?.groups || !repo || repo.length === 0) {
throw new ConfigurationError(
errorMessages.getConfigFileRepoFormatInvalidMessage(configFile),
);
}
const owner: string | undefined = pieces.groups.owner?.trim();
const path: string | undefined = pieces.groups.path?.trim();
const ref: string | undefined = pieces.groups.ref?.trim();
// Ensure that the path is a relative path.
if (pieces.groups.path?.startsWith("/")) {
if (path?.startsWith("/")) {
throw new ConfigurationError(
`The path component of '${configFile}' cannot be an absolute path.`,
);
}
return {
owner: pieces.groups.owner || getDefaultOwner(env),
repo: pieces.groups.repo.trim(),
path: pieces.groups.path || DEFAULT_CONFIG_FILE_NAME,
ref: pieces.groups.ref || DEFAULT_CONFIG_FILE_REF,
owner: owner || getDefaultOwner(env),
repo,
path: path || DEFAULT_CONFIG_FILE_NAME,
ref: ref || DEFAULT_CONFIG_FILE_REF,
};
}