Critical Securities (5) fixes

This commit is contained in:
2026-02-25 19:44:38 +00:00
parent 10acd14755
commit 5d4f0114cd
14 changed files with 302 additions and 103 deletions
+1
View File
@@ -3,3 +3,4 @@ export * from './user-middleware';
export * from './origin-middleware';
export * from './origin-validation';
export * from './rate-limiter';
export * from './super-admin-middleware';
+17 -16
View File
@@ -3,38 +3,39 @@ import * as errors from '../custom-errors';
const { PUBLIC_BUILD_ENV } = process.env;
const ALLOWED_ORIGINS: Record<string, string[]> = {
staging: ['https://staging.officer.dev'],
const WEB_ORIGINS: Record<string, string[]> = {
alpha: ['https://alpha.officer.dev'],
production: ['https://app.officer.dev', 'https://edge.officer.dev'],
};
const CHROME_EXTENSIONS: string[] = [
// 'chrome-extension://<id>'
];
const APP_ORIGINS: string[] = [
// Future: mobile app / webview origins
];
export function isOriginAllowed(origin: string | undefined, host?: string): boolean {
// Dev environment: allow any origin
if (!PUBLIC_BUILD_ENV || PUBLIC_BUILD_ENV === 'dev' || PUBLIC_BUILD_ENV === 'development') {
return true;
}
// If origin is present, validate it
if (origin) {
// Chrome extension check
if (origin.startsWith('chrome-extension://')) {
if (PUBLIC_BUILD_ENV === 'staging') {
return true; // Allow any extension in staging
}
if (PUBLIC_BUILD_ENV === 'production') {
return origin === 'chrome-extension://fjooappefigppfjolhadliepialebodg';
}
return CHROME_EXTENSIONS.includes(origin);
}
// Web origin check
const allowed = ALLOWED_ORIGINS[PUBLIC_BUILD_ENV];
if (APP_ORIGINS.includes(origin)) {
return true;
}
const allowed = WEB_ORIGINS[PUBLIC_BUILD_ENV];
return !!allowed?.includes(origin);
}
// No origin header: allow same-origin requests by checking Host header
// This handles cases where browsers don't send Origin for same-origin requests
if (host) {
const allowed = ALLOWED_ORIGINS[PUBLIC_BUILD_ENV];
const allowed = WEB_ORIGINS[PUBLIC_BUILD_ENV];
return !!allowed?.some((o) => o.endsWith(host));
}
@@ -0,0 +1,8 @@
import type { MiddlewareHandler } from 'hono';
import * as errors from '@@/custom-errors';
export const superAdminMiddleware: MiddlewareHandler = function (ctx, next) {
const user = ctx.get('user');
if (user?.role !== 'Super Admin') throw errors.FORBIDDEN();
return next();
};