Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect more auth headers and cookies #380

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
54 changes: 54 additions & 0 deletions library/agent/api-discovery/getApiAuthType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ t.test("it detects api keys", async (t) => {
]);
});

t.test("it doesn't add the same api key again", async () => {
t.same(get(getContext({ "api-key": "token", "api-key-a": "token" })), [
{ type: "apiKey", in: "header", name: "api-key" },
{ type: "apiKey", in: "header", name: "api-key-a" },
]);
t.same(get(getContext({ "api-key": "token", "api-key-apikey": "token" })), [
{ type: "apiKey", in: "header", name: "api-key" },
{ type: "apiKey", in: "header", name: "api-key-apikey" },
]);
});

t.test("it detects auth cookies", async (t) => {
t.same(get(getContext({}, { "api-key": "token" })), [
{ type: "apiKey", in: "cookie", name: "api-key" },
Expand All @@ -54,6 +65,49 @@ t.test("it detects auth cookies", async (t) => {
]);
});

t.test("it detects multiple auth cookies", async (t) => {
t.same(get(getContext({}, { "api-key": "token", "api-key-a": "token" })), [
{ type: "apiKey", in: "cookie", name: "api-key" },
{ type: "apiKey", in: "cookie", name: "api-key-a" },
]);
t.same(get(getContext({}, { "api-key": "token", "api-key-sid": "token" })), [
{ type: "apiKey", in: "cookie", name: "api-key-sid" },
{ type: "apiKey", in: "cookie", name: "api-key" },
]);
});

t.test("it detects cookies and api keys at same time", async (t) => {
t.same(
get(
getContext(
{
"api-key": "token",
},
{ "api-key": "token" }
)
),
[
{ type: "apiKey", in: "header", name: "api-key" },
{ type: "apiKey", in: "cookie", name: "api-key" },
]
);

t.same(
get(
getContext(
{
"x-api-key": "token",
},
{ "api-key": "token" }
)
),
[
{ type: "apiKey", in: "header", name: "x-api-key" },
{ type: "apiKey", in: "cookie", name: "api-key" },
]
);
});

t.test("no auth", async (t) => {
t.same(get(getContext()), undefined);
t.same(get(getContext({})), undefined);
Expand Down
50 changes: 29 additions & 21 deletions library/agent/api-discovery/getApiAuthType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,13 @@ export type APIAuthType = {
};

// Incoming request headers are lowercase in Node.js
const commonApiKeyHeaderNames = [
"x-api-key",
"api-key",
"apikey",
"x-token",
"token",
];
const commonApiKeyHeaderNames = ["api-key", "apikey", "token"];

const commonAuthCookieNames = [
"auth",
"session",
"jwt",
"token",
"sid",
"connect.sid",
"auth_token",
"access_token",
"refresh_token",
...commonApiKeyHeaderNames,
];

Expand Down Expand Up @@ -82,12 +71,13 @@ function getAuthorizationHeaderType(
if (authHeader.includes(" ")) {
const [type, value] = authHeader.split(" ");

if (typeof type === "string" && typeof value === "string") {
if (type && value) {
if (isHTTPAuthScheme(type)) {
return { type: "http", scheme: type.toLowerCase() as HTTPAuthScheme };
}
}
}

// Default to apiKey if the auth type is not recognized
return { type: "apiKey", in: "header", name: "Authorization" };
}
Expand All @@ -98,10 +88,20 @@ function getAuthorizationHeaderType(
function findApiKeys(context: Context): APIAuthType[] {
const result: APIAuthType[] = [];

const headerNames = Object.keys(context.headers);
for (const header of commonApiKeyHeaderNames) {
if (context.headers[header]) {
result.push({ type: "apiKey", in: "header", name: header });
}
const matches = headerNames.filter((name) =>
name.toLowerCase().includes(header.toLowerCase())
hansott marked this conversation as resolved.
Show resolved Hide resolved
hansott marked this conversation as resolved.
Show resolved Hide resolved
);

matches.forEach((match) => {
const alreadyAdded = result.some(
(authType) => authType.name === match && authType.in === "header"
);
if (!alreadyAdded) {
result.push({ type: "apiKey", in: "header", name: match });
}
});
}

if (
Expand All @@ -110,11 +110,19 @@ function findApiKeys(context: Context): APIAuthType[] {
!Array.isArray(context.cookies) &&
Object.keys(context.cookies).length > 0
) {
const relevantCookies = Object.keys(context.cookies).filter((cookieName) =>
commonAuthCookieNames.includes(cookieName.toLowerCase())
);
for (const cookie of relevantCookies) {
result.push({ type: "apiKey", in: "cookie", name: cookie });
for (const cookie of commonAuthCookieNames) {
const matches = Object.keys(context.cookies).filter((name) =>
name.toLowerCase().includes(cookie.toLowerCase())
);

matches.forEach((match) => {
const alreadyAdded = result.some(
(authType) => authType.name === match && authType.in === "cookie"
);
if (!alreadyAdded) {
result.push({ type: "apiKey", in: "cookie", name: match });
}
});
}
}

Expand Down
Loading