Merge pull request #822 from kaitranntt/kai/fix/821-websearch-validation-followup

fix(websearch): align provider validation metadata
This commit is contained in:
Kai (Tam Nhu) Tran
2026-03-27 15:07:14 -04:00
committed by GitHub
2 changed files with 61 additions and 23 deletions
+6 -2
View File
@@ -9,6 +9,7 @@ import { getWebSearchReadiness, getWebSearchCliProviders } from '../../utils/web
import {
applyWebSearchApiKeyUpdates,
getWebSearchApiKeyStates,
WEBSEARCH_API_KEY_PROVIDERS,
type WebSearchApiKeyProviderId,
} from '../../utils/websearch/provider-secrets';
import { requireLocalAccessWhenAuthDisabled } from '../middleware/auth-middleware';
@@ -16,7 +17,6 @@ import { requireLocalAccessWhenAuthDisabled } from '../middleware/auth-middlewar
const router = Router();
const WEBSEARCH_LOCAL_ACCESS_ERROR =
'WebSearch endpoints require localhost access when dashboard auth is disabled.';
const WEBSEARCH_API_KEY_PROVIDER_IDS = ['exa', 'tavily', 'brave'] as const;
type WebSearchApiKeyUpdates = Partial<Record<WebSearchApiKeyProviderId, string | null>>;
@@ -24,6 +24,10 @@ interface WebSearchDashboardPayload extends Partial<WebSearchConfig> {
apiKeys?: WebSearchApiKeyUpdates;
}
function isWebSearchApiKeyProviderId(value: string): value is WebSearchApiKeyProviderId {
return Object.prototype.hasOwnProperty.call(WEBSEARCH_API_KEY_PROVIDERS, value);
}
router.use((req: Request, res: Response, next) => {
if (requireLocalAccessWhenAuthDisabled(req, res, WEBSEARCH_LOCAL_ACCESS_ERROR)) {
next();
@@ -88,7 +92,7 @@ router.put('/', (req: Request, res: Response): void => {
if (apiKeys) {
for (const [providerId, value] of Object.entries(apiKeys)) {
if (!WEBSEARCH_API_KEY_PROVIDER_IDS.includes(providerId as WebSearchApiKeyProviderId)) {
if (!isWebSearchApiKeyProviderId(providerId)) {
res.status(400).json({ error: `Unsupported WebSearch provider: ${providerId}` });
return;
}
+55 -21
View File
@@ -28,6 +28,17 @@ describe('websearch routes', () => {
let originalEnvValues: Record<(typeof WEBSEARCH_ENV_KEYS)[number], string | undefined>;
let forcedRemoteAddress = '127.0.0.1';
async function putWebsearch(
body: string | Record<string, unknown>,
headers: Record<string, string> = { 'Content-Type': 'application/json' }
): Promise<Response> {
return fetch(`${baseUrl}/api/websearch`, {
method: 'PUT',
headers,
body: typeof body === 'string' ? body : JSON.stringify(body),
});
}
beforeAll(async () => {
const app = express();
app.use(express.json());
@@ -278,11 +289,7 @@ describe('websearch routes', () => {
});
it('rejects non-object request bodies', async () => {
const response = await fetch(`${baseUrl}/api/websearch`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: '[]',
});
const response = await putWebsearch('[]');
expect(response.status).toBe(400);
expect(await response.json()).toEqual({
@@ -290,15 +297,19 @@ describe('websearch routes', () => {
});
});
it('rejects primitive JSON null bodies at the JSON parser layer', async () => {
const response = await putWebsearch('null');
expect(response.status).toBe(400);
expect(response.headers.get('content-type')).toContain('text/html');
expect(await response.text()).toContain('SyntaxError');
});
it('rejects unsupported API key providers', async () => {
const response = await fetch(`${baseUrl}/api/websearch`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
apiKeys: {
invalid: 'secret',
},
}),
const response = await putWebsearch({
apiKeys: {
invalid: 'secret',
},
});
expect(response.status).toBe(400);
@@ -308,14 +319,10 @@ describe('websearch routes', () => {
});
it('rejects non-string API key values', async () => {
const response = await fetch(`${baseUrl}/api/websearch`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
apiKeys: {
exa: 123,
},
}),
const response = await putWebsearch({
apiKeys: {
exa: 123,
},
});
expect(response.status).toBe(400);
@@ -323,4 +330,31 @@ describe('websearch routes', () => {
error: 'Invalid value for exa API key',
});
});
it('rejects null and array values for providers and apiKeys', async () => {
const invalidPayloads = [
{
body: { providers: null },
error: 'Invalid value for providers. Must be an object.',
},
{
body: { providers: [] },
error: 'Invalid value for providers. Must be an object.',
},
{
body: { apiKeys: null },
error: 'Invalid value for apiKeys. Must be an object.',
},
{
body: { apiKeys: [] },
error: 'Invalid value for apiKeys. Must be an object.',
},
] as const;
for (const invalidPayload of invalidPayloads) {
const response = await putWebsearch(invalidPayload.body);
expect(response.status).toBe(400);
expect(await response.json()).toEqual({ error: invalidPayload.error });
}
});
});