@zyro-sec/server 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +40 -4
- package/script/build.ts +28 -0
- package/script/pack.ts +5 -0
- package/script/workerd-probe.ts +59 -0
- package/src/api.ts +12 -0
- package/src/auth.ts +70 -0
- package/src/content-headers.ts +32 -0
- package/src/cors.ts +34 -0
- package/src/event-feed.ts +93 -0
- package/src/fetch.ts +67 -0
- package/src/handlers/agent.ts +23 -0
- package/src/handlers/artifact.ts +242 -0
- package/src/handlers/automation.ts +63 -0
- package/src/handlers/blob.ts +55 -0
- package/src/handlers/command.ts +8 -0
- package/src/handlers/config.ts +26 -0
- package/src/handlers/credential.ts +87 -0
- package/src/handlers/debug.ts +57 -0
- package/src/handlers/event.ts +38 -0
- package/src/handlers/form.ts +15 -0
- package/src/handlers/fs.ts +180 -0
- package/src/handlers/generate.ts +117 -0
- package/src/handlers/integration.ts +310 -0
- package/src/handlers/job.ts +29 -0
- package/src/handlers/location.ts +33 -0
- package/src/handlers/machine.ts +336 -0
- package/src/handlers/mcp.ts +160 -0
- package/src/handlers/media.ts +44 -0
- package/src/handlers/message.ts +82 -0
- package/src/handlers/migration.ts +13 -0
- package/src/handlers/model.ts +25 -0
- package/src/handlers/permission.ts +131 -0
- package/src/handlers/persistent-pty.ts +285 -0
- package/src/handlers/plugin.ts +97 -0
- package/src/handlers/project.ts +23 -0
- package/src/handlers/provider.ts +32 -0
- package/src/handlers/pty-socket.ts +47 -0
- package/src/handlers/pty.ts +181 -0
- package/src/handlers/push.ts +67 -0
- package/src/handlers/reference.ts +8 -0
- package/src/handlers/rpc.ts +31 -0
- package/src/handlers/server.ts +58 -0
- package/src/handlers/session-error.ts +47 -0
- package/src/handlers/session.ts +722 -0
- package/src/handlers/shell.ts +72 -0
- package/src/handlers/skill.ts +34 -0
- package/src/handlers/usage.ts +23 -0
- package/src/handlers/vcs.ts +104 -0
- package/src/handlers/voice.ts +50 -0
- package/src/handlers/websearch.ts +50 -0
- package/src/handlers/worktree.ts +60 -0
- package/src/handlers.ts +69 -0
- package/src/location.ts +86 -0
- package/src/media-uri.ts +13 -0
- package/src/middleware/authorization.ts +111 -0
- package/src/middleware/defect-response.ts +39 -0
- package/src/middleware/form-location.ts +44 -0
- package/src/middleware/schema-error.ts +49 -0
- package/src/middleware/session-location.ts +30 -0
- package/src/options.ts +61 -0
- package/src/pairing.ts +36 -0
- package/src/process.ts +274 -0
- package/src/range.ts +16 -0
- package/src/request-site.ts +36 -0
- package/src/request-tracing.ts +9 -0
- package/src/routes.ts +261 -0
- package/src/server-info.ts +41 -0
- package/src/service-status.ts +30 -0
- package/src/workerd.ts +184 -0
- package/test/artifact.test.ts +224 -0
- package/test/auth.test.ts +24 -0
- package/test/automation.test.ts +315 -0
- package/test/client-mcp.test.ts +369 -0
- package/test/codemode-api.test.ts +88 -0
- package/test/config.test.ts +104 -0
- package/test/cors.test.ts +24 -0
- package/test/defect-response.test.ts +83 -0
- package/test/event-feed.test.ts +201 -0
- package/test/fetch-option.test.ts +104 -0
- package/test/fetch.test.ts +698 -0
- package/test/fixture/mcp-starts.cjs +20 -0
- package/test/fixture/routes.ts +37 -0
- package/test/fixture/server.ts +20 -0
- package/test/fixture/worktree-delegate/index.ts +13 -0
- package/test/fixture/worktree-plugin/index.ts +47 -0
- package/test/flag-channel.test.ts +126 -0
- package/test/fs.test.ts +439 -0
- package/test/generate.test.ts +132 -0
- package/test/handler-policy.test.ts +43 -0
- package/test/heap-dump.test.ts +137 -0
- package/test/integration-policy.test.ts +93 -0
- package/test/job.test.ts +186 -0
- package/test/machine-iroh.test.ts +259 -0
- package/test/machine-link.test.ts +1234 -0
- package/test/machine-routes.test.ts +198 -0
- package/test/media.test.ts +343 -0
- package/test/message.test.ts +105 -0
- package/test/model.test.ts +62 -0
- package/test/options.test.ts +42 -0
- package/test/permission-saved.test.ts +73 -0
- package/test/persistent-pty.test.ts +1007 -0
- package/test/process.test.ts +261 -0
- package/test/provider.test.ts +88 -0
- package/test/pty-socket.property.test.ts +240 -0
- package/test/pty-socket.test.ts +89 -0
- package/test/pty-transport.test.ts +200 -0
- package/test/push-routes.test.ts +157 -0
- package/test/request-site.test.ts +71 -0
- package/test/request-tracing.test.ts +87 -0
- package/test/rpc-handler-errors.test.ts +76 -0
- package/test/schema-error.test.ts +68 -0
- package/test/service-status.test.ts +34 -0
- package/test/session-admit-location.test.ts +92 -0
- package/test/session-create.test.ts +65 -0
- package/test/session-diff.test.ts +100 -0
- package/test/session-form.test.ts +50 -0
- package/test/session-import.test.ts +152 -0
- package/test/session-instances.test.ts +335 -0
- package/test/session-unarchive.test.ts +73 -0
- package/test/session-update.test.ts +66 -0
- package/test/skill.test.ts +57 -0
- package/test/sse-failure.test.ts +71 -0
- package/test/usage.test.ts +88 -0
- package/test/vcs.test.ts +356 -0
- package/test/video-generate.test.ts +131 -0
- package/test/voice.test.ts +170 -0
- package/test/workerd.test.ts +227 -0
- package/test/worktree.test.ts +333 -0
- package/tsconfig.build.json +15 -0
- package/tsconfig.json +11 -0
- package/README.md +0 -3
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { expect } from "bun:test"
|
|
2
|
+
import { Effect } from "effect"
|
|
3
|
+
import { HttpServer } from "effect/http"
|
|
4
|
+
import fs from "node:fs/promises"
|
|
5
|
+
import os from "node:os"
|
|
6
|
+
import path from "node:path"
|
|
7
|
+
import { OpenCode } from "../../client/src/promise/index"
|
|
8
|
+
import { it } from "../../core/test/lib/effect"
|
|
9
|
+
import { ServerProcess } from "../src/process"
|
|
10
|
+
|
|
11
|
+
const live = process.platform === "win32" ? it.live.skip : it.live
|
|
12
|
+
|
|
13
|
+
live(
|
|
14
|
+
"PTY websocket preserves replay, text and binary input, resize, reconnect, and exit",
|
|
15
|
+
() =>
|
|
16
|
+
Effect.gen(function* () {
|
|
17
|
+
const directory = yield* Effect.acquireRelease(
|
|
18
|
+
Effect.promise(() => fs.mkdtemp(path.join(os.tmpdir(), "opencode-pty-transport-"))),
|
|
19
|
+
(directory) => Effect.promise(() => fs.rm(directory, { recursive: true, force: true })),
|
|
20
|
+
)
|
|
21
|
+
const server = yield* ServerProcess.start<never, never>({
|
|
22
|
+
hostname: "127.0.0.1",
|
|
23
|
+
port: 0,
|
|
24
|
+
password: "secret",
|
|
25
|
+
app: { version: "test-version" },
|
|
26
|
+
database: { path: ":memory:" },
|
|
27
|
+
models: { fetch: false },
|
|
28
|
+
config: { directory, project: false, content: "{}" },
|
|
29
|
+
fs: { filewatcher: false },
|
|
30
|
+
})
|
|
31
|
+
const base = HttpServer.formatAddress(server.address)
|
|
32
|
+
const client = OpenCode.make({ baseUrl: base, headers: { authorization: `Basic ${btoa("opencode:secret")}` } })
|
|
33
|
+
const location = { directory }
|
|
34
|
+
const terminal = yield* Effect.acquireRelease(
|
|
35
|
+
Effect.promise(() =>
|
|
36
|
+
client.pty.create({
|
|
37
|
+
location,
|
|
38
|
+
command: "/bin/sh",
|
|
39
|
+
args: [
|
|
40
|
+
"-c",
|
|
41
|
+
"stty -echo; printf 'ready\\n'; while IFS= read -r line; do case \"$line\" in size) stty size;; quit) exit 0;; *) printf 'reply:%s\\n' \"$line\";; esac; done",
|
|
42
|
+
],
|
|
43
|
+
cwd: directory,
|
|
44
|
+
}),
|
|
45
|
+
),
|
|
46
|
+
(terminal) => Effect.promise(() => client.pty.remove({ location, ptyID: terminal.data.id })),
|
|
47
|
+
)
|
|
48
|
+
yield* Effect.promise(async () => {
|
|
49
|
+
const open = async () => {
|
|
50
|
+
const token = await client.pty.connect.token({ location, ptyID: terminal.data.id, "x-opencode-ticket": "1" })
|
|
51
|
+
const url = new URL(`/api/pty/${terminal.data.id}/connect`, base)
|
|
52
|
+
url.protocol = "ws:"
|
|
53
|
+
url.searchParams.set("location[directory]", directory)
|
|
54
|
+
url.searchParams.set("ticket", token.data.ticket)
|
|
55
|
+
const socket = new WebSocket(url)
|
|
56
|
+
socket.binaryType = "arraybuffer"
|
|
57
|
+
const state = { output: "", frames: [] as string[], closed: false, code: 0, reason: "", error: false }
|
|
58
|
+
socket.addEventListener("message", (event) => {
|
|
59
|
+
if (typeof event.data === "string") {
|
|
60
|
+
state.output += event.data
|
|
61
|
+
state.frames.push("output")
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
const bytes = new Uint8Array(event.data)
|
|
65
|
+
if (bytes[0] === 0) {
|
|
66
|
+
state.frames.push("cursor")
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
state.output += new TextDecoder().decode(bytes)
|
|
70
|
+
state.frames.push("output")
|
|
71
|
+
})
|
|
72
|
+
socket.addEventListener("close", (event) => {
|
|
73
|
+
state.closed = true
|
|
74
|
+
state.code = event.code
|
|
75
|
+
state.reason = event.reason
|
|
76
|
+
})
|
|
77
|
+
socket.addEventListener("error", () => {
|
|
78
|
+
state.error = true
|
|
79
|
+
})
|
|
80
|
+
return { socket, state }
|
|
81
|
+
}
|
|
82
|
+
const connect = async () => {
|
|
83
|
+
const connection = await open()
|
|
84
|
+
await waitFor(
|
|
85
|
+
() => connection.state.frames.includes("cursor") || connection.state.closed || connection.state.error,
|
|
86
|
+
)
|
|
87
|
+
expect(connection.state.error).toBeFalse()
|
|
88
|
+
expect(connection.state.closed).toBeFalse()
|
|
89
|
+
return connection
|
|
90
|
+
}
|
|
91
|
+
const first = await connect()
|
|
92
|
+
try {
|
|
93
|
+
await waitFor(() => first.state.output.includes("ready"))
|
|
94
|
+
first.socket.send("text\n")
|
|
95
|
+
first.socket.send(new TextEncoder().encode("binary\n"))
|
|
96
|
+
await waitFor(() => first.state.output.includes("reply:binary"))
|
|
97
|
+
expect(first.state.output).toContain("reply:text")
|
|
98
|
+
await client.pty.update({ location, ptyID: terminal.data.id, size: { rows: 31, cols: 93 } })
|
|
99
|
+
first.socket.send("size\n")
|
|
100
|
+
await waitFor(() => first.state.output.includes("31 93"))
|
|
101
|
+
first.socket.close()
|
|
102
|
+
await waitFor(() => first.state.closed)
|
|
103
|
+
const second = await connect()
|
|
104
|
+
try {
|
|
105
|
+
expect(second.state.output).toContain("reply:text")
|
|
106
|
+
expect(second.state.output).toContain("reply:binary")
|
|
107
|
+
expect(second.state.frames.at(-1)).toBe("cursor")
|
|
108
|
+
second.socket.send("reconnected\n")
|
|
109
|
+
await waitFor(() => second.state.output.includes("reply:reconnected"))
|
|
110
|
+
expect(first.state.output).not.toContain("reply:reconnected")
|
|
111
|
+
second.socket.send("quit\n")
|
|
112
|
+
await waitFor(() => second.state.closed)
|
|
113
|
+
expect(second.state.code).toBe(1000)
|
|
114
|
+
expect((await client.pty.get({ location, ptyID: terminal.data.id })).data.status).toBe("exited")
|
|
115
|
+
const exited = await open()
|
|
116
|
+
await waitFor(() => exited.state.closed)
|
|
117
|
+
expect(exited.state.code).toBe(4404)
|
|
118
|
+
expect(exited.state.reason).toBe("session exited")
|
|
119
|
+
expect(exited.state.frames).toEqual([])
|
|
120
|
+
} finally {
|
|
121
|
+
second.socket.close()
|
|
122
|
+
}
|
|
123
|
+
} finally {
|
|
124
|
+
first.socket.close()
|
|
125
|
+
}
|
|
126
|
+
})
|
|
127
|
+
}),
|
|
128
|
+
20_000,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
live(
|
|
132
|
+
"PTY websocket closes with 1012 when its location reloads",
|
|
133
|
+
() =>
|
|
134
|
+
Effect.gen(function* () {
|
|
135
|
+
const directory = yield* Effect.acquireRelease(
|
|
136
|
+
Effect.promise(() => fs.mkdtemp(path.join(os.tmpdir(), "opencode-pty-reload-"))),
|
|
137
|
+
(directory) => Effect.promise(() => fs.rm(directory, { recursive: true, force: true })),
|
|
138
|
+
)
|
|
139
|
+
const server = yield* ServerProcess.start<never, never>({
|
|
140
|
+
hostname: "127.0.0.1",
|
|
141
|
+
port: 0,
|
|
142
|
+
password: "secret",
|
|
143
|
+
app: { version: "test-version" },
|
|
144
|
+
database: { path: ":memory:" },
|
|
145
|
+
models: { fetch: false },
|
|
146
|
+
config: { directory, project: false, content: "{}" },
|
|
147
|
+
fs: { filewatcher: false },
|
|
148
|
+
})
|
|
149
|
+
const base = HttpServer.formatAddress(server.address)
|
|
150
|
+
const client = OpenCode.make({ baseUrl: base, headers: { authorization: `Basic ${btoa("opencode:secret")}` } })
|
|
151
|
+
const location = { directory }
|
|
152
|
+
yield* Effect.promise(async () => {
|
|
153
|
+
const terminal = await client.pty.create({
|
|
154
|
+
location,
|
|
155
|
+
command: "/bin/sh",
|
|
156
|
+
args: ["-c", "sleep 30"],
|
|
157
|
+
cwd: directory,
|
|
158
|
+
})
|
|
159
|
+
const token = await client.pty.connect.token({ location, ptyID: terminal.data.id, "x-opencode-ticket": "1" })
|
|
160
|
+
const url = new URL(`/api/pty/${terminal.data.id}/connect`, base)
|
|
161
|
+
url.protocol = "ws:"
|
|
162
|
+
url.searchParams.set("location[directory]", directory)
|
|
163
|
+
url.searchParams.set("ticket", token.data.ticket)
|
|
164
|
+
const socket = new WebSocket(url)
|
|
165
|
+
const state = { open: false, code: 0 }
|
|
166
|
+
socket.addEventListener("open", () => (state.open = true))
|
|
167
|
+
socket.addEventListener("close", (event) => (state.code = event.code))
|
|
168
|
+
try {
|
|
169
|
+
await waitFor(() => state.open)
|
|
170
|
+
// The socket holds the location; reload must still close it so the old location can shut down.
|
|
171
|
+
await client.location.reload()
|
|
172
|
+
await waitFor(() => state.code !== 0)
|
|
173
|
+
expect(state.code).toBe(1012)
|
|
174
|
+
// Releasing the socket let the old location close, and its shell with it.
|
|
175
|
+
await waitFor(() => !alive(terminal.data.pid))
|
|
176
|
+
expect(await client.pty.get({ location, ptyID: terminal.data.id }).catch(() => undefined)).toBeUndefined()
|
|
177
|
+
} finally {
|
|
178
|
+
socket.close()
|
|
179
|
+
}
|
|
180
|
+
})
|
|
181
|
+
}),
|
|
182
|
+
20_000,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
function alive(pid: number) {
|
|
186
|
+
try {
|
|
187
|
+
process.kill(pid, 0)
|
|
188
|
+
return true
|
|
189
|
+
} catch {
|
|
190
|
+
return false
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function waitFor(predicate: () => boolean) {
|
|
195
|
+
for (let attempt = 0; attempt < 200; attempt++) {
|
|
196
|
+
if (predicate()) return
|
|
197
|
+
await Bun.sleep(20)
|
|
198
|
+
}
|
|
199
|
+
throw new Error("PTY transport did not reach the expected state")
|
|
200
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { expect } from "bun:test"
|
|
2
|
+
import { createECDH } from "node:crypto"
|
|
3
|
+
import { Effect } from "effect"
|
|
4
|
+
import { HttpServer } from "effect/http"
|
|
5
|
+
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
6
|
+
import { it } from "../../core/test/lib/effect"
|
|
7
|
+
import { ServerProcess } from "../src/process"
|
|
8
|
+
|
|
9
|
+
const authorization = `Basic ${btoa("opencode:secret")}`
|
|
10
|
+
const id = "12345678-1234-1234-1234-123456789abc"
|
|
11
|
+
const relay = "https://opencode-notify-relay.anomalyco.workers.dev"
|
|
12
|
+
|
|
13
|
+
const start = Effect.fnUntraced(function* () {
|
|
14
|
+
const directory = (yield* tmpdirScoped("opencode-push-routes-")).path
|
|
15
|
+
const started = yield* ServerProcess.start<never, never>({
|
|
16
|
+
password: "secret",
|
|
17
|
+
app: { version: "test-version" },
|
|
18
|
+
database: { path: ":memory:" },
|
|
19
|
+
models: { fetch: false },
|
|
20
|
+
config: {
|
|
21
|
+
directory,
|
|
22
|
+
project: false,
|
|
23
|
+
},
|
|
24
|
+
fs: { filewatcher: false },
|
|
25
|
+
hostname: "127.0.0.1",
|
|
26
|
+
port: 0,
|
|
27
|
+
})
|
|
28
|
+
const base = HttpServer.formatAddress(started.address)
|
|
29
|
+
return (path: string, init: RequestInit = {}) =>
|
|
30
|
+
Effect.promise(async () => {
|
|
31
|
+
const response = await fetch(`${base}${path}`, {
|
|
32
|
+
...init,
|
|
33
|
+
headers: { authorization, "content-type": "application/json" },
|
|
34
|
+
})
|
|
35
|
+
const text = await response.text()
|
|
36
|
+
return { status: response.status, body: text ? JSON.parse(text) : undefined }
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const register = (publicKey: string, callbackURL = `${relay}/v2/notify/sealed`) =>
|
|
41
|
+
JSON.stringify({ callbackURL, expiresAt: Date.now() + 86_400_000, publicKey, platform: "android", name: "Pixel" })
|
|
42
|
+
|
|
43
|
+
const phoneKey = () => {
|
|
44
|
+
const phone = createECDH("prime256v1")
|
|
45
|
+
phone.generateKeys()
|
|
46
|
+
return phone.getPublicKey("base64")
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
it.live("registers, lists and removes a phone", () =>
|
|
50
|
+
Effect.gen(function* () {
|
|
51
|
+
const request = yield* start()
|
|
52
|
+
const registered = yield* request(`/api/experimental/push/device/${id}`, {
|
|
53
|
+
method: "PUT",
|
|
54
|
+
body: register(phoneKey()),
|
|
55
|
+
})
|
|
56
|
+
expect(registered.status).toBe(200)
|
|
57
|
+
expect(Buffer.from(registered.body.data.publicKey, "base64")).toHaveLength(65)
|
|
58
|
+
|
|
59
|
+
const listed = yield* request("/api/experimental/push")
|
|
60
|
+
expect(listed.body.data.devices.map((device: { id: string }) => device.id)).toEqual([id])
|
|
61
|
+
expect(JSON.stringify(listed.body)).not.toContain("/v2/notify")
|
|
62
|
+
expect(JSON.stringify(listed.body)).not.toContain(registered.body.data.publicKey)
|
|
63
|
+
|
|
64
|
+
const badURL = yield* request(`/api/experimental/push/device/${id}`, {
|
|
65
|
+
method: "PUT",
|
|
66
|
+
body: register(phoneKey(), "https://relay.example.com/v2/notify/sealed"),
|
|
67
|
+
})
|
|
68
|
+
expect(badURL.status).toBe(400)
|
|
69
|
+
const badID = yield* request("/api/experimental/push/device/NOT-A-UUID", {
|
|
70
|
+
method: "PUT",
|
|
71
|
+
body: register(phoneKey()),
|
|
72
|
+
})
|
|
73
|
+
expect(badID.status).toBe(400)
|
|
74
|
+
|
|
75
|
+
expect((yield* request(`/api/experimental/push/device/${id}`, { method: "DELETE" })).status).toBe(204)
|
|
76
|
+
expect((yield* request(`/api/experimental/push/device/${id}`, { method: "DELETE" })).status).toBe(204)
|
|
77
|
+
expect((yield* request("/api/experimental/push")).body.data.devices).toEqual([])
|
|
78
|
+
}),
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
it.live("updates phones and sets session bells", () =>
|
|
82
|
+
Effect.gen(function* () {
|
|
83
|
+
const request = yield* start()
|
|
84
|
+
const device = `/api/experimental/push/device/${id}`
|
|
85
|
+
const until = Date.now() + 3_600_000
|
|
86
|
+
|
|
87
|
+
const unknown = yield* request(device, { method: "PATCH", body: JSON.stringify({ paused: true }) })
|
|
88
|
+
expect(unknown.status).toBe(404)
|
|
89
|
+
expect(unknown.body.deviceID).toBe(id)
|
|
90
|
+
expect(
|
|
91
|
+
(yield* request(`${device}/session/ses_push`, { method: "PUT", body: JSON.stringify({ level: "none" }) })).status,
|
|
92
|
+
).toBe(404)
|
|
93
|
+
|
|
94
|
+
yield* request(device, { method: "PUT", body: register(phoneKey()) })
|
|
95
|
+
const paused = yield* request(device, { method: "PATCH", body: JSON.stringify({ paused: true }) })
|
|
96
|
+
expect(paused.body.data.paused).toBe(true)
|
|
97
|
+
expect(paused.body.data.settings).toEqual({ default: "all" })
|
|
98
|
+
const quieter = yield* request(device, {
|
|
99
|
+
method: "PATCH",
|
|
100
|
+
body: JSON.stringify({ settings: { default: "attention" } }),
|
|
101
|
+
})
|
|
102
|
+
expect(quieter.body.data.paused).toBe(true)
|
|
103
|
+
expect(quieter.body.data.settings).toEqual({ default: "attention" })
|
|
104
|
+
const bell = yield* request(`${device}/session/ses_push`, {
|
|
105
|
+
method: "PUT",
|
|
106
|
+
body: JSON.stringify({ level: "attention", until }),
|
|
107
|
+
})
|
|
108
|
+
expect(bell.body.data).toEqual({ level: "attention", until })
|
|
109
|
+
const shared = yield* request("/api/experimental/push/session/ses_push", {
|
|
110
|
+
method: "PUT",
|
|
111
|
+
body: JSON.stringify({ level: "none" }),
|
|
112
|
+
})
|
|
113
|
+
expect(shared.body.data).toEqual({ level: "none" })
|
|
114
|
+
const all = yield* request("/api/experimental/push/settings", {
|
|
115
|
+
method: "PUT",
|
|
116
|
+
body: JSON.stringify({ paused: true, until }),
|
|
117
|
+
})
|
|
118
|
+
expect(all.body.data).toEqual({ paused: true, until })
|
|
119
|
+
|
|
120
|
+
const listed = (yield* request("/api/experimental/push")).body.data
|
|
121
|
+
expect(listed.settings).toEqual({ paused: true, until })
|
|
122
|
+
expect(listed.sessions).toEqual({ ses_push: { level: "none" } })
|
|
123
|
+
expect(listed.devices[0].paused).toBe(true)
|
|
124
|
+
expect(listed.devices[0].settings).toEqual({ default: "attention" })
|
|
125
|
+
expect(listed.devices[0].sessions).toEqual({ ses_push: { level: "attention", until } })
|
|
126
|
+
|
|
127
|
+
const badLevel = yield* request("/api/experimental/push/session/ses_push", {
|
|
128
|
+
method: "PUT",
|
|
129
|
+
body: JSON.stringify({ level: "loud" }),
|
|
130
|
+
})
|
|
131
|
+
expect(badLevel.status).toBe(400)
|
|
132
|
+
const pastUntil = yield* request("/api/experimental/push/session/ses_push", {
|
|
133
|
+
method: "PUT",
|
|
134
|
+
body: JSON.stringify({ level: "none", until: 1_000 }),
|
|
135
|
+
})
|
|
136
|
+
expect(pastUntil.status).toBe(400)
|
|
137
|
+
|
|
138
|
+
const parent = yield* request("/api/session", { method: "POST", body: JSON.stringify({ title: "Parent" }) })
|
|
139
|
+
const child = yield* request("/api/session", {
|
|
140
|
+
method: "POST",
|
|
141
|
+
body: JSON.stringify({ parentID: parent.body.data.id, title: "Child" }),
|
|
142
|
+
})
|
|
143
|
+
const subagent = { method: "PUT", body: JSON.stringify({ level: "none" }) }
|
|
144
|
+
expect((yield* request(`${device}/session/${child.body.data.id}`, subagent)).status).toBe(400)
|
|
145
|
+
expect((yield* request(`/api/experimental/push/session/${child.body.data.id}`, subagent)).status).toBe(400)
|
|
146
|
+
expect((yield* request(`/api/experimental/push/session/${parent.body.data.id}`, subagent)).status).toBe(200)
|
|
147
|
+
expect((yield* request(`/api/experimental/push/session/${parent.body.data.id}`, { method: "DELETE" })).status).toBe(
|
|
148
|
+
204,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
expect((yield* request(`${device}/session/ses_push`, { method: "DELETE" })).status).toBe(204)
|
|
152
|
+
expect((yield* request("/api/experimental/push/session/ses_push", { method: "DELETE" })).status).toBe(204)
|
|
153
|
+
const cleared = (yield* request("/api/experimental/push")).body.data
|
|
154
|
+
expect(cleared.sessions).toEqual({})
|
|
155
|
+
expect(cleared.devices[0].sessions).toEqual({})
|
|
156
|
+
}),
|
|
157
|
+
)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import fs from "node:fs/promises"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { expect } from "bun:test"
|
|
4
|
+
import { Effect } from "effect"
|
|
5
|
+
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
6
|
+
import { it } from "../../core/test/lib/effect"
|
|
7
|
+
import { startServer } from "./fixture/server"
|
|
8
|
+
|
|
9
|
+
it.live("rejects API navigations and calls from other sites while other clients pass", () =>
|
|
10
|
+
Effect.gen(function* () {
|
|
11
|
+
const tmp = yield* tmpdirScoped()
|
|
12
|
+
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "page.html"), "<p>hi</p>"))
|
|
13
|
+
const server = yield* startServer(path.join(tmp.path, "config"))
|
|
14
|
+
const read = new URL("/api/fs/read/page.html", server.base)
|
|
15
|
+
read.searchParams.set("location[directory]", tmp.path)
|
|
16
|
+
const status = (url: URL | string, headers: Record<string, string>, method = "GET") =>
|
|
17
|
+
Effect.promise(async () => {
|
|
18
|
+
const response = await fetch(new URL(url, server.base), {
|
|
19
|
+
method,
|
|
20
|
+
headers: { ...server.headers, ...headers },
|
|
21
|
+
})
|
|
22
|
+
return response.status
|
|
23
|
+
})
|
|
24
|
+
const navigate = (site: string) => ({
|
|
25
|
+
"sec-fetch-site": site,
|
|
26
|
+
"sec-fetch-mode": "navigate",
|
|
27
|
+
"sec-fetch-dest": "document",
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
// Native clients, same-origin pages and typed-in URLs.
|
|
31
|
+
expect(yield* status(read, {})).toBe(200)
|
|
32
|
+
expect(yield* status(read, navigate("same-origin"))).toBe(200)
|
|
33
|
+
expect(yield* status(read, navigate("none"))).toBe(200)
|
|
34
|
+
expect(yield* status(read, { "sec-fetch-site": "same-origin", "sec-fetch-mode": "cors" })).toBe(200)
|
|
35
|
+
|
|
36
|
+
// Another site opening an API URL as a page.
|
|
37
|
+
expect(yield* status(read, navigate("cross-site"))).toBe(403)
|
|
38
|
+
expect(yield* status(read, navigate("same-site"))).toBe(403)
|
|
39
|
+
expect(yield* status("/api/path", navigate("cross-site"))).toBe(403)
|
|
40
|
+
expect(yield* status("/api/path", navigate("cross-site"), "POST")).toBe(403)
|
|
41
|
+
|
|
42
|
+
// Calls from other sites need an allowed origin; media elements send none.
|
|
43
|
+
const cors = (origin: string) => ({ "sec-fetch-site": "cross-site", "sec-fetch-mode": "cors", origin })
|
|
44
|
+
expect(yield* status(read, cors("https://app.opencode.ai"))).toBe(200)
|
|
45
|
+
expect(yield* status(read, cors("oc://renderer"))).toBe(200)
|
|
46
|
+
expect(yield* status(read, cors("http://localhost:3000"))).toBe(200)
|
|
47
|
+
expect(yield* status(read, cors("https://example.com"))).toBe(403)
|
|
48
|
+
expect(yield* status("/api/path", cors("null"), "POST")).toBe(403)
|
|
49
|
+
expect(
|
|
50
|
+
yield* status(
|
|
51
|
+
"/api/path",
|
|
52
|
+
{ "sec-fetch-site": "cross-site", "sec-fetch-mode": "no-cors", origin: "https://example.com" },
|
|
53
|
+
"POST",
|
|
54
|
+
),
|
|
55
|
+
).toBe(403)
|
|
56
|
+
expect(
|
|
57
|
+
yield* status(read, { "sec-fetch-site": "cross-site", "sec-fetch-mode": "no-cors", "sec-fetch-dest": "video" }),
|
|
58
|
+
).toBe(200)
|
|
59
|
+
|
|
60
|
+
// Pairing links and artifact pages are opened from elsewhere on purpose.
|
|
61
|
+
expect(yield* status("/auth/connect/expired", { ...navigate("cross-site"), accept: "text/html" })).toBe(401)
|
|
62
|
+
const artifact = new URL("/api/experimental/artifacts/art_missing/view/", server.base)
|
|
63
|
+
artifact.searchParams.set("location[directory]", tmp.path)
|
|
64
|
+
const opened = yield* Effect.promise(() =>
|
|
65
|
+
fetch(artifact, { headers: { ...server.headers, ...navigate("cross-site") } }).then((response) =>
|
|
66
|
+
response.json(),
|
|
67
|
+
),
|
|
68
|
+
)
|
|
69
|
+
expect(opened.message).not.toBe("Cross-site requests to the API are not allowed")
|
|
70
|
+
}),
|
|
71
|
+
)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { Effect, Option, Scope, Tracer } from "effect"
|
|
3
|
+
import { HttpMiddleware, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
4
|
+
import { it } from "../../core/test/lib/effect"
|
|
5
|
+
import { ServerProcess } from "../src/process"
|
|
6
|
+
import { withoutParentSpan } from "../src/request-tracing"
|
|
7
|
+
|
|
8
|
+
test("requests ignore ambient parents and continue inbound trace context", async () => {
|
|
9
|
+
const spans: Tracer.NativeSpan[] = []
|
|
10
|
+
const tracer = Tracer.make({
|
|
11
|
+
span(options) {
|
|
12
|
+
const span = new Tracer.NativeSpan(options)
|
|
13
|
+
spans.push(span)
|
|
14
|
+
return span
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
const app = Effect.gen(function* () {
|
|
18
|
+
yield* Scope.Scope
|
|
19
|
+
return yield* HttpMiddleware.tracer(Effect.succeed(HttpServerResponse.empty()))
|
|
20
|
+
})
|
|
21
|
+
const request = (traceparent?: string) =>
|
|
22
|
+
app.pipe(
|
|
23
|
+
Effect.provideService(
|
|
24
|
+
HttpServerRequest.HttpServerRequest,
|
|
25
|
+
HttpServerRequest.fromWeb(
|
|
26
|
+
new Request("http://localhost/trace", {
|
|
27
|
+
headers: traceparent === undefined ? undefined : { traceparent },
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
30
|
+
),
|
|
31
|
+
withoutParentSpan,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
await Effect.runPromise(
|
|
35
|
+
Effect.gen(function* () {
|
|
36
|
+
yield* request()
|
|
37
|
+
yield* request()
|
|
38
|
+
yield* request("00-11111111111111111111111111111111-2222222222222222-01")
|
|
39
|
+
yield* Effect.yieldNow
|
|
40
|
+
}).pipe(Effect.withSpan("fixture.lifecycle"), Effect.provideService(Tracer.Tracer, tracer), Effect.scoped),
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
const requests = spans.filter((span) => span.kind === "server")
|
|
44
|
+
expect(requests).toHaveLength(3)
|
|
45
|
+
expect(requests[0]?.traceId).not.toBe(requests[1]?.traceId)
|
|
46
|
+
expect(Option.getOrUndefined(requests[0]?.parent ?? Option.none())).toBeUndefined()
|
|
47
|
+
expect(Option.getOrUndefined(requests[1]?.parent ?? Option.none())).toBeUndefined()
|
|
48
|
+
expect(requests[2]?.traceId).toBe("11111111111111111111111111111111")
|
|
49
|
+
expect(Option.getOrUndefined(requests[2]?.parent ?? Option.none())?.spanId).toBe("2222222222222222")
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it.live("handler spans nest under their request, not under server startup", () => {
|
|
53
|
+
const spans: Tracer.NativeSpan[] = []
|
|
54
|
+
const tracer = Tracer.make({
|
|
55
|
+
span(options) {
|
|
56
|
+
const span = new Tracer.NativeSpan(options)
|
|
57
|
+
spans.push(span)
|
|
58
|
+
return span
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
return Effect.gen(function* () {
|
|
62
|
+
const server = yield* ServerProcess.start<never, never>({
|
|
63
|
+
hostname: "127.0.0.1",
|
|
64
|
+
port: 0,
|
|
65
|
+
password: "secret",
|
|
66
|
+
app: { version: "test" },
|
|
67
|
+
database: { path: ":memory:" },
|
|
68
|
+
fs: { filewatcher: false },
|
|
69
|
+
})
|
|
70
|
+
const response = yield* Effect.promise(() =>
|
|
71
|
+
fetch(new URL("/api/session", HttpServer.formatAddress(server.address)), {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: { authorization: `Basic ${btoa("opencode:secret")}`, "content-type": "application/json" },
|
|
74
|
+
body: JSON.stringify({ title: "Traced" }),
|
|
75
|
+
}),
|
|
76
|
+
)
|
|
77
|
+
expect(response.status).toBe(200)
|
|
78
|
+
|
|
79
|
+
const startup = spans.find((span) => span.name === "ServerProcess.start")
|
|
80
|
+
const request = spans.find((span) => span.kind === "server" && span.attributes.get("http.route") === "/api/session")
|
|
81
|
+
const create = spans.find((span) => span.name === "Session.create")
|
|
82
|
+
expect(request).toBeDefined()
|
|
83
|
+
expect(Option.isNone(request!.parent)).toBe(true)
|
|
84
|
+
expect(create?.traceId).toBe(request!.traceId)
|
|
85
|
+
expect(create?.traceId).not.toBe(startup?.traceId)
|
|
86
|
+
}).pipe(Effect.withTracer(tracer), Effect.scoped)
|
|
87
|
+
})
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { expect } from "bun:test"
|
|
2
|
+
import { SdkPlugins } from "@zyro-sec/core/plugin/sdk"
|
|
3
|
+
import { define } from "@zyro-sec/plugin/effect/plugin"
|
|
4
|
+
import { Rpc } from "@zyro-sec/schema/rpc"
|
|
5
|
+
import { Context, Effect, Layer, Schema } from "effect"
|
|
6
|
+
import { HttpEffect, HttpRouter, HttpServer } from "effect/http"
|
|
7
|
+
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
8
|
+
import { it } from "../../core/test/lib/effect"
|
|
9
|
+
import { createEmbeddedRoutes } from "../src/routes"
|
|
10
|
+
import { pluginsLoaded } from "./fixture/routes"
|
|
11
|
+
|
|
12
|
+
const Broken = Rpc.define({
|
|
13
|
+
id: "broken",
|
|
14
|
+
methods: {
|
|
15
|
+
handler: { input: Schema.String, output: Schema.String },
|
|
16
|
+
schema: {
|
|
17
|
+
input: Schema.String.check(
|
|
18
|
+
Schema.makeFilter(() => {
|
|
19
|
+
throw new Error("private schema detail")
|
|
20
|
+
}),
|
|
21
|
+
),
|
|
22
|
+
output: Schema.String,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
events: {},
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
for (const method of ["handler", "schema"] as const) {
|
|
29
|
+
it.live(`returns HTTP 500 without exposing the ${method} defect`, () =>
|
|
30
|
+
Effect.gen(function* () {
|
|
31
|
+
const directory = yield* tmpdirScoped()
|
|
32
|
+
const context = yield* Layer.build(
|
|
33
|
+
createEmbeddedRoutes({
|
|
34
|
+
database: { path: ":memory:" },
|
|
35
|
+
models: { fetch: false },
|
|
36
|
+
config: { directory: directory.path, project: false, content: "{}" },
|
|
37
|
+
fs: { filewatcher: false },
|
|
38
|
+
}).pipe(Layer.provide(HttpServer.layerServices)),
|
|
39
|
+
)
|
|
40
|
+
const sdk = Context.get(context, SdkPlugins.Service)
|
|
41
|
+
yield* sdk.register(
|
|
42
|
+
define({
|
|
43
|
+
id: "broken-rpc",
|
|
44
|
+
effect: (ctx) =>
|
|
45
|
+
ctx.rpc
|
|
46
|
+
.register(Broken, {
|
|
47
|
+
handler: () => Effect.die(new Error("private handler detail")),
|
|
48
|
+
schema: Effect.succeed,
|
|
49
|
+
})
|
|
50
|
+
.pipe(Effect.asVoid, Effect.orDie),
|
|
51
|
+
}),
|
|
52
|
+
)
|
|
53
|
+
const handler = Context.get(context, HttpRouter.HttpRouter)
|
|
54
|
+
.asHttpEffect()
|
|
55
|
+
.pipe(HttpEffect.toWebHandlerWith(context))
|
|
56
|
+
yield* pluginsLoaded(context, directory.path)
|
|
57
|
+
const url = new URL(`/api/rpc/broken/${method}`, "http://opencode.local")
|
|
58
|
+
url.searchParams.set("location[directory]", directory.path)
|
|
59
|
+
const response = yield* Effect.promise(() =>
|
|
60
|
+
handler(
|
|
61
|
+
new Request(url, {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: { "content-type": "application/json" },
|
|
64
|
+
body: JSON.stringify({ input: "hello" }),
|
|
65
|
+
}),
|
|
66
|
+
),
|
|
67
|
+
)
|
|
68
|
+
expect(response.status).toBe(500)
|
|
69
|
+
expect(yield* Effect.promise(() => response.json())).toEqual({
|
|
70
|
+
_tag: "RpcInternalError",
|
|
71
|
+
type: "rpc.internal",
|
|
72
|
+
message: "RPC call failed",
|
|
73
|
+
})
|
|
74
|
+
}),
|
|
75
|
+
)
|
|
76
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { expect } from "bun:test"
|
|
2
|
+
import { Generate } from "@zyro-sec/core/generate"
|
|
3
|
+
import { makeLocationNode } from "@zyro-sec/util/effect/app-node"
|
|
4
|
+
import { Effect, Layer } from "effect"
|
|
5
|
+
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
6
|
+
import { it } from "../../core/test/lib/effect"
|
|
7
|
+
import { ServerFetch } from "../src/fetch"
|
|
8
|
+
|
|
9
|
+
// The success schema declares `text` as a string, so a number fails response encoding.
|
|
10
|
+
const generate = makeLocationNode({
|
|
11
|
+
service: Generate.Service,
|
|
12
|
+
layer: Layer.succeed(
|
|
13
|
+
Generate.Service,
|
|
14
|
+
Generate.Service.of({
|
|
15
|
+
text: () => Effect.succeed(42 as unknown as string),
|
|
16
|
+
image: () => Effect.die("unused"),
|
|
17
|
+
decisions: () => Effect.die("unused"),
|
|
18
|
+
video: {
|
|
19
|
+
start: () => Effect.die("unused"),
|
|
20
|
+
status: () => Effect.die("unused"),
|
|
21
|
+
result: () => Effect.die("unused"),
|
|
22
|
+
cancel: () => Effect.die("unused"),
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
),
|
|
26
|
+
deps: [],
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it.live("reports response encoding failures as server errors and request decoding failures as bad requests", () =>
|
|
30
|
+
Effect.gen(function* () {
|
|
31
|
+
const config = yield* tmpdirScoped()
|
|
32
|
+
const handler = yield* ServerFetch.make(
|
|
33
|
+
{
|
|
34
|
+
config: { directory: config.path, project: false },
|
|
35
|
+
database: { path: ":memory:" },
|
|
36
|
+
fs: { filewatcher: false },
|
|
37
|
+
models: { fetch: false },
|
|
38
|
+
},
|
|
39
|
+
{ overrides: [Generate.node.replace(generate)] },
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
const encoding = yield* request(handler, { prompt: "hello" })
|
|
43
|
+
expect(encoding.status).toBe(500)
|
|
44
|
+
expect(encoding.body).toMatchObject({
|
|
45
|
+
_tag: "UnknownError",
|
|
46
|
+
message: "Server failed to encode its response. Check server logs for details.",
|
|
47
|
+
ref: expect.stringMatching(/^err_/),
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const decoding = yield* request(handler, { prompt: 1 })
|
|
51
|
+
expect(decoding.status).toBe(400)
|
|
52
|
+
expect(decoding.body).toMatchObject({ _tag: "InvalidRequestError", kind: "Payload" })
|
|
53
|
+
}),
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
function request(handler: (request: Request) => Promise<Response>, body: unknown) {
|
|
57
|
+
return Effect.promise(async () => {
|
|
58
|
+
const response = await handler(
|
|
59
|
+
new Request("http://opencode.local/api/experimental/generate", {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: { "content-type": "application/json" },
|
|
62
|
+
body: JSON.stringify(body),
|
|
63
|
+
}),
|
|
64
|
+
)
|
|
65
|
+
const json: unknown = await response.json()
|
|
66
|
+
return { status: response.status, body: json }
|
|
67
|
+
})
|
|
68
|
+
}
|