Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion apps/mail-bridge/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@ import { opentelemetry } from '@u22n/otel/hono';
import { trpcMailBridgeRouter } from './trpc';
import type { Ctx, TRPCContext } from './ctx';
import { db } from '@u22n/database';
import { timingSafeEqual, createHash } from 'crypto';
import { env } from './env';

function safeEqual(a: string, b: string): boolean {
try {
const hashA = createHash('sha256').update(a).digest();
const hashB = createHash('sha256').update(b).digest();
return timingSafeEqual(hashA, hashB);
} catch {
return false;
}
}

const processCleanup: Array<() => Promise<void>> = [];

if (env.MAILBRIDGE_MODE === 'dual' || env.MAILBRIDGE_MODE === 'handler') {
Expand All @@ -31,7 +42,7 @@ if (env.MAILBRIDGE_MODE === 'dual' || env.MAILBRIDGE_MODE === 'handler') {

setupTrpcHandler(app, trpcMailBridgeRouter, (_, c) => {
const authToken = c.req.header('Authorization');
const isServiceAuthenticated = authToken === env.MAILBRIDGE_KEY;
const isServiceAuthenticated = authToken ? safeEqual(authToken, env.MAILBRIDGE_KEY) : false;
return {
auth: isServiceAuthenticated,
db,
Expand Down
13 changes: 12 additions & 1 deletion ee/apps/billing/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@ import { trpcBillingRouter } from './trpc';
import { stripeData } from './stripe';
import { db } from '@u22n/database';
import { type Ctx } from './ctx';
import { timingSafeEqual, createHash } from 'crypto';
import { env } from './env';

function safeEqual(a: string, b: string): boolean {
try {
const hashA = createHash('sha256').update(a).digest();
const hashB = createHash('sha256').update(b).digest();
return timingSafeEqual(hashA, hashB);
} catch {
return false;
}
}

await validateLicense();

const app = createHonoApp<Ctx>();
Expand All @@ -28,7 +39,7 @@ setupErrorHandlers(app);
setupTrpcHandler(app, trpcBillingRouter, (_, c) => {
const authToken = c.req.header('Authorization');
return {
auth: authToken === env.BILLING_KEY,
auth: authToken ? safeEqual(authToken, env.BILLING_KEY) : false,
stripe: stripeData,
db
};
Expand Down