Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/start-proxy.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/start-proxy.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions lib/start-proxy.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/start-proxy.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions src/start-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,36 @@ test("getCredentials returns all credentials when no language specified", async
);
t.is(credentials.length, 3);
});

test("getCredentials throws an error when non-printable characters are used", async (t) => {
const invalidCredentials = [
{ type: "nuget_feed", host: "1nuget.pkg.github.com", token: "abc\u0000" }, // Non-printable character in token
{ type: "nuget_feed", host: "2nuget.pkg.github.com\u0001" }, // Non-printable character in host
{
type: "nuget_feed",
host: "3nuget.pkg.github.com",
password: "ghi\u0002",
}, // Non-printable character in password
{ type: "nuget_feed", host: "4nuget.pkg.github.com", password: "ghi\x00" }, // Non-printable character in password
];

for (const invalidCredential of invalidCredentials) {
const credentialsInput = Buffer.from(
JSON.stringify([invalidCredential]),
).toString("base64");

t.throws(
() =>
startProxyExports.getCredentials(
getRunnerLogger(true),
undefined,
credentialsInput,
undefined,
),
Comment on lines +103 to +108

Check failure

Code scanning / CodeQL

Untrusted data passed to external API with additional heuristic sources

Call to ava/types/assertions.ThrowsAssertion()() \[callback 0 result\] with untrusted data from [e.password](1).
{
message:
"Invalid credentials - fields must contain only printable characters",
},
);
}
});
28 changes: 27 additions & 1 deletion src/start-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,19 @@ export function getCredentials(
}

// Parse and validate the credentials
const parsed = JSON.parse(credentialsStr) as Credential[];
let parsed: Credential[];
try {
parsed = JSON.parse(credentialsStr) as Credential[];
} catch {
// Don't log the error since it might contain sensitive information.
logger.error("Failed to parse the credentials data.");
throw new Error("Invalid credentials format.");
Comment thread
marcogario marked this conversation as resolved.
Outdated
}

const out: Credential[] = [];
for (const e of parsed) {
if (e.url === undefined && e.host === undefined) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it also an error if both url and host are defined?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure, I believe the proxy will use the url in that case. I will double check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment.

// The proxy needs one of these to work. If both are defined, the url has the precedence.
throw new Error("Invalid credentials - must specify host or url");
}

Expand All @@ -64,6 +73,23 @@ export function getCredentials(
continue;
}

const isPrintable = (str: string | undefined): boolean => {
return str ? /^[\x20-\x7E]*$/.test(str) : true;
};

if (
!isPrintable(e.type) ||
!isPrintable(e.host) ||
!isPrintable(e.url) ||
!isPrintable(e.username) ||
!isPrintable(e.password) ||
!isPrintable(e.token)
) {
throw new Error(
Comment thread
marcogario marked this conversation as resolved.
Outdated
"Invalid credentials - fields must contain only printable characters",
);
}

out.push({
type: e.type,
host: e.host,
Expand Down