chore: 项目基线(基于 WeKnora 定制版,整理文件忽略规则)
This commit is contained in:
commit
9097ee699d
5769 files changed
+1446466
No files matched your search
@@ -0,0 +1,48 @@
|
||||
export type AllowedOriginsValidationError =
|
||||
| { code: 'required' }
|
||||
| { code: 'wildcard_prod' }
|
||||
| { code: 'invalid'; origin: string }
|
||||
|
||||
/** Mirrors backend validateAllowedOrigins in internal/handler/embed_channel.go */
|
||||
export function parseAllowedOrigins(text: string): string[] {
|
||||
return text
|
||||
.split('\n')
|
||||
.map((line) => line.trim())
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
export function validateAllowedOrigins(
|
||||
origins: string[],
|
||||
prod = import.meta.env?.PROD ?? false,
|
||||
): { ok: true; origins: string[] } | { ok: false; error: AllowedOriginsValidationError } {
|
||||
const cleaned = parseAllowedOrigins(origins.join('\n'))
|
||||
if (cleaned.length === 0) {
|
||||
return { ok: false, error: { code: 'required' } }
|
||||
}
|
||||
for (const origin of cleaned) {
|
||||
if (origin === '*') {
|
||||
if (prod) {
|
||||
return { ok: false, error: { code: 'wildcard_prod' } }
|
||||
}
|
||||
continue
|
||||
}
|
||||
let host = origin
|
||||
if (origin.startsWith('*.')) {
|
||||
host = `https://${origin.slice(2)}`
|
||||
}
|
||||
try {
|
||||
if (!/^https?:\/\/[^/?#\\\s]+\/?$/i.test(host)) {
|
||||
return { ok: false, error: { code: 'invalid', origin } }
|
||||
}
|
||||
const url = new URL(host)
|
||||
if ((url.protocol !== 'http:' && url.protocol !== 'https:') || !url.host ||
|
||||
url.username || url.password || (url.pathname !== '' && url.pathname !== '/') ||
|
||||
host.includes('?') || host.includes('#') || /[\s'";,*\\]/.test(url.host)) {
|
||||
return { ok: false, error: { code: 'invalid', origin } }
|
||||
}
|
||||
} catch {
|
||||
return { ok: false, error: { code: 'invalid', origin } }
|
||||
}
|
||||
}
|
||||
return { ok: true, origins: cleaned }
|
||||
}
|
||||
Reference in new issue
Block a user