Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

1.2 KB
Raw Blame
export const hasProviderHeaderAuth = (headers?: Record<string, string>): boolean =>
    Object.entries(headers || {}).some(([name, value]) =>
        ["authorization", "x-api-key", "api-key"].includes(name.toLowerCase()) && value.trim() !== "");

export const parseProviderHeaders = (value: string): Record<string, string> | null => {
    if (!value.trim()) {
        return {};
    }
    try {
        const parsed = JSON.parse(value);
        if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
            return null;
        }
        const names = new Set<string>();
        for (const [name, headerValue] of Object.entries(parsed)) {
            const normalized = name.toLowerCase();
            if (!/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(name) || names.has(normalized) ||
                typeof headerValue !== "string" || Array.from(headerValue).some((character) => {
                    const code = character.charCodeAt(0);
                    return (code < 32 && code !== 9) || code === 127;
                })) {
                return null;
            }
            names.add(normalized);
        }
        return parsed;
    } catch {
        return null;
    }
};