diff --git a/lib/config.ts b/lib/config.ts index 2834a3d88..edf8760a3 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -76,6 +76,7 @@ export type Config = { allow_user_hotlink_template: boolean; filter_regex_engine: string; allow_user_supply_unsafe_domain: boolean; + disable_nsfw: boolean; }; suffix?: string; titleLengthLimit: number; @@ -538,6 +539,7 @@ const calculateValue = () => { allow_user_hotlink_template: toBoolean(envs.ALLOW_USER_HOTLINK_TEMPLATE, false), filter_regex_engine: envs.FILTER_REGEX_ENGINE || 're2', allow_user_supply_unsafe_domain: toBoolean(envs.ALLOW_USER_SUPPLY_UNSAFE_DOMAIN, false), + disable_nsfw: toBoolean(envs.DISABLE_NSFW, false), }, suffix: envs.SUFFIX, titleLengthLimit: toInt(envs.TITLE_LENGTH_LIMIT, 150), diff --git a/lib/registry.test.ts b/lib/registry.test.ts index a2a2eb8fc..0892ca7d7 100644 --- a/lib/registry.test.ts +++ b/lib/registry.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import app from '@/app'; import { config } from '@/config'; @@ -37,4 +37,27 @@ describe('registry', () => { expect(response.headers.get('cache-control')).toBe('no-cache'); expect(await response.text()).toBe('ok'); }); + + it('namespaces respects DISABLE_NSFW=true', async () => { + vi.resetModules(); + vi.stubEnv('DISABLE_NSFW', 'true'); + + const { namespaces } = await import('./registry'); + + // All routes in all namespaces should not have nsfw features + for (const ns of Object.values(namespaces)) { + for (const route of Object.values(ns.routes)) { + expect(route.features?.nsfw).not.toBe(true); + } + } + expect(namespaces['2048']).toBeUndefined(); + }); + + it('namespaces includes NSFW routes when DISABLE_NSFW=false', async () => { + vi.resetModules(); + vi.stubEnv('DISABLE_NSFW', 'false'); + + const { namespaces } = await import('./registry'); + expect(namespaces['2048']).toBeDefined(); + }); }); diff --git a/lib/registry.ts b/lib/registry.ts index 5887e4974..d50a9a050 100644 --- a/lib/registry.ts +++ b/lib/registry.ts @@ -14,16 +14,34 @@ import logger from '@/utils/logger'; const __dirname = import.meta.dirname; +function isSafeRoutes(routes: RoutesType): boolean { + return Object.values(routes).every((route: Route) => !route.features?.nsfw); +} + +function safeNamespaces(namespaces: NamespacesType): NamespacesType { + const safe: NamespacesType = {}; + + for (const [key, value] of Object.entries(namespaces)) { + if (value.routes === null || value.routes === undefined || isSafeRoutes(value.routes)) { + safe[key] = value; + } + } + return safe; +} + let modules: Record = {}; -let namespaces: Record< + +type RoutesType = Record< + string, + Route & { + location: string; + } +>; + +export type NamespacesType = Record< string, Namespace & { - routes: Record< - string, - Route & { - location: string; - } - >; + routes: RoutesType; apiRoutes: Record< string, APIRoute & { @@ -31,7 +49,9 @@ let namespaces: Record< } >; } -> = {}; +>; + +let namespaces: NamespacesType = {}; switch (process.env.NODE_ENV) { case 'production': @@ -40,6 +60,10 @@ switch (process.env.NODE_ENV) { case 'test': // @ts-expect-error namespaces = await import('../assets/build/routes.json'); + if (namespaces.default) { + // @ts-ignore + namespaces = namespaces.default; + } break; default: modules = directoryImport({ @@ -48,6 +72,10 @@ switch (process.env.NODE_ENV) { }) as typeof modules; } +if (config.feature.disable_nsfw) { + namespaces = safeNamespaces(namespaces); +} + if (Object.keys(modules).length) { for (const module in modules) { const content = modules[module] as diff --git a/lib/routes/2048/index.ts b/lib/routes/2048/index.ts index 95e51e39c..0355a37de 100644 --- a/lib/routes/2048/index.ts +++ b/lib/routes/2048/index.ts @@ -20,6 +20,7 @@ export const route: Route = { supportBT: true, supportPodcast: false, supportScihub: false, + nsfw: true, }, name: '论坛', maintainers: ['nczitzk'], diff --git a/lib/routes/t66y/index.ts b/lib/routes/t66y/index.ts index f8cc4825f..751293d60 100644 --- a/lib/routes/t66y/index.ts +++ b/lib/routes/t66y/index.ts @@ -17,6 +17,7 @@ export const route: Route = { supportBT: false, supportPodcast: false, supportScihub: false, + nsfw: true, }, name: '分区帖子', maintainers: ['zhboner'], diff --git a/lib/types.ts b/lib/types.ts index e4a45d3ba..57c483a35 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -346,6 +346,9 @@ interface RouteItem { /** Set to `true` if the feed supports Sci-Hub */ supportScihub?: boolean; + + /** Set to `true` if this feed is not safe for work */ + nsfw?: boolean; }; /**