Make URL parsing more robust
This commit is contained in:
Родитель
2841489ddf
Коммит
c4dc1b0438
|
@ -23,24 +23,6 @@ const upload_lib = __importStar(require("./upload-lib"));
|
|||
const util_1 = require("./util");
|
||||
const program = new commander_1.Command();
|
||||
program.version("0.0.1");
|
||||
function parseGithubUrl(inputUrl) {
|
||||
try {
|
||||
const url = new URL(inputUrl);
|
||||
// If we detect this is trying to be to github.com
|
||||
// then return with a fixed canonical URL.
|
||||
if (url.hostname === "github.com" || url.hostname === "api.github.com") {
|
||||
return "https://github.com";
|
||||
}
|
||||
// Remove the API prefix if it's present
|
||||
if (url.pathname.indexOf("/api/v3") !== -1) {
|
||||
url.pathname = url.pathname.substring(0, url.pathname.indexOf("/api/v3"));
|
||||
}
|
||||
return url.toString();
|
||||
}
|
||||
catch (e) {
|
||||
throw new Error(`"${inputUrl}" is not a valid URL`);
|
||||
}
|
||||
}
|
||||
function getTempDir(userInput) {
|
||||
const tempDir = path.join(userInput || process.cwd(), "codeql-runner");
|
||||
if (!fs.existsSync(tempDir)) {
|
||||
|
@ -126,9 +108,9 @@ program
|
|||
codeql = codeql_1.getCodeQL(cmd.codeqlPath);
|
||||
}
|
||||
else {
|
||||
codeql = await init_1.initCodeQL(undefined, cmd.githubAuth, parseGithubUrl(cmd.githubUrl), tempDir, toolsDir, "runner", logger);
|
||||
codeql = await init_1.initCodeQL(undefined, cmd.githubAuth, util_1.parseGithubUrl(cmd.githubUrl), tempDir, toolsDir, "runner", logger);
|
||||
}
|
||||
const config = await init_1.initConfig(cmd.languages, cmd.queries, cmd.configFile, repository_1.parseRepositoryNwo(cmd.repository), tempDir, toolsDir, codeql, cmd.checkoutPath || process.cwd(), cmd.githubAuth, parseGithubUrl(cmd.githubUrl), logger);
|
||||
const config = await init_1.initConfig(cmd.languages, cmd.queries, cmd.configFile, repository_1.parseRepositoryNwo(cmd.repository), tempDir, toolsDir, codeql, cmd.checkoutPath || process.cwd(), cmd.githubAuth, util_1.parseGithubUrl(cmd.githubUrl), logger);
|
||||
const tracerConfig = await init_1.runInit(codeql, config);
|
||||
if (tracerConfig === undefined) {
|
||||
return;
|
||||
|
@ -237,7 +219,7 @@ program
|
|||
throw new Error("Config file could not be found at expected location. " +
|
||||
"Was the 'init' command run with the same '--temp-dir' argument as this command.");
|
||||
}
|
||||
await analyze_1.runAnalyze(repository_1.parseRepositoryNwo(cmd.repository), cmd.commit, parseRef(cmd.ref), undefined, undefined, undefined, cmd.checkoutPath || process.cwd(), undefined, cmd.githubAuth, parseGithubUrl(cmd.githubUrl), cmd.upload, "runner", outputDir, util_1.getMemoryFlag(cmd.ram), util_1.getAddSnippetsFlag(cmd.addSnippets), util_1.getThreadsFlag(cmd.threads, logger), config, logger);
|
||||
await analyze_1.runAnalyze(repository_1.parseRepositoryNwo(cmd.repository), cmd.commit, parseRef(cmd.ref), undefined, undefined, undefined, cmd.checkoutPath || process.cwd(), undefined, cmd.githubAuth, util_1.parseGithubUrl(cmd.githubUrl), cmd.upload, "runner", outputDir, util_1.getMemoryFlag(cmd.ram), util_1.getAddSnippetsFlag(cmd.addSnippets), util_1.getThreadsFlag(cmd.threads, logger), config, logger);
|
||||
}
|
||||
catch (e) {
|
||||
logger.error("Analyze failed");
|
||||
|
@ -259,7 +241,7 @@ program
|
|||
.action(async (cmd) => {
|
||||
const logger = logging_1.getRunnerLogger(cmd.debug);
|
||||
try {
|
||||
await upload_lib.upload(cmd.sarifFile, repository_1.parseRepositoryNwo(cmd.repository), cmd.commit, parseRef(cmd.ref), undefined, undefined, undefined, cmd.checkoutPath || process.cwd(), undefined, cmd.githubAuth, parseGithubUrl(cmd.githubUrl), "runner", logger);
|
||||
await upload_lib.upload(cmd.sarifFile, repository_1.parseRepositoryNwo(cmd.repository), cmd.commit, parseRef(cmd.ref), undefined, undefined, undefined, cmd.checkoutPath || process.cwd(), undefined, cmd.githubAuth, util_1.parseGithubUrl(cmd.githubUrl), "runner", logger);
|
||||
}
|
||||
catch (e) {
|
||||
logger.error("Upload failed");
|
||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -151,4 +151,43 @@ function getCodeQLDatabasePath(tempDir, language) {
|
|||
return path.resolve(getCodeQLDatabasesDir(tempDir), language);
|
||||
}
|
||||
exports.getCodeQLDatabasePath = getCodeQLDatabasePath;
|
||||
/**
|
||||
* Parses user input of a github.com or GHES URL to a canonical form.
|
||||
* Removes any API prefix or suffix if one is present.
|
||||
*/
|
||||
function parseGithubUrl(inputUrl) {
|
||||
const originalUrl = inputUrl;
|
||||
if (inputUrl.indexOf("://") === -1) {
|
||||
inputUrl = `https://${inputUrl}`;
|
||||
}
|
||||
if (!inputUrl.startsWith("http://") && !inputUrl.startsWith("https://")) {
|
||||
throw new Error(`"${originalUrl}" is not a http or https URL`);
|
||||
}
|
||||
let url;
|
||||
try {
|
||||
url = new URL(inputUrl);
|
||||
}
|
||||
catch (e) {
|
||||
throw new Error(`"${originalUrl}" is not a valid URL`);
|
||||
}
|
||||
// If we detect this is trying to be to github.com
|
||||
// then return with a fixed canonical URL.
|
||||
if (url.hostname === "github.com" || url.hostname === "api.github.com") {
|
||||
return "https://github.com/";
|
||||
}
|
||||
// Remove the API prefix if it's present
|
||||
if (url.pathname.indexOf("/api/v3") !== -1) {
|
||||
url.pathname = url.pathname.substring(0, url.pathname.indexOf("/api/v3"));
|
||||
}
|
||||
// Also consider subdomain isolation on GHES
|
||||
if (url.hostname.startsWith("api.")) {
|
||||
url.hostname = url.hostname.substring(4);
|
||||
}
|
||||
// Normalise path to having a trailing slash for consistency
|
||||
if (!url.pathname.endsWith("/")) {
|
||||
url.pathname = `${url.pathname}/`;
|
||||
}
|
||||
return url.toString();
|
||||
}
|
||||
exports.parseGithubUrl = parseGithubUrl;
|
||||
//# sourceMappingURL=util.js.map
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;;;;;;;;AAAA,uCAAyB;AACzB,uCAAyB;AACzB,2CAA6B;AAU7B;;G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AC,EAAE;QACV,MAAM,IAAI,KAAK,CAAC,IAAI,WAAW,sBAAsB,CAAC,CAAC;KACxD;IAED,kDAAkD;IAClD,0CAA0C;IAC1C,IAAI,GAAG,CAAC,QAAQ,KAAK,YAAY,IAAI,GAAG,CAAC,QAAQ,KAAK,gBAAgB,EAAE;QACtE,OAAO,qBAAqB,CAAC;KAC9B;IAED,wCAAwC;IACxC,IAAI,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE;QAC1C,GAAG,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;KAC3E;IACD,4CAA4C;IAC5C,IAAI,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE;QACnC,GAAG,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;KAC1C;IAED,4DAA4D;IAC5D,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QAC/B,GAAG,CAAC,QAAQ,GAAG,GAAG,GAAG,CAAC,QAAQ,GAAG,CAAC;KACnC;IAED,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC;AACxB,CAAC;AArCD,wCAqCC"}
|
|
@ -99,4 +99,28 @@ ava_1.default("getExtraOptionsEnvParam() fails on invalid JSON", (t) => {
|
|||
t.throws(util.getExtraOptionsEnvParam);
|
||||
process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
|
||||
});
|
||||
ava_1.default("parseGithubUrl", (t) => {
|
||||
t.deepEqual(util.parseGithubUrl("github.com"), "https://github.com/");
|
||||
t.deepEqual(util.parseGithubUrl("https://github.com"), "https://github.com/");
|
||||
t.deepEqual(util.parseGithubUrl("https://api.github.com"), "https://github.com/");
|
||||
t.deepEqual(util.parseGithubUrl("https://github.com/foo/bar"), "https://github.com/");
|
||||
t.deepEqual(util.parseGithubUrl("github.example.com"), "https://github.example.com/");
|
||||
t.deepEqual(util.parseGithubUrl("https://github.example.com"), "https://github.example.com/");
|
||||
t.deepEqual(util.parseGithubUrl("https://api.github.example.com"), "https://github.example.com/");
|
||||
t.deepEqual(util.parseGithubUrl("https://github.example.com/api/v3"), "https://github.example.com/");
|
||||
t.deepEqual(util.parseGithubUrl("https://github.example.com:1234"), "https://github.example.com:1234/");
|
||||
t.deepEqual(util.parseGithubUrl("https://api.github.example.com:1234"), "https://github.example.com:1234/");
|
||||
t.deepEqual(util.parseGithubUrl("https://github.example.com:1234/api/v3"), "https://github.example.com:1234/");
|
||||
t.deepEqual(util.parseGithubUrl("https://github.example.com/base/path"), "https://github.example.com/base/path/");
|
||||
t.deepEqual(util.parseGithubUrl("https://github.example.com/base/path/api/v3"), "https://github.example.com/base/path/");
|
||||
t.throws(() => util.parseGithubUrl(""), {
|
||||
message: '"" is not a valid URL',
|
||||
});
|
||||
t.throws(() => util.parseGithubUrl("ssh://github.com"), {
|
||||
message: '"ssh://github.com" is not a http or https URL',
|
||||
});
|
||||
t.throws(() => util.parseGithubUrl("http:///::::433"), {
|
||||
message: '"http:///::::433" is not a valid URL',
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=util.test.js.map
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -12,32 +12,16 @@ import { Language, parseLanguage } from "./languages";
|
|||
import { getRunnerLogger } from "./logging";
|
||||
import { parseRepositoryNwo } from "./repository";
|
||||
import * as upload_lib from "./upload-lib";
|
||||
import { getAddSnippetsFlag, getMemoryFlag, getThreadsFlag } from "./util";
|
||||
import {
|
||||
getAddSnippetsFlag,
|
||||
getMemoryFlag,
|
||||
getThreadsFlag,
|
||||
parseGithubUrl,
|
||||
} from "./util";
|
||||
|
||||
const program = new Command();
|
||||
program.version("0.0.1");
|
||||
|
||||
function parseGithubUrl(inputUrl: string): string {
|
||||
try {
|
||||
const url = new URL(inputUrl);
|
||||
|
||||
// If we detect this is trying to be to github.com
|
||||
// then return with a fixed canonical URL.
|
||||
if (url.hostname === "github.com" || url.hostname === "api.github.com") {
|
||||
return "https://github.com";
|
||||
}
|
||||
|
||||
// Remove the API prefix if it's present
|
||||
if (url.pathname.indexOf("/api/v3") !== -1) {
|
||||
url.pathname = url.pathname.substring(0, url.pathname.indexOf("/api/v3"));
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
} catch (e) {
|
||||
throw new Error(`"${inputUrl}" is not a valid URL`);
|
||||
}
|
||||
}
|
||||
|
||||
function getTempDir(userInput: string | undefined): string {
|
||||
const tempDir = path.join(userInput || process.cwd(), "codeql-runner");
|
||||
if (!fs.existsSync(tempDir)) {
|
||||
|
|
|
@ -122,3 +122,63 @@ test("getExtraOptionsEnvParam() fails on invalid JSON", (t) => {
|
|||
|
||||
process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
|
||||
});
|
||||
|
||||
test("parseGithubUrl", (t) => {
|
||||
t.deepEqual(util.parseGithubUrl("github.com"), "https://github.com/");
|
||||
t.deepEqual(util.parseGithubUrl("https://github.com"), "https://github.com/");
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("https://api.github.com"),
|
||||
"https://github.com/"
|
||||
);
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("https://github.com/foo/bar"),
|
||||
"https://github.com/"
|
||||
);
|
||||
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("github.example.com"),
|
||||
"https://github.example.com/"
|
||||
);
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("https://github.example.com"),
|
||||
"https://github.example.com/"
|
||||
);
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("https://api.github.example.com"),
|
||||
"https://github.example.com/"
|
||||
);
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("https://github.example.com/api/v3"),
|
||||
"https://github.example.com/"
|
||||
);
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("https://github.example.com:1234"),
|
||||
"https://github.example.com:1234/"
|
||||
);
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("https://api.github.example.com:1234"),
|
||||
"https://github.example.com:1234/"
|
||||
);
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("https://github.example.com:1234/api/v3"),
|
||||
"https://github.example.com:1234/"
|
||||
);
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("https://github.example.com/base/path"),
|
||||
"https://github.example.com/base/path/"
|
||||
);
|
||||
t.deepEqual(
|
||||
util.parseGithubUrl("https://github.example.com/base/path/api/v3"),
|
||||
"https://github.example.com/base/path/"
|
||||
);
|
||||
|
||||
t.throws(() => util.parseGithubUrl(""), {
|
||||
message: '"" is not a valid URL',
|
||||
});
|
||||
t.throws(() => util.parseGithubUrl("ssh://github.com"), {
|
||||
message: '"ssh://github.com" is not a http or https URL',
|
||||
});
|
||||
t.throws(() => util.parseGithubUrl("http:///::::433"), {
|
||||
message: '"http:///::::433" is not a valid URL',
|
||||
});
|
||||
});
|
||||
|
|
43
src/util.ts
43
src/util.ts
|
@ -165,3 +165,46 @@ export function getCodeQLDatabasesDir(tempDir: string) {
|
|||
export function getCodeQLDatabasePath(tempDir: string, language: Language) {
|
||||
return path.resolve(getCodeQLDatabasesDir(tempDir), language);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses user input of a github.com or GHES URL to a canonical form.
|
||||
* Removes any API prefix or suffix if one is present.
|
||||
*/
|
||||
export function parseGithubUrl(inputUrl: string): string {
|
||||
const originalUrl = inputUrl;
|
||||
if (inputUrl.indexOf("://") === -1) {
|
||||
inputUrl = `https://${inputUrl}`;
|
||||
}
|
||||
if (!inputUrl.startsWith("http://") && !inputUrl.startsWith("https://")) {
|
||||
throw new Error(`"${originalUrl}" is not a http or https URL`);
|
||||
}
|
||||
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(inputUrl);
|
||||
} catch (e) {
|
||||
throw new Error(`"${originalUrl}" is not a valid URL`);
|
||||
}
|
||||
|
||||
// If we detect this is trying to be to github.com
|
||||
// then return with a fixed canonical URL.
|
||||
if (url.hostname === "github.com" || url.hostname === "api.github.com") {
|
||||
return "https://github.com/";
|
||||
}
|
||||
|
||||
// Remove the API prefix if it's present
|
||||
if (url.pathname.indexOf("/api/v3") !== -1) {
|
||||
url.pathname = url.pathname.substring(0, url.pathname.indexOf("/api/v3"));
|
||||
}
|
||||
// Also consider subdomain isolation on GHES
|
||||
if (url.hostname.startsWith("api.")) {
|
||||
url.hostname = url.hostname.substring(4);
|
||||
}
|
||||
|
||||
// Normalise path to having a trailing slash for consistency
|
||||
if (!url.pathname.endsWith("/")) {
|
||||
url.pathname = `${url.pathname}/`;
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче