Skip to content

Commit 9afa035

Browse files
Require http(s) protocol for chat transport baseURL
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent 3a9047b commit 9afa035

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

docs/tasks/streams.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,8 @@ If `onError` is omitted, reconnect still returns `null` and continues without ca
655655

656656
`baseURL` supports optional path prefixes and trailing slashes; both trigger and stream URLs
657657
are normalized consistently, surrounding whitespace is trimmed before normalization, and
658-
the resulting value must not be empty. The value must also be a valid absolute URL.
658+
the resulting value must not be empty. The value must also be a valid absolute URL using
659+
the `http` or `https` protocol.
659660

660661
For richer TypeScript ergonomics in app code, `@trigger.dev/ai` also exports:
661662

packages/ai/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@
2323
- Added surrounding-whitespace trimming for `baseURL` before endpoint normalization.
2424
- Added explicit validation that `baseURL` is non-empty after normalization.
2525
- Added explicit validation that `baseURL` is a valid absolute URL.
26+
- Added explicit validation that `baseURL` uses `http` or `https`.

packages/ai/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ both cleanup steps (`set` inactive state and `delete`) even if one of them fails
162162
- Surrounding whitespace is trimmed before normalization.
163163
- `baseURL` must not be empty after trimming/normalization.
164164
- `baseURL` must be a valid absolute URL.
165+
- `baseURL` must use the `http` or `https` protocol.
165166

166167
## `ai.tool(...)` example
167168

packages/ai/src/chatTransport.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,17 @@ describe("TriggerChatTransport", function () {
642642
}).toThrowError("baseURL must be a valid absolute URL");
643643
});
644644

645+
it("throws when baseURL protocol is not http or https", function () {
646+
expect(function () {
647+
new TriggerChatTransport({
648+
task: "chat-task",
649+
accessToken: "pk_trigger",
650+
baseURL: "ftp://example.com",
651+
stream: "chat-stream",
652+
});
653+
}).toThrowError("baseURL must use http or https protocol");
654+
});
655+
645656
it("combines path prefixes with run and stream URL encoding", async function () {
646657
let observedTriggerPath: string | undefined;
647658
let observedStreamPath: string | undefined;
@@ -2778,6 +2789,17 @@ describe("TriggerChatTransport", function () {
27782789
}).toThrowError("baseURL must be a valid absolute URL");
27792790
});
27802791

2792+
it("throws from factory when baseURL protocol is not http or https", function () {
2793+
expect(function () {
2794+
createTriggerChatTransport({
2795+
task: "chat-task",
2796+
accessToken: "pk_trigger",
2797+
baseURL: "ftp://example.com",
2798+
stream: "chat-stream",
2799+
});
2800+
}).toThrowError("baseURL must use http or https protocol");
2801+
});
2802+
27812803
it("continues streaming when onTriggeredRun callback throws", async function () {
27822804
let callbackCalled = false;
27832805
const errors: TriggerChatTransportError[] = [];

packages/ai/src/chatTransport.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,20 @@ function normalizeBaseUrl(baseURL: string) {
466466
throw new Error("baseURL must not be empty");
467467
}
468468

469+
let parsedBaseUrl: URL;
469470
try {
470-
new URL(normalizedBaseUrl);
471+
parsedBaseUrl = new URL(normalizedBaseUrl);
471472
} catch {
472473
throw new Error("baseURL must be a valid absolute URL");
473474
}
474475

476+
if (
477+
parsedBaseUrl.protocol !== "http:" &&
478+
parsedBaseUrl.protocol !== "https:"
479+
) {
480+
throw new Error("baseURL must use http or https protocol");
481+
}
482+
475483
return normalizedBaseUrl;
476484
}
477485

0 commit comments

Comments
 (0)