Skip to content

Commit 016ad8d

Browse files
committed
fix(opencode): validate workspace usage links
1 parent 6714ba0 commit 016ad8d

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎packages/provider-opencode/src/server/v2/OpenCode2Client.test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ describe("OpenCode2Client usage limits", () => {
253253
it.effect("reads the connected workspace and maps Go and Console budgets", () =>
254254
Effect.gen(function* () {
255255
let workspace = "wrk_remote";
256+
let usageURL: string | undefined;
256257
const transport = HttpClient.make((request) => {
257258
assert.strictEqual(request.headers.authorization, utf8Basic("remote-password"));
258259
assert.deepStrictEqual(Array.from(request.urlParams), [
@@ -286,7 +287,7 @@ describe("OpenCode2Client usage limits", () => {
286287
workspace: {
287288
id: workspace,
288289
name: workspace,
289-
usageURL: "https://opencode.ai/console/" + workspace,
290+
usageURL: usageURL ?? "https://opencode.ai/console/" + workspace,
290291
},
291292
go: {
292293
rolling: { percent: 23, limited: false, resetsAt: 1793491200000 },
@@ -381,6 +382,19 @@ describe("OpenCode2Client usage limits", () => {
381382
const next = yield* api.usageLimits("/remote/project");
382383
assert.strictEqual(next?.externalUsage?.label, "wrk_switched");
383384
assert.strictEqual(next?.externalUsage?.url, "https://opencode.ai/console/wrk_switched");
385+
for (const url of [
386+
"not a URL",
387+
"javascript:alert(1)",
388+
"custom-app://usage",
389+
"file:///tmp/usage",
390+
]) {
391+
usageURL = url;
392+
const invalid = yield* api.usageLimits("/remote/project");
393+
assert.strictEqual(invalid?.externalUsage, undefined);
394+
assert.deepStrictEqual(invalid?.windows, next?.windows);
395+
}
396+
usageURL = "http://console.example/workspace/usage";
397+
assert.strictEqual((yield* api.usageLimits("/remote/project"))?.externalUsage?.url, usageURL);
384398
}),
385399
);
386400

‎packages/provider-opencode/src/server/v2/usageLimits.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,12 @@ export const readServerUsageLimits = Effect.fn("OpenCode2Client.usageLimits")(fu
168168
});
169169
}
170170
}
171+
const usageURL = URL.parse(workspace.usageURL);
171172
return {
172173
...makeUsageLimits({ checkedAt, windows }),
173-
externalUsage: { label: workspace.name?.trim() || workspace.id, url: workspace.usageURL },
174+
...(usageURL?.protocol === "http:" || usageURL?.protocol === "https:"
175+
? { externalUsage: { label: workspace.name?.trim() || workspace.id, url: usageURL.href } }
176+
: {}),
174177
};
175178
}).pipe(
176179
Effect.timeout("5 seconds"),

0 commit comments

Comments
 (0)