mirror of
https://github.com/shuaiplus/nodewarden.git
synced 2026-08-05 06:50:10 +00:00
feat: add Web Vault visibility switch
This commit is contained in:
+12
-17
@@ -5,6 +5,11 @@ import { handleRequest } from './router';
|
||||
import { StorageService } from './services/storage';
|
||||
import { applyCors, jsonResponse } from './utils/response';
|
||||
import { runScheduledBackupIfDue } from './handlers/backup';
|
||||
import {
|
||||
isBackendRequestPath,
|
||||
isWebVaultHidden,
|
||||
webVaultNotFoundResponse,
|
||||
} from './web-vault-visibility';
|
||||
|
||||
let dbInitialized = false;
|
||||
let dbInitError: string | null = null;
|
||||
@@ -19,22 +24,6 @@ function normalizeRequestUrl(request: Request): Request {
|
||||
return new Request(url.toString(), request);
|
||||
}
|
||||
|
||||
function isWorkerHandledPath(path: string): boolean {
|
||||
return (
|
||||
path.startsWith('/api/') ||
|
||||
path.startsWith('/identity/') ||
|
||||
path.startsWith('/icons/') ||
|
||||
path.startsWith('/fill-assist/') ||
|
||||
path.startsWith('/notifications/') ||
|
||||
path.startsWith('/.well-known/') ||
|
||||
path === '/v1/assetlinks:check' ||
|
||||
path === '/web-bootstrap' ||
|
||||
path === '/config' ||
|
||||
path === '/api/config' ||
|
||||
path === '/api/version'
|
||||
);
|
||||
}
|
||||
|
||||
function addSearchIndexHeaders(request: Request, response: Response): Response {
|
||||
const url = new URL(request.url);
|
||||
const contentType = String(response.headers.get('Content-Type') || '').toLowerCase();
|
||||
@@ -58,7 +47,7 @@ async function maybeServeAsset(request: Request, env: Env): Promise<Response | n
|
||||
if (!env.ASSETS) return null;
|
||||
if (request.method !== 'GET' && request.method !== 'HEAD') return null;
|
||||
const url = new URL(request.url);
|
||||
if (isWorkerHandledPath(url.pathname)) return null;
|
||||
if (isBackendRequestPath(url.pathname)) return null;
|
||||
|
||||
const response = await env.ASSETS.fetch(request);
|
||||
return addSearchIndexHeaders(request, response);
|
||||
@@ -90,6 +79,12 @@ export default {
|
||||
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
||||
void ctx;
|
||||
const normalizedRequest = normalizeRequestUrl(request);
|
||||
const requestPath = new URL(normalizedRequest.url).pathname;
|
||||
|
||||
if (isWebVaultHidden(env) && !isBackendRequestPath(requestPath)) {
|
||||
return webVaultNotFoundResponse(normalizedRequest);
|
||||
}
|
||||
|
||||
const assetResponse = await maybeServeAsset(normalizedRequest, env);
|
||||
if (assetResponse) {
|
||||
return applyCors(normalizedRequest, assetResponse, env);
|
||||
|
||||
@@ -6,6 +6,8 @@ export interface Env {
|
||||
ASSETS?: {
|
||||
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
||||
};
|
||||
// Set to "1" to return 404 for the Web Vault while keeping client APIs available.
|
||||
HIDE_WEB_VAULT?: string;
|
||||
// Prefer R2 when available. Optional to support KV-only deployments.
|
||||
ATTACHMENTS?: R2Bucket;
|
||||
// Optional fallback for attachment/send file storage (no credit card required).
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import type { Env } from './types';
|
||||
|
||||
const BACKEND_PATH_PREFIXES = [
|
||||
'/api',
|
||||
'/identity',
|
||||
'/icons',
|
||||
'/fill-assist',
|
||||
'/notifications',
|
||||
'/.well-known',
|
||||
// Compatibility aliases retained for older Bitwarden clients.
|
||||
'/devices',
|
||||
'/auth-requests',
|
||||
'/webauthn',
|
||||
] as const;
|
||||
|
||||
const BACKEND_EXACT_PATHS = new Set([
|
||||
'/v1/assetlinks:check',
|
||||
'/web-bootstrap',
|
||||
'/config',
|
||||
'/accounts/kdf',
|
||||
'/settings/domains',
|
||||
]);
|
||||
|
||||
export function isBackendRequestPath(pathname: string): boolean {
|
||||
const path = pathname.toLowerCase();
|
||||
if (BACKEND_EXACT_PATHS.has(path)) return true;
|
||||
|
||||
return BACKEND_PATH_PREFIXES.some((prefix) => (
|
||||
path === prefix || path.startsWith(`${prefix}/`)
|
||||
));
|
||||
}
|
||||
|
||||
export function isWebVaultHidden(env: Env): boolean {
|
||||
return String(env.HIDE_WEB_VAULT || '').trim() === '1';
|
||||
}
|
||||
|
||||
export function webVaultNotFoundResponse(request: Request): Response {
|
||||
const body = request.method === 'HEAD' ? null : 'Not Found';
|
||||
return new Response(body, {
|
||||
status: 404,
|
||||
headers: {
|
||||
'Cache-Control': 'no-store, max-age=0',
|
||||
'Content-Type': 'text/plain; charset=utf-8',
|
||||
'X-Robots-Tag': 'noindex, nofollow, noarchive, nosnippet',
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user