Skip to content

Commit 3a9047b

Browse files
Validate baseURL is an absolute URL
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent edeed64 commit 3a9047b

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

docs/tasks/streams.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ 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.
658+
the resulting value must not be empty. The value must also be a valid absolute URL.
659659

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

packages/ai/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
- Added consistent baseURL normalization for trigger and stream endpoints (including path prefixes and trailing slashes).
2323
- Added surrounding-whitespace trimming for `baseURL` before endpoint normalization.
2424
- Added explicit validation that `baseURL` is non-empty after normalization.
25+
- Added explicit validation that `baseURL` is a valid absolute URL.

packages/ai/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ both cleanup steps (`set` inactive state and `delete`) even if one of them fails
161161
- Trailing slashes are normalized automatically before trigger/stream requests.
162162
- Surrounding whitespace is trimmed before normalization.
163163
- `baseURL` must not be empty after trimming/normalization.
164+
- `baseURL` must be a valid absolute URL.
164165

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

packages/ai/src/chatTransport.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,28 @@ describe("TriggerChatTransport", function () {
620620
}).toThrowError("baseURL must not be empty");
621621
});
622622

623+
it("throws when baseURL is not a valid absolute URL", function () {
624+
expect(function () {
625+
new TriggerChatTransport({
626+
task: "chat-task",
627+
accessToken: "pk_trigger",
628+
baseURL: "not-a-valid-url",
629+
stream: "chat-stream",
630+
});
631+
}).toThrowError("baseURL must be a valid absolute URL");
632+
});
633+
634+
it("throws when baseURL is a relative path", function () {
635+
expect(function () {
636+
new TriggerChatTransport({
637+
task: "chat-task",
638+
accessToken: "pk_trigger",
639+
baseURL: "/relative/path",
640+
stream: "chat-stream",
641+
});
642+
}).toThrowError("baseURL must be a valid absolute URL");
643+
});
644+
623645
it("combines path prefixes with run and stream URL encoding", async function () {
624646
let observedTriggerPath: string | undefined;
625647
let observedStreamPath: string | undefined;
@@ -2745,6 +2767,17 @@ describe("TriggerChatTransport", function () {
27452767
}).toThrowError("baseURL must not be empty");
27462768
});
27472769

2770+
it("throws from factory when baseURL is not a valid absolute URL", function () {
2771+
expect(function () {
2772+
createTriggerChatTransport({
2773+
task: "chat-task",
2774+
accessToken: "pk_trigger",
2775+
baseURL: "invalid-base-url",
2776+
stream: "chat-stream",
2777+
});
2778+
}).toThrowError("baseURL must be a valid absolute URL");
2779+
});
2780+
27482781
it("continues streaming when onTriggeredRun callback throws", async function () {
27492782
let callbackCalled = false;
27502783
const errors: TriggerChatTransportError[] = [];

packages/ai/src/chatTransport.ts

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

469+
try {
470+
new URL(normalizedBaseUrl);
471+
} catch {
472+
throw new Error("baseURL must be a valid absolute URL");
473+
}
474+
469475
return normalizedBaseUrl;
470476
}
471477

0 commit comments

Comments
 (0)