diff --git a/lib/start-proxy.js b/lib/start-proxy.js index 66d50ed551..60a4eeb053 100644 --- a/lib/start-proxy.js +++ b/lib/start-proxy.js @@ -50,6 +50,14 @@ const LANGUAGE_TO_REGISTRY_TYPE = { cpp: "", swift: "", }; +/** + * Checks that `value` is neither `undefined` nor `null`. + * @param value The value to test. + * @returns Narrows the type of `value` to exclude `undefined` and `null`. + */ +function isDefined(value) { + return value !== undefined && value !== null; +} // getCredentials returns registry credentials from action inputs. // It prefers `registries_credentials` over `registry_secrets`. // If neither is set, it returns an empty array. @@ -81,16 +89,23 @@ function getCredentials(logger, registrySecrets, registriesCredentials, language logger.error("Failed to parse the credentials data."); throw new util_1.ConfigurationError("Invalid credentials format."); } + // Check that the parsed data is indeed an array. + if (!Array.isArray(parsed)) { + throw new util_1.ConfigurationError("Expected credentials data to be an array of configurations, but it is not."); + } const out = []; for (const e of parsed) { + if (e === null || typeof e !== "object") { + throw new util_1.ConfigurationError("Invalid credentials - must be an object"); + } // Mask credentials to reduce chance of accidental leakage in logs. - if (e.password !== undefined) { + if (isDefined(e.password)) { core.setSecret(e.password); } - if (e.token !== undefined) { + if (isDefined(e.token)) { core.setSecret(e.token); } - if (e.url === undefined && e.host === undefined) { + if (!isDefined(e.url) && !isDefined(e.host)) { // The proxy needs one of these to work. If both are defined, the url has the precedence. throw new util_1.ConfigurationError("Invalid credentials - must specify host or url"); } diff --git a/lib/start-proxy.js.map b/lib/start-proxy.js.map index d6cc42f1b0..1fa39a9993 100644 --- a/lib/start-proxy.js.map +++ b/lib/start-proxy.js.map @@ -1 +1 @@ -{"version":3,"file":"start-proxy.js","sourceRoot":"","sources":["../src/start-proxy.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,wCAmFC;AAnHD,oDAAsC;AAEtC,2CAAsD;AAEtD,iCAA4C;AAW5C,MAAM,yBAAyB,GAA6B;IAC1D,IAAI,EAAE,kBAAkB;IACxB,MAAM,EAAE,YAAY;IACp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CAAC,QAAQ;YACpB,QAAQ,EAAE,CAAC,CAAC,QAAQ;YACpB,KAAK,EAAE,CAAC,CAAC,KAAK;SACf,CAAC,CAAC;IACL,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"} \ No newline at end of file diff --git a/lib/start-proxy.test.js b/lib/start-proxy.test.js index b4d53aad87..f7ad5ddbae 100644 --- a/lib/start-proxy.test.js +++ b/lib/start-proxy.test.js @@ -41,6 +41,7 @@ const logging_1 = require("./logging"); const startProxyExports = __importStar(require("./start-proxy")); const testing_utils_1 = require("./testing-utils"); (0, testing_utils_1.setupTests)(ava_1.default); +const toEncodedJSON = (data) => Buffer.from(JSON.stringify(data)).toString("base64"); (0, ava_1.default)("getCredentials prefers registriesCredentials over registrySecrets", async (t) => { const registryCredentials = Buffer.from(JSON.stringify([ { type: "npm_registry", host: "npm.pkg.github.com", token: "abc" }, @@ -52,19 +53,40 @@ const testing_utils_1 = require("./testing-utils"); t.is(credentials.length, 1); t.is(credentials[0].host, "npm.pkg.github.com"); }); -(0, ava_1.default)("getCredentials throws error when credential missing host and url", async (t) => { - const registryCredentials = Buffer.from(JSON.stringify([{ type: "npm_registry", token: "abc" }])).toString("base64"); +(0, ava_1.default)("getCredentials throws an error when configurations are not an array", async (t) => { + const registryCredentials = Buffer.from(JSON.stringify({ type: "npm_registry", token: "abc" })).toString("base64"); t.throws(() => startProxyExports.getCredentials((0, logging_1.getRunnerLogger)(true), undefined, registryCredentials, undefined), { - message: "Invalid credentials - must specify host or url", + message: "Expected credentials data to be an array of configurations, but it is not.", }); }); +(0, ava_1.default)("getCredentials throws error when credential is not an object", async (t) => { + const testCredentials = [["foo"], [null]].map(toEncodedJSON); + for (const testCredential of testCredentials) { + t.throws(() => startProxyExports.getCredentials((0, logging_1.getRunnerLogger)(true), undefined, testCredential, undefined), { + message: "Invalid credentials - must be an object", + }); + } +}); +(0, ava_1.default)("getCredentials throws error when credential missing host and url", async (t) => { + const testCredentials = [ + [{ type: "npm_registry", token: "abc" }], + [{ type: "npm_registry", token: "abc", host: null }], + [{ type: "npm_registry", token: "abc", url: null }], + ].map(toEncodedJSON); + for (const testCredential of testCredentials) { + t.throws(() => startProxyExports.getCredentials((0, logging_1.getRunnerLogger)(true), undefined, testCredential, undefined), { + message: "Invalid credentials - must specify host or url", + }); + } +}); (0, ava_1.default)("getCredentials filters by language when specified", async (t) => { const mixedCredentials = [ { type: "npm_registry", host: "npm.pkg.github.com", token: "abc" }, { type: "maven_repository", host: "maven.pkg.github.com", token: "def" }, { type: "nuget_feed", host: "nuget.pkg.github.com", token: "ghi" }, + { type: "goproxy_server", host: "goproxy.example.com", token: "jkl" }, ]; - const credentials = startProxyExports.getCredentials((0, logging_1.getRunnerLogger)(true), undefined, Buffer.from(JSON.stringify(mixedCredentials)).toString("base64"), "java"); + const credentials = startProxyExports.getCredentials((0, logging_1.getRunnerLogger)(true), undefined, toEncodedJSON(mixedCredentials), "java"); t.is(credentials.length, 1); t.is(credentials[0].type, "maven_repository"); }); @@ -73,10 +95,11 @@ const testing_utils_1 = require("./testing-utils"); { type: "npm_registry", host: "npm.pkg.github.com", token: "abc" }, { type: "maven_repository", host: "maven.pkg.github.com", token: "def" }, { type: "nuget_feed", host: "nuget.pkg.github.com", token: "ghi" }, + { type: "goproxy_server", host: "goproxy.example.com", token: "jkl" }, ]; - const credentialsInput = Buffer.from(JSON.stringify(mixedCredentials)).toString("base64"); + const credentialsInput = toEncodedJSON(mixedCredentials); const credentials = startProxyExports.getCredentials((0, logging_1.getRunnerLogger)(true), undefined, credentialsInput, undefined); - t.is(credentials.length, 3); + t.is(credentials.length, mixedCredentials.length); }); (0, ava_1.default)("getCredentials throws an error when non-printable characters are used", async (t) => { const invalidCredentials = [ diff --git a/lib/start-proxy.test.js.map b/lib/start-proxy.test.js.map index c9ca771a5c..6e3758784e 100644 --- a/lib/start-proxy.test.js.map +++ b/lib/start-proxy.test.js.map @@ -1 +1 @@ -{"version":3,"file":"start-proxy.test.js","sourceRoot":"","sources":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yBAAe,EAAC,IAAI,CAAC,EACrB,SAAS,EACT,gBAAgB,EAChB,SAAS,CACV,EACH;YACE,OAAO,EACL,qEAAqE;SACxE,CACF,CAAC;IACJ,CAAC;AACH,CAAC,CAAC,CAAC"} \ No newline at end of file +{"version":3,"file":"start-proxy.test.js","sourceRoot":"","sources":["../src/start-proxy.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8CAAuB;AAEvB,uCAA4C;AAC5C,iEAAmD;AACnD,mDAA6C;AAE7C,IAAA,0BAAU,EAAC,aAAI,CAAC,CAAC;AAEjB,MAAM,aAAa,GAAG,CAAC,IAAS,EAAE,EAAE,CAClC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAEvD,IAAA,aAAI,EAAC,mEAAmE,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE;IACpF,MAAM,mBAAmB,GAAG,MAAM,CAAC,IAAI,CACrC,IAAI,CAAC,SAAS,CAAC;QACb,EAAE,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,oBAAoB,EAAE,KAAK,EAAE,KAAK,EAAE;KACnE,CAAC,CACH,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACrB,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC;QACrC,EAAE,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,oBAAoB,EAAE,KAAK,EAAE,KAAK,EAAE;KACnE,CAAC,CAAC;IAEH,MAAM,WAAW,GAAG,iBAAiB,CAAC,cAAc,CAClD,IAAA,yBAAe,EAAC,IAAI,CAAC,EACrB,eAAe,EACf,mBAAmB,EACnB,SAAS,CACV,CAAC;IACF,CAAC,CAAC,EAAE,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAC5B,CAAC,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC,CA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sC;KACnH,CAAC;IAEF,KAAK,MAAM,iBAAiB,IAAI,kBAAkB,EAAE,CAAC;QACnD,MAAM,gBAAgB,GAAG,MAAM,CAAC,IAAI,CAClC,IAAI,CAAC,SAAS,CAAC,CAAC,iBAAiB,CAAC,CAAC,CACpC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAErB,CAAC,CAAC,MAAM,CACN,GAAG,EAAE,CACH,iBAAiB,CAAC,cAAc,CAC9B,IAAA,yBAAe,EAAC,IAAI,CAAC,EACrB,SAAS,EACT,gBAAgB,EAChB,SAAS,CACV,EACH;YACE,OAAO,EACL,qEAAqE;SACxE,CACF,CAAC;IACJ,CAAC;AACH,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/src/start-proxy.test.ts b/src/start-proxy.test.ts index cb84833cc6..edd29ea619 100644 --- a/src/start-proxy.test.ts +++ b/src/start-proxy.test.ts @@ -6,6 +6,9 @@ import { setupTests } from "./testing-utils"; setupTests(test); +const toEncodedJSON = (data: any) => + Buffer.from(JSON.stringify(data)).toString("base64"); + test("getCredentials prefers registriesCredentials over registrySecrets", async (t) => { const registryCredentials = Buffer.from( JSON.stringify([ @@ -26,9 +29,9 @@ test("getCredentials prefers registriesCredentials over registrySecrets", async t.is(credentials[0].host, "npm.pkg.github.com"); }); -test("getCredentials throws error when credential missing host and url", async (t) => { +test("getCredentials throws an error when configurations are not an array", async (t) => { const registryCredentials = Buffer.from( - JSON.stringify([{ type: "npm_registry", token: "abc" }]), + JSON.stringify({ type: "npm_registry", token: "abc" }), ).toString("base64"); t.throws( @@ -40,22 +43,66 @@ test("getCredentials throws error when credential missing host and url", async ( undefined, ), { - message: "Invalid credentials - must specify host or url", + message: + "Expected credentials data to be an array of configurations, but it is not.", }, ); }); +test("getCredentials throws error when credential is not an object", async (t) => { + const testCredentials = [["foo"], [null]].map(toEncodedJSON); + + for (const testCredential of testCredentials) { + t.throws( + () => + startProxyExports.getCredentials( + getRunnerLogger(true), + undefined, + testCredential, + undefined, + ), + { + message: "Invalid credentials - must be an object", + }, + ); + } +}); + +test("getCredentials throws error when credential missing host and url", async (t) => { + const testCredentials = [ + [{ type: "npm_registry", token: "abc" }], + [{ type: "npm_registry", token: "abc", host: null }], + [{ type: "npm_registry", token: "abc", url: null }], + ].map(toEncodedJSON); + + for (const testCredential of testCredentials) { + t.throws( + () => + startProxyExports.getCredentials( + getRunnerLogger(true), + undefined, + testCredential, + undefined, + ), + { + message: "Invalid credentials - must specify host or url", + }, + ); + } +}); + test("getCredentials filters by language when specified", async (t) => { const mixedCredentials = [ { type: "npm_registry", host: "npm.pkg.github.com", token: "abc" }, { type: "maven_repository", host: "maven.pkg.github.com", token: "def" }, { type: "nuget_feed", host: "nuget.pkg.github.com", token: "ghi" }, + { type: "goproxy_server", host: "goproxy.example.com", token: "jkl" }, ]; const credentials = startProxyExports.getCredentials( getRunnerLogger(true), undefined, - Buffer.from(JSON.stringify(mixedCredentials)).toString("base64"), + toEncodedJSON(mixedCredentials), "java", ); t.is(credentials.length, 1); @@ -67,10 +114,9 @@ test("getCredentials returns all credentials when no language specified", async { type: "npm_registry", host: "npm.pkg.github.com", token: "abc" }, { type: "maven_repository", host: "maven.pkg.github.com", token: "def" }, { type: "nuget_feed", host: "nuget.pkg.github.com", token: "ghi" }, + { type: "goproxy_server", host: "goproxy.example.com", token: "jkl" }, ]; - const credentialsInput = Buffer.from( - JSON.stringify(mixedCredentials), - ).toString("base64"); + const credentialsInput = toEncodedJSON(mixedCredentials); const credentials = startProxyExports.getCredentials( getRunnerLogger(true), @@ -78,7 +124,7 @@ test("getCredentials returns all credentials when no language specified", async credentialsInput, undefined, ); - t.is(credentials.length, 3); + t.is(credentials.length, mixedCredentials.length); }); test("getCredentials throws an error when non-printable characters are used", async (t) => { diff --git a/src/start-proxy.ts b/src/start-proxy.ts index cc9fcb9a75..6924fe7154 100644 --- a/src/start-proxy.ts +++ b/src/start-proxy.ts @@ -27,6 +27,15 @@ const LANGUAGE_TO_REGISTRY_TYPE: Record = { swift: "", } as const; +/** + * Checks that `value` is neither `undefined` nor `null`. + * @param value The value to test. + * @returns Narrows the type of `value` to exclude `undefined` and `null`. + */ +function isDefined(value: T | null | undefined): value is T { + return value !== undefined && value !== null; +} + // getCredentials returns registry credentials from action inputs. // It prefers `registries_credentials` over `registry_secrets`. // If neither is set, it returns an empty array. @@ -63,17 +72,28 @@ export function getCredentials( throw new ConfigurationError("Invalid credentials format."); } + // Check that the parsed data is indeed an array. + if (!Array.isArray(parsed)) { + throw new ConfigurationError( + "Expected credentials data to be an array of configurations, but it is not.", + ); + } + const out: Credential[] = []; for (const e of parsed) { + if (e === null || typeof e !== "object") { + throw new ConfigurationError("Invalid credentials - must be an object"); + } + // Mask credentials to reduce chance of accidental leakage in logs. - if (e.password !== undefined) { + if (isDefined(e.password)) { core.setSecret(e.password); } - if (e.token !== undefined) { + if (isDefined(e.token)) { core.setSecret(e.token); } - if (e.url === undefined && e.host === undefined) { + if (!isDefined(e.url) && !isDefined(e.host)) { // The proxy needs one of these to work. If both are defined, the url has the precedence. throw new ConfigurationError( "Invalid credentials - must specify host or url",