fix(ai-clarifai): require clean api base urls#817
Open
aiirvizionz wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the Clarifai AI adapter’s endpoint construction by normalizing and validating the OpenAI-compatible baseUrl before issuing chat completions requests, preventing surprising or malformed request targets.
Changes:
- Normalize
baseUrlvia a newcleanBaseUrl()helper and strip trailing slashes to avoid//chat/completions. - Reject
baseUrlvalues containing credentials, query strings, or fragments before any network call is made. - Extend Vitest coverage to validate trailing-slash normalization and rejection of “unclean” base URLs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/ai/clarifai/src/index.ts | Introduces cleanBaseUrl() and uses it to build a stable /chat/completions endpoint while rejecting unsafe base URL forms. |
| packages/ai/clarifai/src/index.test.ts | Updates the custom base URL test for trailing slashes and adds coverage for rejecting credentials/query/hash in base URLs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+72
to
+81
| function cleanBaseUrl(value: string): string { | ||
| const url = new URL(value); | ||
| if (url.protocol !== 'https:' && url.protocol !== 'http:') { | ||
| throw new Error('Clarifai baseUrl must use http or https'); | ||
| } | ||
| if (url.username || url.password || url.search || url.hash) { | ||
| throw new Error('Clarifai baseUrl must be a clean API base without credentials, query, or hash'); | ||
| } | ||
| return url.toString().replace(/\/+$/, ''); | ||
| } |
Comment on lines
+113
to
+126
| it('rejects base URLs with credentials, query, or hash', async () => { | ||
| const fetchMock = vi.fn(); | ||
| vi.stubGlobal('fetch', fetchMock); | ||
|
|
||
| await expect( | ||
| adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://user:pass@clarifai.test/v2/ext/openai/v1' }), | ||
| ).rejects.toThrow('clean API base'); | ||
| await expect( | ||
| adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://clarifai.test/v2/ext/openai/v1?target=chat' }), | ||
| ).rejects.toThrow('clean API base'); | ||
| await expect( | ||
| adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://clarifai.test/v2/ext/openai/v1#chat' }), | ||
| ).rejects.toThrow('clean API base'); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/v1/bases do not produce double-slash request pathsWhy
The adapter previously concatenated
config.baseUrldirectly with/chat/completions. A custom base URL ending in/produced a//chat/completionspath, and URLs with credentials/query/hash could create surprising request targets. This keeps Clarifai endpoint construction explicit and predictable.Validation
vitest run packages/ai/clarifai/src/index.test.ts(7 passed)tsc -p packages/ai/clarifai/tsconfig.json --noEmitgit diff --check