Skip to content

Commit ecf7232

Browse files
committed
Sanitize inputs
1 parent 51bb5eb commit ecf7232

6 files changed

+81
-6
lines changed

Diff for: lib/start-proxy.js

+17-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/start-proxy.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/start-proxy.test.js

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/start-proxy.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/start-proxy.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,31 @@ test("getCredentials returns all credentials when no language specified", async
8080
);
8181
t.is(credentials.length, 3);
8282
});
83+
84+
test("getCredentials throws an error when non-printable characters are used", async (t) => {
85+
const invalidCredentials = [
86+
{ type: "nuget_feed", host: "1nuget.pkg.github.com", token: "abc\u0000" }, // Non-printable character in token
87+
{ type: "nuget_feed", host: "2nuget.pkg.github.com\u0001" }, // Non-printable character in host
88+
{ type: "nuget_feed", host: "3nuget.pkg.github.com", password: "ghi\u0002" }, // Non-printable character in password
89+
{ type: "nuget_feed", host: "4nuget.pkg.github.com", password: "ghi\x00" }, // Non-printable character in password
90+
];
91+
92+
for (const invalidCredential of invalidCredentials) {
93+
const credentialsInput = Buffer.from(
94+
JSON.stringify([invalidCredential]),
95+
).toString("base64");
96+
97+
t.throws(
98+
() =>
99+
startProxyExports.getCredentials(
100+
getRunnerLogger(true),
101+
undefined,
102+
credentialsInput,
103+
undefined,
104+
),
105+
{
106+
message: "Invalid credentials - fields must contain only printable characters",
107+
},
108+
);
109+
}
110+
});

Diff for: src/start-proxy.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,19 @@ export function getCredentials(
5151
}
5252

5353
// Parse and validate the credentials
54-
const parsed = JSON.parse(credentialsStr) as Credential[];
55-
const out: Credential[] = [];
54+
let parsed: Credential[];
55+
try {
56+
parsed = JSON.parse(credentialsStr) as Credential[];
57+
} catch (error) {
58+
// Don't log the error since it might contain sensitive information.
59+
logger.error("Failed to parse the credentials data.");
60+
throw new Error("Invalid credentials format.");
61+
}
62+
63+
let out: Credential[] = [];
5664
for (const e of parsed) {
5765
if (e.url === undefined && e.host === undefined) {
66+
// The proxy needs one of these to work. If both are defined, the url has the precedence.
5867
throw new Error("Invalid credentials - must specify host or url");
5968
}
6069

@@ -64,6 +73,15 @@ export function getCredentials(
6473
continue;
6574
}
6675

76+
77+
const isPrintable = (str: string | undefined): boolean => {
78+
return str ? /^[\x20-\x7E]*$/.test(str) : true;
79+
};
80+
81+
if (!isPrintable(e.type) || !isPrintable(e.host) || !isPrintable(e.url) || !isPrintable(e.username) || !isPrintable(e.password) || !isPrintable(e.token)) {
82+
throw new Error("Invalid credentials - fields must contain only printable characters");
83+
}
84+
6785
out.push({
6886
type: e.type,
6987
host: e.host,

0 commit comments

Comments
 (0)