mirror of
https://github.com/tiennm99/ccs.git
synced 2026-08-11 00:24:08 +00:00
fix(websearch): align provider validation metadata
This commit is contained in:
@@ -9,6 +9,7 @@ import { getWebSearchReadiness, getWebSearchCliProviders } from '../../utils/web
|
|||||||
import {
|
import {
|
||||||
applyWebSearchApiKeyUpdates,
|
applyWebSearchApiKeyUpdates,
|
||||||
getWebSearchApiKeyStates,
|
getWebSearchApiKeyStates,
|
||||||
|
WEBSEARCH_API_KEY_PROVIDERS,
|
||||||
type WebSearchApiKeyProviderId,
|
type WebSearchApiKeyProviderId,
|
||||||
} from '../../utils/websearch/provider-secrets';
|
} from '../../utils/websearch/provider-secrets';
|
||||||
import { requireLocalAccessWhenAuthDisabled } from '../middleware/auth-middleware';
|
import { requireLocalAccessWhenAuthDisabled } from '../middleware/auth-middleware';
|
||||||
@@ -16,7 +17,6 @@ import { requireLocalAccessWhenAuthDisabled } from '../middleware/auth-middlewar
|
|||||||
const router = Router();
|
const router = Router();
|
||||||
const WEBSEARCH_LOCAL_ACCESS_ERROR =
|
const WEBSEARCH_LOCAL_ACCESS_ERROR =
|
||||||
'WebSearch endpoints require localhost access when dashboard auth is disabled.';
|
'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>>;
|
type WebSearchApiKeyUpdates = Partial<Record<WebSearchApiKeyProviderId, string | null>>;
|
||||||
|
|
||||||
@@ -24,6 +24,10 @@ interface WebSearchDashboardPayload extends Partial<WebSearchConfig> {
|
|||||||
apiKeys?: WebSearchApiKeyUpdates;
|
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) => {
|
router.use((req: Request, res: Response, next) => {
|
||||||
if (requireLocalAccessWhenAuthDisabled(req, res, WEBSEARCH_LOCAL_ACCESS_ERROR)) {
|
if (requireLocalAccessWhenAuthDisabled(req, res, WEBSEARCH_LOCAL_ACCESS_ERROR)) {
|
||||||
next();
|
next();
|
||||||
@@ -88,7 +92,7 @@ router.put('/', (req: Request, res: Response): void => {
|
|||||||
|
|
||||||
if (apiKeys) {
|
if (apiKeys) {
|
||||||
for (const [providerId, value] of Object.entries(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}` });
|
res.status(400).json({ error: `Unsupported WebSearch provider: ${providerId}` });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,17 @@ describe('websearch routes', () => {
|
|||||||
let originalEnvValues: Record<(typeof WEBSEARCH_ENV_KEYS)[number], string | undefined>;
|
let originalEnvValues: Record<(typeof WEBSEARCH_ENV_KEYS)[number], string | undefined>;
|
||||||
let forcedRemoteAddress = '127.0.0.1';
|
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 () => {
|
beforeAll(async () => {
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
@@ -278,11 +289,7 @@ describe('websearch routes', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('rejects non-object request bodies', async () => {
|
it('rejects non-object request bodies', async () => {
|
||||||
const response = await fetch(`${baseUrl}/api/websearch`, {
|
const response = await putWebsearch('[]');
|
||||||
method: 'PUT',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: '[]',
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(response.status).toBe(400);
|
expect(response.status).toBe(400);
|
||||||
expect(await response.json()).toEqual({
|
expect(await response.json()).toEqual({
|
||||||
@@ -290,15 +297,16 @@ describe('websearch routes', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('rejects primitive JSON null bodies before route validation', async () => {
|
||||||
|
const response = await putWebsearch('null');
|
||||||
|
expect(response.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
it('rejects unsupported API key providers', async () => {
|
it('rejects unsupported API key providers', async () => {
|
||||||
const response = await fetch(`${baseUrl}/api/websearch`, {
|
const response = await putWebsearch({
|
||||||
method: 'PUT',
|
apiKeys: {
|
||||||
headers: { 'Content-Type': 'application/json' },
|
invalid: 'secret',
|
||||||
body: JSON.stringify({
|
},
|
||||||
apiKeys: {
|
|
||||||
invalid: 'secret',
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(response.status).toBe(400);
|
expect(response.status).toBe(400);
|
||||||
@@ -308,14 +316,10 @@ describe('websearch routes', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('rejects non-string API key values', async () => {
|
it('rejects non-string API key values', async () => {
|
||||||
const response = await fetch(`${baseUrl}/api/websearch`, {
|
const response = await putWebsearch({
|
||||||
method: 'PUT',
|
apiKeys: {
|
||||||
headers: { 'Content-Type': 'application/json' },
|
exa: 123,
|
||||||
body: JSON.stringify({
|
},
|
||||||
apiKeys: {
|
|
||||||
exa: 123,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(response.status).toBe(400);
|
expect(response.status).toBe(400);
|
||||||
@@ -323,4 +327,31 @@ describe('websearch routes', () => {
|
|||||||
error: 'Invalid value for exa API key',
|
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 });
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user