fix: Prevent OAuth tokens from creating webhooks

This commit is contained in:
Tom Moor committed 2025-04-07 20:47:29 -04:00
1 parent 1b90ab85e7
commit 7fc305b5d5
4 files changed
+53 -8

No files matched your search

@@ -8,7 +8,7 @@ import validate from "@server/middlewares/validate";
import { WebhookSubscription } from "@server/models";
import { authorize } from "@server/policies";
import pagination from "@server/routes/api/middlewares/pagination";
import { APIContext } from "@server/types";
import { APIContext, AuthenticationType } from "@server/types";
import presentWebhookSubscription from "../presenters/webhookSubscription";
import * as T from "./schema";
@@ -41,7 +41,10 @@ router.post(
router.post(
"webhookSubscriptions.create",
auth({ role: UserRole.Admin }),
auth({
role: UserRole.Admin,
type: [AuthenticationType.API, AuthenticationType.APP],
}),
validate(T.WebhookSubscriptionsCreateSchema),
transaction(),
async (ctx: APIContext<T.WebhookSubscriptionsCreateReq>) => {
@@ -68,7 +71,10 @@ router.post(
router.post(
"webhookSubscriptions.delete",
auth({ role: UserRole.Admin }),
auth({
role: UserRole.Admin,
type: [AuthenticationType.API, AuthenticationType.APP],
}),
validate(T.WebhookSubscriptionsDeleteSchema),
transaction(),
async (ctx: APIContext<T.WebhookSubscriptionsDeleteReq>) => {
@@ -94,7 +100,10 @@ router.post(
router.post(
"webhookSubscriptions.update",
auth({ role: UserRole.Admin }),
auth({
role: UserRole.Admin,
type: [AuthenticationType.API, AuthenticationType.APP],
}),
validate(T.WebhookSubscriptionsUpdateSchema),
transaction(),
async (ctx: APIContext<T.WebhookSubscriptionsUpdateReq>) => {
+25
View File
@@ -8,6 +8,7 @@ import {
buildApiKey,
buildOAuthAuthentication,
} from "@server/test/factories";
import { AuthenticationType } from "@server/types";
import auth from "./authentication";
describe("Authentication middleware", () => {
@@ -51,6 +52,30 @@ describe("Authentication middleware", () => {
expect(e.message).toBe("Invalid token");
}
});
it("should return error if AuthenticationType mismatches", async () => {
const state = {} as DefaultState;
const user = await buildUser();
const authMiddleware = auth({
type: AuthenticationType.API,
});
try {
await authMiddleware(
{
// @ts-expect-error mock request
request: {
get: jest.fn(() => `Bearer ${user.getJwtToken()}`),
},
state,
cache: {},
},
jest.fn()
);
} catch (e) {
expect(e.message).toBe("Invalid authentication type");
}
});
});
describe("with API key", () => {
+7 -2
View File
@@ -20,7 +20,7 @@ type AuthenticationOptions = {
/** Role requuired to access the route. */
role?: UserRole;
/** Type of authentication required to access the route. */
type?: AuthenticationType;
type?: AuthenticationType | AuthenticationType[];
/** Authentication is parsed, but optional. */
optional?: boolean;
};
@@ -168,7 +168,12 @@ export default function auth(options: AuthenticationOptions = {}) {
throw AuthorizationError(`${capitalize(options.role)} role required`);
}
if (options.type && type !== options.type) {
if (
options.type &&
(Array.isArray(options.type)
? !options.type.includes(type)
: type !== options.type)
) {
throw AuthorizationError(`Invalid authentication type`);
}
+8 -2
View File
@@ -15,7 +15,10 @@ const router = new Router();
router.post(
"apiKeys.create",
auth({ role: UserRole.Member, type: AuthenticationType.APP }),
auth({
role: UserRole.Member,
type: AuthenticationType.APP,
}),
validate(T.APIKeysCreateSchema),
transaction(),
async (ctx: APIContext<T.APIKeysCreateReq>) => {
@@ -90,7 +93,10 @@ router.post(
router.post(
"apiKeys.delete",
auth({ role: UserRole.Member }),
auth({
role: UserRole.Member,
type: AuthenticationType.APP,
}),
validate(T.APIKeysDeleteSchema),
transaction(),
async (ctx: APIContext<T.APIKeysDeleteReq>) => {