security: 隐藏 MongoDB 连接串与库名,health 不再返回 uri/db 并对错误脱敏
This commit is contained in:
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
# MongoDB 连接(留空则回落到本地默认,配置/历史不持久化)
|
# MongoDB 连接(留空则回落到本地默认,配置/历史不持久化)
|
||||||
# 本地: mongodb://127.0.0.1:27017
|
# 本地: mongodb://127.0.0.1:27017
|
||||||
# 云端 Atlas 示例(把 <PASSWORD> 换成真实密码,数据库会建在 MONGODB_DB 指定的库,不会落到 test):
|
# 云端 Atlas 示例(把 <PASSWORD> 换成真实密码、<cluster> 换成你的集群地址,数据库会建在 MONGODB_DB 指定的库,不会落到 test):
|
||||||
# MONGODB_URI=mongodb://devops:<PASSWORD>@ac-eqksaup-shard-00-00.utlbkzb.mongodb.net:27017,ac-eqksaup-shard-00-01.utlbkzb.mongodb.net:27017,ac-eqksaup-shard-00-02.utlbkzb.mongodb.net:27017/?ssl=true&replicaSet=atlas-ofsrbz-shard-0&authSource=admin&appName=devops
|
# MONGODB_URI=mongodb://<user>:<PASSWORD>@<cluster>-shard-00-00.<xxxx>.mongodb.net:27017,<cluster>-shard-00-01.<xxxx>.mongodb.net:27017,<cluster>-shard-00-02.<xxxx>.mongodb.net:27017/?ssl=true&replicaSet=atlas-<xxxx>-shard-0&authSource=admin&appName=app
|
||||||
MONGODB_URI=mongodb://127.0.0.1:27017
|
MONGODB_URI=mongodb://127.0.0.1:27017
|
||||||
MONGODB_DB=torrent_console
|
MONGODB_DB=torrent_console
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ onMounted(() => {
|
|||||||
const mongoPill = computed(() => {
|
const mongoPill = computed(() => {
|
||||||
if (!health.value) return { cls: '', text: 'MONGO ?' }
|
if (!health.value) return { cls: '', text: 'MONGO ?' }
|
||||||
return health.value.mongo.ok
|
return health.value.mongo.ok
|
||||||
? { cls: 'on', text: `MONGO · ${health.value.mongo.db}` }
|
? { cls: 'on', text: 'MONGO' }
|
||||||
: { cls: 'off', text: 'MONGO 未连接' }
|
: { cls: 'off', text: 'MONGO 未连接' }
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
+1
-2
@@ -153,8 +153,7 @@ function previewLink(item: TorrentView) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
<div v-if="health && !health.mongo.ok" class="note warn mt rise rise-2">
|
<div v-if="health && !health.mongo.ok" class="note warn mt rise rise-2">
|
||||||
MongoDB 未连接,搜索记录与种子缓存不会被保存(其余功能可用)。在 .env 配置正确的 MONGODB_URI(云端实例请填真实密码)后刷新即可:
|
MongoDB 未连接,搜索记录与种子缓存不会被保存(其余功能可用)。在 .env 配置正确的 MONGODB_URI 后刷新即可。
|
||||||
<span class="mono">{{ health.mongo.uri }}</span>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="tabs rise rise-2">
|
<div class="tabs rise rise-2">
|
||||||
|
|||||||
@@ -321,7 +321,6 @@ onMounted(load)
|
|||||||
<dt>MongoDB</dt>
|
<dt>MongoDB</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span :class="health?.mongo.ok ? 'ok' : 'err'">{{ health?.mongo.ok ? '已连接' : '未连接' }}</span>
|
<span :class="health?.mongo.ok ? 'ok' : 'err'">{{ health?.mongo.ok ? '已连接' : '未连接' }}</span>
|
||||||
<span class="mono small dim"> · {{ health?.mongo.uri }} / {{ health?.mongo.db }}</span>
|
|
||||||
<div v-if="health?.mongo.error" class="small err mono break">{{ health.mongo.error }}</div>
|
<div v-if="health?.mongo.error" class="small err mono break">{{ health.mongo.error }}</div>
|
||||||
</dd>
|
</dd>
|
||||||
<dt>磁力解析</dt>
|
<dt>磁力解析</dt>
|
||||||
|
|||||||
+1
-1
@@ -95,7 +95,7 @@ export interface ClientsResponse {
|
|||||||
export interface HealthResponse {
|
export interface HealthResponse {
|
||||||
ok: boolean
|
ok: boolean
|
||||||
time: string
|
time: string
|
||||||
mongo: { ok: boolean; uri: string; db: string; error: string | null }
|
mongo: { ok: boolean; error: string | null }
|
||||||
webTorrent: boolean
|
webTorrent: boolean
|
||||||
providers: { id: string; label: string; homepage: string }[]
|
providers: { id: string; label: string; homepage: string }[]
|
||||||
enabledProviders: string[]
|
enabledProviders: string[]
|
||||||
|
|||||||
@@ -225,7 +225,7 @@ export const zh2en: Record<string, string> = {
|
|||||||
'点击关键词直接搜索': 'Click a keyword to search',
|
'点击关键词直接搜索': 'Click a keyword to search',
|
||||||
'已写入 MongoDB': 'Saved to MongoDB',
|
'已写入 MongoDB': 'Saved to MongoDB',
|
||||||
'MongoDB 未连接,未记录': 'MongoDB not connected, not recorded',
|
'MongoDB 未连接,未记录': 'MongoDB not connected, not recorded',
|
||||||
'MongoDB 未连接,搜索记录与种子缓存不会被保存(其余功能可用)。在 .env 配置正确的 MONGODB_URI(云端实例请填真实密码)后刷新即可:': 'MongoDB not connected; search history and torrent cache will not be saved (other features still work). Set a valid MONGODB_URI in .env (use the real password for cloud instances) and refresh:'
|
'MongoDB 未连接,搜索记录与种子缓存不会被保存(其余功能可用)。在 .env 配置正确的 MONGODB_URI 后刷新即可。': 'MongoDB not connected; search history and torrent cache will not be saved (other features still work). Set a valid MONGODB_URI in .env and refresh.'
|
||||||
}
|
}
|
||||||
|
|
||||||
export const en2zh: Record<string, string> = Object.fromEntries(
|
export const en2zh: Record<string, string> = Object.fromEntries(
|
||||||
|
|||||||
+10
-5
@@ -68,18 +68,23 @@ export async function downloadsCol(): Promise<Collection<DownloadDoc>> {
|
|||||||
return (await getDb()).collection<DownloadDoc>('downloads')
|
return (await getDb()).collection<DownloadDoc>('downloads')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 供 /api/health 使用:探测 MongoDB 是否可用 */
|
/** 把驱动错误里的连接串 / 主机名脱敏,避免泄露凭证与数据库地址 */
|
||||||
|
function sanitizeMongoError(raw: string): string {
|
||||||
|
return raw
|
||||||
|
.replace(/mongodb(\+srv)?:\/\/[^\s'"]+/gi, '***')
|
||||||
|
.replace(/[a-z0-9.-]+\.mongodb\.net(?::\d+)?/gi, '***')
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 供 /api/health 使用:探测 MongoDB 是否可用(不返回任何连接串 / 库名) */
|
||||||
export async function mongoStatus() {
|
export async function mongoStatus() {
|
||||||
try {
|
try {
|
||||||
const db = await getDb()
|
const db = await getDb()
|
||||||
await db.command({ ping: 1 })
|
await db.command({ ping: 1 })
|
||||||
return { ok: true, uri: mongoUri(), db: mongoDbName(), error: null as string | null }
|
return { ok: true, error: null as string | null }
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return {
|
return {
|
||||||
ok: false,
|
ok: false,
|
||||||
uri: mongoUri(),
|
error: sanitizeMongoError(lastError?.message || (err instanceof Error ? err.message : String(err)))
|
||||||
db: mongoDbName(),
|
|
||||||
error: lastError?.message || (err instanceof Error ? err.message : String(err))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user