mirror of
https://github.com/outline/outline.git
synced 2026-12-04 18:34:54 +03:00
fix: Restrict credential transports on auth routes (#14268)
* fix: Restrict credential transports on auth routes * fix: Skip disallowed transports when parsing the token
This commit is contained in:
1 parent
e61b7ef21c
commit
41ba5c9fcb
3 files changed
+56
-7
No files matched your search
@@ -22,6 +22,8 @@ type BaseAuthenticationOptions = {
|
||||
role?: UserRole;
|
||||
/** Type of authentication required to access the route. */
|
||||
type?: AuthenticationType | AuthenticationType[];
|
||||
/** Transports the token may be received from, defaults to all. */
|
||||
transports?: AuthTransport[];
|
||||
};
|
||||
|
||||
type AuthenticationOptions =
|
||||
@@ -101,10 +103,18 @@ export default function auth(options: AuthenticationOptions = {}) {
|
||||
* Parses the authentication token from the request context.
|
||||
*
|
||||
* @param ctx The application context containing the request information.
|
||||
* @param transports The transports to read the token from, defaults to all.
|
||||
* @returns An object containing the token and its transport method.
|
||||
*/
|
||||
export function parseAuthentication(ctx: AppContext): AuthInput {
|
||||
const authorizationHeader = ctx.request.get("authorization");
|
||||
export function parseAuthentication(
|
||||
ctx: AppContext,
|
||||
transports?: AuthTransport[]
|
||||
): AuthInput {
|
||||
const isAllowed = (transport: AuthTransport) =>
|
||||
!transports || transports.includes(transport);
|
||||
const authorizationHeader = isAllowed("header")
|
||||
? ctx.request.get("authorization")
|
||||
: undefined;
|
||||
|
||||
if (authorizationHeader) {
|
||||
const parts = authorizationHeader.split(" ");
|
||||
@@ -125,6 +135,7 @@ export function parseAuthentication(ctx: AppContext): AuthInput {
|
||||
);
|
||||
}
|
||||
} else if (
|
||||
isAllowed("body") &&
|
||||
ctx.request.body &&
|
||||
typeof ctx.request.body === "object" &&
|
||||
"token" in ctx.request.body
|
||||
@@ -133,12 +144,12 @@ export function parseAuthentication(ctx: AppContext): AuthInput {
|
||||
token: String(ctx.request.body.token),
|
||||
transport: "body",
|
||||
};
|
||||
} else if (ctx.request.query?.token) {
|
||||
} else if (isAllowed("query") && ctx.request.query?.token) {
|
||||
return {
|
||||
token: String(ctx.request.query.token),
|
||||
transport: "query",
|
||||
};
|
||||
} else {
|
||||
} else if (isAllowed("cookie")) {
|
||||
const accessToken = ctx.cookies.get("accessToken");
|
||||
if (accessToken) {
|
||||
return {
|
||||
@@ -164,7 +175,7 @@ async function validateAuthentication(
|
||||
service?: string;
|
||||
scope?: string[];
|
||||
}> {
|
||||
const { token, transport } = parseAuthentication(ctx);
|
||||
const { token, transport } = parseAuthentication(ctx, options.transports);
|
||||
|
||||
if (!token) {
|
||||
throw AuthenticationError("Authentication required");
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { addMonths } from "date-fns";
|
||||
import { buildUser, buildCollection } from "@server/test/factories";
|
||||
import {
|
||||
buildAdmin,
|
||||
buildApiKey,
|
||||
buildUser,
|
||||
buildCollection,
|
||||
} from "@server/test/factories";
|
||||
import {
|
||||
getTestServer,
|
||||
setCloudHosted,
|
||||
@@ -97,4 +102,36 @@ describe("auth provider routes", () => {
|
||||
const state = verifyOAuthState(location.searchParams.get("state") ?? "");
|
||||
expect(state.actorId).toEqual(user.id);
|
||||
});
|
||||
|
||||
it("should not use an API key in the query as the actor", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const apiKey = await buildApiKey({ userId: admin.id });
|
||||
const res = await server.get(`/auth/google?token=${apiKey.value}`, {
|
||||
redirect: "manual",
|
||||
});
|
||||
expect(res.status).toEqual(302);
|
||||
|
||||
const location = new URL(res.headers.get("location") ?? "");
|
||||
const state = verifyOAuthState(location.searchParams.get("state") ?? "");
|
||||
expect(state.actorId).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should prefer the session cookie over a token in the query", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const victim = await buildUser();
|
||||
const res = await server.get(
|
||||
`/auth/google?token=${admin.getTransferToken()}`,
|
||||
{
|
||||
redirect: "manual",
|
||||
headers: {
|
||||
Cookie: `accessToken=${victim.getSessionToken()}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
expect(res.status).toEqual(302);
|
||||
|
||||
const location = new URL(res.headers.get("location") ?? "");
|
||||
const state = verifyOAuthState(location.searchParams.get("state") ?? "");
|
||||
expect(state.actorId).toEqual(victim.id);
|
||||
});
|
||||
});
|
||||
@@ -26,7 +26,8 @@ void (async () => {
|
||||
)
|
||||
);
|
||||
|
||||
router.use(authMiddleware({ optional: true }));
|
||||
// The OAuth actor must come from the browser session, not a link.
|
||||
router.use(authMiddleware({ optional: true, transports: ["cookie"] }));
|
||||
for (const resolvedRouter of resolvedRouters) {
|
||||
if (resolvedRouter) {
|
||||
router.use("/", resolvedRouter.routes());
|
||||
|
||||
Reference in new issue
Block a user