@sixb/server 0.1.16 → 0.1.18
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/README.md +6 -0
- package/dist/auth/experience.d.ts +5 -1
- package/dist/auth/experience.d.ts.map +1 -1
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/shared-access.d.ts.map +1 -1
- package/dist/files/content.d.ts +0 -1
- package/dist/files/content.d.ts.map +1 -1
- package/dist/index.js +603 -456
- package/dist/index.js.map +34 -33
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/agents.d.ts +56 -28
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts.map +1 -1
- package/dist/routes/auth.d.ts +4 -3
- package/dist/routes/auth.d.ts.map +1 -1
- package/dist/routes/datasets.d.ts +4 -1
- package/dist/routes/datasets.d.ts.map +1 -1
- package/dist/routes/file-downloads.d.ts +98 -0
- package/dist/routes/file-downloads.d.ts.map +1 -0
- package/dist/routes/files.d.ts.map +1 -1
- package/dist/routes/models.d.ts +1 -0
- package/dist/routes/models.d.ts.map +1 -1
- package/dist/routes/objects.d.ts +24 -4
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/ontology.d.ts +4 -4
- package/dist/routes/project.d.ts +2 -0
- package/dist/routes/project.d.ts.map +1 -1
- package/dist/routes/status.d.ts +0 -4
- package/dist/routes/status.d.ts.map +1 -1
- package/dist/routes/workflows.d.ts +6 -0
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/events.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +11 -0
- package/dist/routes/ws/logs.d.ts.map +1 -1
- package/dist/routes/ws/subscription-hub.d.ts +71 -0
- package/dist/routes/ws/subscription-hub.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +255 -123
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +234 -110
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/ai-usage.d.ts +3 -0
- package/dist/schemas/ai-usage.d.ts.map +1 -1
- package/dist/schemas/auth.d.ts +9 -2
- package/dist/schemas/auth.d.ts.map +1 -1
- package/dist/schemas/common.d.ts +8 -0
- package/dist/schemas/common.d.ts.map +1 -1
- package/dist/schemas/datasets.d.ts +18 -3
- package/dist/schemas/datasets.d.ts.map +1 -1
- package/dist/schemas/event-payloads.d.ts +39 -0
- package/dist/schemas/event-payloads.d.ts.map +1 -1
- package/dist/schemas/files.d.ts +1 -1
- package/dist/schemas/files.d.ts.map +1 -1
- package/dist/schemas/models.d.ts +10 -0
- package/dist/schemas/models.d.ts.map +1 -1
- package/dist/schemas/objects.d.ts +32 -0
- package/dist/schemas/objects.d.ts.map +1 -1
- package/dist/schemas/ontology.d.ts +18 -18
- package/dist/schemas/project.d.ts +8 -0
- package/dist/schemas/project.d.ts.map +1 -1
- package/dist/schemas/status.d.ts +0 -26
- package/dist/schemas/status.d.ts.map +1 -1
- package/dist/schemas/workflows.d.ts +52 -0
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/auth/experience.ts +20 -2
- package/src/auth/public-routes.ts +10 -0
- package/src/auth/shared-access.ts +48 -6
- package/src/files/content.ts +43 -88
- package/src/registerRoutes.ts +2 -0
- package/src/routes/agents.ts +29 -9
- package/src/routes/ai-accounting.ts +1 -0
- package/src/routes/auth.ts +84 -19
- package/src/routes/datasets.ts +12 -43
- package/src/routes/file-downloads.ts +48 -0
- package/src/routes/files.ts +3 -1
- package/src/routes/models.ts +3 -0
- package/src/routes/objects.ts +25 -5
- package/src/routes/project.ts +2 -1
- package/src/routes/workflows.ts +5 -0
- package/src/routes/ws/events.ts +98 -133
- package/src/routes/ws/logs.ts +67 -5
- package/src/routes/ws/subscription-hub.ts +394 -0
- package/src/schemas/agents.ts +28 -3
- package/src/schemas/ai-accounting.ts +5 -0
- package/src/schemas/ai-usage.ts +3 -0
- package/src/schemas/auth.ts +5 -0
- package/src/schemas/common.ts +3 -0
- package/src/schemas/datasets.ts +23 -1
- package/src/schemas/event-payloads.ts +3 -1
- package/src/schemas/files.ts +7 -1
- package/src/schemas/models.ts +2 -0
- package/src/schemas/objects.ts +41 -2
- package/src/schemas/project.ts +4 -0
- package/src/schemas/status.ts +0 -6
- package/src/server.ts +7 -1
- package/dist/routes/ws/log-subscription-hub.d.ts +0 -50
- package/dist/routes/ws/log-subscription-hub.d.ts.map +0 -1
- package/src/routes/ws/log-subscription-hub.ts +0 -369
package/src/files/content.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { createHash } from "node:crypto"
|
|
2
|
-
import { type BlobInfo, type BlobStorage, type FileRef,
|
|
2
|
+
import { type BlobInfo, type BlobStorage, type FileRef, fileNameFor } from "@sixb/core"
|
|
3
3
|
import { BlobStorageError, supportsRangeRead } from "@sixb/core/blob-storage/server"
|
|
4
|
+
import {
|
|
5
|
+
canonicalMediaType,
|
|
6
|
+
resolveFileRefAtPath,
|
|
7
|
+
statFileRef,
|
|
8
|
+
} from "@sixb/core/internal/blob-storage"
|
|
4
9
|
import { ZodError } from "zod"
|
|
5
10
|
|
|
6
11
|
export type FileContentDisposition = "inline" | "attachment"
|
|
@@ -148,8 +153,8 @@ export function fileContentHeadResponses(options: FileContentResponsesOptions =
|
|
|
148
153
|
export async function createFileContentResponse(
|
|
149
154
|
input: FileContentResponseInput
|
|
150
155
|
): Promise<Response | null> {
|
|
151
|
-
const stat = await input.blobStorage
|
|
152
|
-
if (!stat
|
|
156
|
+
const stat = await statFileRef(input.blobStorage, input.fileRef)
|
|
157
|
+
if (!stat) {
|
|
153
158
|
return null
|
|
154
159
|
}
|
|
155
160
|
|
|
@@ -255,11 +260,6 @@ export async function createContextualFileContentResponse<TQuery extends FileCon
|
|
|
255
260
|
}
|
|
256
261
|
}
|
|
257
262
|
|
|
258
|
-
export function resolveFileRefAtPath(root: unknown, path: string): FileRef | null {
|
|
259
|
-
const value = resolveJsonPointer(root, path)
|
|
260
|
-
return isFileRef(value) ? value : null
|
|
261
|
-
}
|
|
262
|
-
|
|
263
263
|
function fileContentNotFound(
|
|
264
264
|
set: { status?: number | string },
|
|
265
265
|
message: string
|
|
@@ -268,14 +268,6 @@ function fileContentNotFound(
|
|
|
268
268
|
return { error: message }
|
|
269
269
|
}
|
|
270
270
|
|
|
271
|
-
function blobMatchesFileRef(stat: BlobInfo, fileRef: FileRef): boolean {
|
|
272
|
-
return (
|
|
273
|
-
stat.blobId === fileRef.blobId &&
|
|
274
|
-
stat.digest === fileRef.digest &&
|
|
275
|
-
stat.sizeBytes === fileRef.sizeBytes
|
|
276
|
-
)
|
|
277
|
-
}
|
|
278
|
-
|
|
279
271
|
function matchesIfNoneMatch(header: string | null | undefined, etag: string): boolean {
|
|
280
272
|
return (
|
|
281
273
|
header?.trim() === "*" ||
|
|
@@ -283,43 +275,32 @@ function matchesIfNoneMatch(header: string | null | undefined, etag: string): bo
|
|
|
283
275
|
)
|
|
284
276
|
}
|
|
285
277
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
current = (current as Record<string, unknown>)[segment]
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
return current
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
function decodeJsonPointerSegment(segment: string): string {
|
|
321
|
-
return segment.replaceAll("~1", "/").replaceAll("~0", "~")
|
|
322
|
-
}
|
|
278
|
+
/**
|
|
279
|
+
* The only types a browser displays instead of downloading: raster images, PDF, and plain-text
|
|
280
|
+
* formats. Everything else, including HTML, SVG, and XML, downloads.
|
|
281
|
+
*/
|
|
282
|
+
const INLINE_MEDIA_TYPES: ReadonlySet<string> = new Set([
|
|
283
|
+
"application/json",
|
|
284
|
+
"application/pdf",
|
|
285
|
+
"image/avif",
|
|
286
|
+
"image/bmp",
|
|
287
|
+
"image/gif",
|
|
288
|
+
"image/jpeg",
|
|
289
|
+
"image/png",
|
|
290
|
+
"image/webp",
|
|
291
|
+
"text/markdown",
|
|
292
|
+
"text/plain",
|
|
293
|
+
])
|
|
294
|
+
|
|
295
|
+
// Keeps any file from running script on the API origin, even when a browser renders it as a page.
|
|
296
|
+
// A sandboxed page cannot run script, but Chrome's PDF viewer does not render in one, so PDFs go
|
|
297
|
+
// without `sandbox`: `default-src 'none'` still blocks script, and `object-src 'self'` lets
|
|
298
|
+
// browsers that show a PDF as an embedded object load it. `style-src 'unsafe-inline'` lets
|
|
299
|
+
// browsers keep the styling of their built-in viewers. Video and audio play in media elements,
|
|
300
|
+
// which ignore the policy of the file they load.
|
|
301
|
+
const FILE_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inline'; sandbox"
|
|
302
|
+
const PDF_CONTENT_SECURITY_POLICY =
|
|
303
|
+
"default-src 'none'; style-src 'unsafe-inline'; object-src 'self'"
|
|
323
304
|
|
|
324
305
|
function fileContentHeaders(
|
|
325
306
|
fileRef: FileRef,
|
|
@@ -327,15 +308,22 @@ function fileContentHeaders(
|
|
|
327
308
|
requestedDisposition: FileContentDisposition | undefined,
|
|
328
309
|
canReadRange: boolean
|
|
329
310
|
): Headers {
|
|
330
|
-
|
|
311
|
+
// Parsed again rather than trusted: stored references may predate media type validation.
|
|
312
|
+
const mediaType = canonicalMediaType(fileRef.mediaType) ?? "application/octet-stream"
|
|
313
|
+
// Canonical, so everything before the first ";" is the lowercased type/subtype.
|
|
314
|
+
const baseType = mediaType.split(";", 1)[0]
|
|
331
315
|
const disposition =
|
|
332
|
-
(requestedDisposition ?? "inline") === "inline" &&
|
|
316
|
+
(requestedDisposition ?? "inline") === "inline" && INLINE_MEDIA_TYPES.has(baseType)
|
|
333
317
|
? "inline"
|
|
334
318
|
: "attachment"
|
|
335
319
|
const headers = new Headers()
|
|
336
320
|
headers.set("content-type", mediaType)
|
|
337
321
|
headers.set("content-length", stat.sizeBytes.toString())
|
|
338
322
|
headers.set("content-disposition", contentDispositionHeader(disposition, fileNameFor(fileRef)))
|
|
323
|
+
headers.set(
|
|
324
|
+
"content-security-policy",
|
|
325
|
+
baseType === "application/pdf" ? PDF_CONTENT_SECURITY_POLICY : FILE_CONTENT_SECURITY_POLICY
|
|
326
|
+
)
|
|
339
327
|
// Metadata belongs to the reference, not the blob; changing it must also invalidate caches.
|
|
340
328
|
const representation = JSON.stringify([
|
|
341
329
|
fileRef.digest,
|
|
@@ -424,39 +412,6 @@ function rangeNotSatisfiableResponse(sizeBytes: number): Response {
|
|
|
424
412
|
return new Response(null, { status: 416, headers })
|
|
425
413
|
}
|
|
426
414
|
|
|
427
|
-
function canRenderInline(mediaType: string): boolean {
|
|
428
|
-
const baseType = mediaType.split(";")[0]?.trim().toLowerCase()
|
|
429
|
-
if (!baseType) {
|
|
430
|
-
return false
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
if (
|
|
434
|
-
baseType === "text/html" ||
|
|
435
|
-
baseType === "image/svg+xml" ||
|
|
436
|
-
baseType === "application/xhtml+xml"
|
|
437
|
-
) {
|
|
438
|
-
return false
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
return (
|
|
442
|
-
baseType === "application/pdf" ||
|
|
443
|
-
baseType === "text/plain" ||
|
|
444
|
-
baseType === "text/markdown" ||
|
|
445
|
-
baseType === "application/json" ||
|
|
446
|
-
baseType.startsWith("image/")
|
|
447
|
-
)
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
function fileNameFor(fileRef: FileRef): string {
|
|
451
|
-
const fromFileName = fileRef.fileName?.split(/[\\/]/).filter(Boolean).at(-1)
|
|
452
|
-
if (fromFileName) {
|
|
453
|
-
return fromFileName
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
const fromLogicalPath = fileRef.logicalPath?.split(/[\\/]/).filter(Boolean).at(-1)
|
|
457
|
-
return fromLogicalPath || `${fileRef.blobId}.bin`
|
|
458
|
-
}
|
|
459
|
-
|
|
460
415
|
function contentDispositionHeader(disposition: FileContentDisposition, fileName: string): string {
|
|
461
416
|
const asciiFileName = fileName.replace(/[^\x20-\x7e]/g, "_").replaceAll('"', "'")
|
|
462
417
|
return `${disposition}; filename="${asciiFileName}"; filename*=UTF-8''${encodeURIComponent(fileName)}`
|
package/src/registerRoutes.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { registerConnectorConnectionRoutes } from "./routes/connector-connection
|
|
|
14
14
|
import { registerConnectorRoutes } from "./routes/connectors"
|
|
15
15
|
import { registerDatasetRoutes } from "./routes/datasets"
|
|
16
16
|
import { registerEventRoutes } from "./routes/events"
|
|
17
|
+
import { registerFileDownloadRoutes } from "./routes/file-downloads"
|
|
17
18
|
import { registerFileRoutes } from "./routes/files"
|
|
18
19
|
import { registerLinkRoutes } from "./routes/links"
|
|
19
20
|
import { registerLogRoutes } from "./routes/logs"
|
|
@@ -56,6 +57,7 @@ export function registerHttpRoutes(app: Elysia, host: SixbHostView, options: Htt
|
|
|
56
57
|
registerObjectRoutes(app, host)
|
|
57
58
|
registerActionRoutes(app, host)
|
|
58
59
|
registerFileRoutes(app, host)
|
|
60
|
+
registerFileDownloadRoutes(app, host)
|
|
59
61
|
registerActionRunRoutes(app, host)
|
|
60
62
|
registerAgentRoutes(app, host)
|
|
61
63
|
registerModelRoutes(app, host)
|
package/src/routes/agents.ts
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
AgentRequestError,
|
|
3
|
+
type AgentRequestErrorCode,
|
|
4
|
+
type AgentRunView,
|
|
5
|
+
type FileRef,
|
|
6
|
+
type SixbHostView,
|
|
7
|
+
} from "@sixb/core"
|
|
8
|
+
import {
|
|
9
|
+
isRetryableAgentRun,
|
|
10
|
+
publishAgentRunCancel,
|
|
11
|
+
publishAgentRunFinished,
|
|
12
|
+
} from "@sixb/core/internal/agents"
|
|
3
13
|
import { agentRunStreamId } from "@sixb/core/internal/agents/streams"
|
|
14
|
+
import { resolveFileRefAtPath } from "@sixb/core/internal/blob-storage"
|
|
4
15
|
import {
|
|
5
16
|
type AgentContextCheckpointRecord,
|
|
6
17
|
type AgentMessageRecord,
|
|
@@ -19,7 +30,6 @@ import {
|
|
|
19
30
|
createFileContentResponse,
|
|
20
31
|
fileContentGetResponses,
|
|
21
32
|
fileContentHeadResponses,
|
|
22
|
-
resolveFileRefAtPath,
|
|
23
33
|
} from "../files/content"
|
|
24
34
|
import { SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT } from "../openapi/security"
|
|
25
35
|
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
@@ -29,6 +39,7 @@ import {
|
|
|
29
39
|
AgentMessageListResponseSchema,
|
|
30
40
|
AgentMessageSchema,
|
|
31
41
|
AgentMessagesQuerySchema,
|
|
42
|
+
AgentRequestErrorResponseSchema,
|
|
32
43
|
AgentRunListQuerySchema,
|
|
33
44
|
AgentRunListResponseSchema,
|
|
34
45
|
AgentRunParamsSchema,
|
|
@@ -170,7 +181,9 @@ async function publishQueuedRunCancellation(
|
|
|
170
181
|
function handleAgentRouteError(
|
|
171
182
|
error: unknown,
|
|
172
183
|
set: { status?: number | string; headers?: unknown }
|
|
173
|
-
):
|
|
184
|
+
):
|
|
185
|
+
| ReturnType<typeof handleRouteError>
|
|
186
|
+
| { readonly error: string; readonly code: AgentRequestErrorCode } {
|
|
174
187
|
// A duplicate id is a conflict, not a bad request. Map it to a generic 409 rather than echoing the
|
|
175
188
|
// provider's raw message (which leaks the id, project, and storage prefix).
|
|
176
189
|
if (error instanceof AgentStorageError && error.code === "duplicate_id") {
|
|
@@ -203,12 +216,16 @@ function handleAgentRouteError(
|
|
|
203
216
|
case "invalid_sandbox_params":
|
|
204
217
|
case "agent_selector_removed":
|
|
205
218
|
case "invalid_context":
|
|
219
|
+
case "invalid_attachment":
|
|
206
220
|
case "invalid_model_selection":
|
|
221
|
+
case "invalid_locale":
|
|
222
|
+
case "invalid_message":
|
|
207
223
|
case "model_not_found":
|
|
208
224
|
set.status = 400
|
|
209
225
|
break
|
|
210
226
|
}
|
|
211
|
-
|
|
227
|
+
// The code lets clients react without parsing the English message.
|
|
228
|
+
return { error: error.message, code: error.code }
|
|
212
229
|
}
|
|
213
230
|
|
|
214
231
|
return handleRouteError(error, set)
|
|
@@ -594,8 +611,11 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
594
611
|
const requestInput = {
|
|
595
612
|
threadId: thread.id,
|
|
596
613
|
text: parsed.text,
|
|
614
|
+
continue: parsed.continue,
|
|
597
615
|
model: parsed.model,
|
|
598
616
|
reasoning: parsed.reasoning,
|
|
617
|
+
timeZone: parsed.timeZone,
|
|
618
|
+
locale: parsed.locale,
|
|
599
619
|
attachments: parsed.attachments as readonly FileRef[] | undefined,
|
|
600
620
|
context: parsed.context,
|
|
601
621
|
messageId: parsed.messageId,
|
|
@@ -615,7 +635,7 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
615
635
|
body: PostAgentMessageBodySchema,
|
|
616
636
|
response: {
|
|
617
637
|
202: PostAgentMessageResponseSchema,
|
|
618
|
-
400:
|
|
638
|
+
400: AgentRequestErrorResponseSchema,
|
|
619
639
|
403: ErrorResponseSchema,
|
|
620
640
|
404: ErrorResponseSchema,
|
|
621
641
|
409: ErrorResponseSchema,
|
|
@@ -747,9 +767,9 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
747
767
|
set.status = 404
|
|
748
768
|
return { error: "Agent run not found" }
|
|
749
769
|
}
|
|
750
|
-
if (failedRun
|
|
770
|
+
if (!isRetryableAgentRun(failedRun)) {
|
|
751
771
|
set.status = 409
|
|
752
|
-
return { error: "Only failed agent runs can be retried" }
|
|
772
|
+
return { error: "Only failed or interrupted agent runs can be retried" }
|
|
753
773
|
}
|
|
754
774
|
|
|
755
775
|
const { run } = await sixb.agent.runs.retry(failedRun.id)
|
|
@@ -773,7 +793,7 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
773
793
|
501: ErrorResponseSchema,
|
|
774
794
|
},
|
|
775
795
|
detail: {
|
|
776
|
-
summary: "Retry a failed agent run",
|
|
796
|
+
summary: "Retry a failed or interrupted agent run",
|
|
777
797
|
tags: [OPENAPI_TAGS.agentRuns.name],
|
|
778
798
|
operationId: "retryAgentRun",
|
|
779
799
|
security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
@@ -109,6 +109,7 @@ function serializeModelCall(item: AiModelCallAccountingItem) {
|
|
|
109
109
|
callId: item.usage.callId,
|
|
110
110
|
providerId: item.usage.providerId,
|
|
111
111
|
requestedModelId: item.usage.requestedModelId,
|
|
112
|
+
modelKind: item.usage.modelKind,
|
|
112
113
|
providerIds: item.usage.providerIds ?? null,
|
|
113
114
|
requestedReasoning: item.usage.requestedReasoning,
|
|
114
115
|
responseModelId: item.usage.responseModelId,
|
package/src/routes/auth.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
isOidcAuthStrategy,
|
|
23
23
|
type MagicLinkAuthStrategy,
|
|
24
24
|
type MemberSummary,
|
|
25
|
+
type OidcStartSignInResult,
|
|
25
26
|
SignInRefusedError,
|
|
26
27
|
shouldUseSecureCookies,
|
|
27
28
|
verifyCsrfToken,
|
|
@@ -51,7 +52,7 @@ import { CSRF_TOKEN_RESPONSE_HEADER_NAME } from "../auth/csrf"
|
|
|
51
52
|
import {
|
|
52
53
|
customAuthExperienceAssetResponse,
|
|
53
54
|
customAuthExperienceResponse,
|
|
54
|
-
type
|
|
55
|
+
type ProjectAuthExperience,
|
|
55
56
|
} from "../auth/experience"
|
|
56
57
|
import { ClientAddressRateLimiter } from "../auth/rate-limit"
|
|
57
58
|
import { requestCaller } from "../auth/scope"
|
|
@@ -142,7 +143,7 @@ export interface AuthRoutesOptions {
|
|
|
142
143
|
request: Request,
|
|
143
144
|
input: AuthInvitationRedirectInput
|
|
144
145
|
) => AuthInvitationRedirectContext
|
|
145
|
-
readonly authExperience?:
|
|
146
|
+
readonly authExperience?: ProjectAuthExperience
|
|
146
147
|
}
|
|
147
148
|
|
|
148
149
|
export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: AuthRoutesOptions) {
|
|
@@ -440,7 +441,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
|
|
|
440
441
|
})
|
|
441
442
|
// Access tokens are not sessions; a native client's session has no cookies to renew.
|
|
442
443
|
if (!caller.authenticated || caller.credentialSource !== "session") {
|
|
443
|
-
return
|
|
444
|
+
return signedOutSessionResponse(host)
|
|
444
445
|
}
|
|
445
446
|
// A native client belongs to no web app; tell it whether its user may open Atlas.
|
|
446
447
|
if (caller.session.bearer) {
|
|
@@ -453,7 +454,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
|
|
|
453
454
|
? await host.auth.getSession(request, { ...authOptions, sessionActivity: "foreground" })
|
|
454
455
|
: caller
|
|
455
456
|
if (!session.authenticated) {
|
|
456
|
-
return
|
|
457
|
+
return signedOutSessionResponse(host)
|
|
457
458
|
}
|
|
458
459
|
|
|
459
460
|
const cookieOptions = host.auth.getCookieOptions(authOptions)
|
|
@@ -742,7 +743,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
|
|
|
742
743
|
summary: "Create a personal access token",
|
|
743
744
|
tags: [OPENAPI_TAGS.authAccessTokens.name],
|
|
744
745
|
operationId: "createAuthPersonalAccessToken",
|
|
745
|
-
security:
|
|
746
|
+
security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
746
747
|
},
|
|
747
748
|
}
|
|
748
749
|
)
|
|
@@ -978,7 +979,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
|
|
|
978
979
|
summary: "Create an access token for an auth service account",
|
|
979
980
|
tags: [OPENAPI_TAGS.authServiceAccounts.name],
|
|
980
981
|
operationId: "createAuthServiceAccountAccessToken",
|
|
981
|
-
security:
|
|
982
|
+
security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
982
983
|
},
|
|
983
984
|
}
|
|
984
985
|
)
|
|
@@ -1383,9 +1384,10 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
|
|
|
1383
1384
|
const existingPending = getCookie(request, MAGIC_LINK_PENDING_COOKIE_NAME)?.trim()
|
|
1384
1385
|
response.headers.append(
|
|
1385
1386
|
"set-cookie",
|
|
1386
|
-
|
|
1387
|
+
callbackCookieHeader({
|
|
1387
1388
|
request,
|
|
1388
1389
|
host,
|
|
1390
|
+
name: MAGIC_LINK_PENDING_COOKIE_NAME,
|
|
1389
1391
|
value: result.status === "sent" ? pending.secret : existingPending || pending.secret,
|
|
1390
1392
|
maxAgeSeconds: magicLinkPendingCookieMaxAgeSeconds(strategy),
|
|
1391
1393
|
})
|
|
@@ -1401,7 +1403,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
|
|
|
1401
1403
|
returnTo: authRedirect.returnTo,
|
|
1402
1404
|
requestOrigin: authRedirect.requestOrigin,
|
|
1403
1405
|
})
|
|
1404
|
-
return
|
|
1406
|
+
return oidcSignInRedirect(request, host, result)
|
|
1405
1407
|
}
|
|
1406
1408
|
|
|
1407
1409
|
return strategyNotImplementedResponse("Sign-in is not implemented yet.")
|
|
@@ -1441,7 +1443,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
|
|
|
1441
1443
|
returnTo: authRedirect.returnTo,
|
|
1442
1444
|
requestOrigin: authRedirect.requestOrigin,
|
|
1443
1445
|
})
|
|
1444
|
-
return
|
|
1446
|
+
return oidcSignInRedirect(request, host, result)
|
|
1445
1447
|
}
|
|
1446
1448
|
|
|
1447
1449
|
return strategyNotImplementedResponse("Sign-in is not implemented yet.")
|
|
@@ -1528,8 +1530,15 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
|
|
|
1528
1530
|
const now = new Date()
|
|
1529
1531
|
const sessionCredential = createSessionCredential()
|
|
1530
1532
|
const device = resolveSessionDevice(request)
|
|
1533
|
+
const state = new URL(request.url).searchParams.get("state")
|
|
1531
1534
|
|
|
1535
|
+
let response: Response
|
|
1532
1536
|
try {
|
|
1537
|
+
if (!state || !matchesOidcStateCookie(request, state)) {
|
|
1538
|
+
throw new Error(
|
|
1539
|
+
"[SixbServer] OIDC callback reached a browser that did not start this sign-in."
|
|
1540
|
+
)
|
|
1541
|
+
}
|
|
1533
1542
|
const authOptions = resolveAuthOptions(options, request)
|
|
1534
1543
|
const result = await strategy.completeOidcSignIn({
|
|
1535
1544
|
projectId: host.id,
|
|
@@ -1546,7 +1555,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
|
|
|
1546
1555
|
},
|
|
1547
1556
|
})
|
|
1548
1557
|
|
|
1549
|
-
|
|
1558
|
+
response = sessionCallbackCompletionResponse({
|
|
1550
1559
|
host,
|
|
1551
1560
|
request,
|
|
1552
1561
|
apiOrigin: options.resolveAuthRequestOrigin(request),
|
|
@@ -1557,11 +1566,25 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
|
|
|
1557
1566
|
})
|
|
1558
1567
|
} catch (error) {
|
|
1559
1568
|
logAuthCallbackError("OIDC", error)
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1569
|
+
response =
|
|
1570
|
+
error instanceof SignInRefusedError
|
|
1571
|
+
? htmlMessageResponse(signInRefusalMessage(error), 403, "You can't sign in")
|
|
1572
|
+
: htmlMessageResponse("This sign-in attempt could not be completed.", 400)
|
|
1564
1573
|
}
|
|
1574
|
+
// Completed or refused, this attempt is over for this browser.
|
|
1575
|
+
if (state) {
|
|
1576
|
+
response.headers.append(
|
|
1577
|
+
"set-cookie",
|
|
1578
|
+
callbackCookieHeader({
|
|
1579
|
+
request,
|
|
1580
|
+
host,
|
|
1581
|
+
name: oidcStateCookieName(state),
|
|
1582
|
+
value: "",
|
|
1583
|
+
maxAgeSeconds: 0,
|
|
1584
|
+
})
|
|
1585
|
+
)
|
|
1586
|
+
}
|
|
1587
|
+
return response
|
|
1565
1588
|
}
|
|
1566
1589
|
|
|
1567
1590
|
return strategyNotImplementedResponse("Auth callback is not implemented yet.")
|
|
@@ -1670,16 +1693,17 @@ function matchesMagicLinkRequester(pendingSecret: string, requesterHash: string)
|
|
|
1670
1693
|
return expected.length === provided.length && timingSafeEqual(expected, provided)
|
|
1671
1694
|
}
|
|
1672
1695
|
|
|
1673
|
-
// SameSite=Lax, not Strict: the emailed link
|
|
1674
|
-
//
|
|
1675
|
-
function
|
|
1696
|
+
// SameSite=Lax, not Strict: the emailed link and the identity provider's redirect arrive as
|
|
1697
|
+
// cross-site top-level navigations, and the cookie must accompany that GET.
|
|
1698
|
+
function callbackCookieHeader(params: {
|
|
1676
1699
|
readonly request: Request
|
|
1677
1700
|
readonly host: SixbHostView
|
|
1701
|
+
readonly name: string
|
|
1678
1702
|
readonly value: string
|
|
1679
1703
|
readonly maxAgeSeconds: number
|
|
1680
1704
|
}): string {
|
|
1681
1705
|
const parts = [
|
|
1682
|
-
`${
|
|
1706
|
+
`${params.name}=${params.value}`,
|
|
1683
1707
|
"Path=/auth/callback",
|
|
1684
1708
|
"SameSite=Lax",
|
|
1685
1709
|
"HttpOnly",
|
|
@@ -1736,9 +1760,10 @@ async function completeMagicLinkCallback(input: {
|
|
|
1736
1760
|
if (input.clearPendingCookie) {
|
|
1737
1761
|
response.headers.append(
|
|
1738
1762
|
"set-cookie",
|
|
1739
|
-
|
|
1763
|
+
callbackCookieHeader({
|
|
1740
1764
|
request: input.request,
|
|
1741
1765
|
host: input.host,
|
|
1766
|
+
name: MAGIC_LINK_PENDING_COOKIE_NAME,
|
|
1742
1767
|
value: "",
|
|
1743
1768
|
maxAgeSeconds: 0,
|
|
1744
1769
|
})
|
|
@@ -1750,6 +1775,41 @@ async function completeMagicLinkCallback(input: {
|
|
|
1750
1775
|
}
|
|
1751
1776
|
}
|
|
1752
1777
|
|
|
1778
|
+
// Binds an OIDC sign-in to the browser that started it: only that browser holds a cookie carrying
|
|
1779
|
+
// the attempt's state, so the callback URL completes nowhere else. Each attempt has its own cookie,
|
|
1780
|
+
// so a sign-in in another tab doesn't displace it. The name is a digest and the value is encoded
|
|
1781
|
+
// because the strategy owns the state's format.
|
|
1782
|
+
function oidcStateCookieName(state: string): string {
|
|
1783
|
+
return `sixb_oidc_${createHash("sha256").update(state).digest("base64url").slice(0, 22)}`
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
function oidcSignInRedirect(
|
|
1787
|
+
request: Request,
|
|
1788
|
+
host: SixbHostView,
|
|
1789
|
+
result: OidcStartSignInResult
|
|
1790
|
+
): Response {
|
|
1791
|
+
const response = redirectResponse(result.redirectTo)
|
|
1792
|
+
response.headers.append(
|
|
1793
|
+
"set-cookie",
|
|
1794
|
+
callbackCookieHeader({
|
|
1795
|
+
request,
|
|
1796
|
+
host,
|
|
1797
|
+
name: oidcStateCookieName(result.state),
|
|
1798
|
+
value: encodeURIComponent(result.state),
|
|
1799
|
+
maxAgeSeconds: Math.max(0, Math.floor((result.expiresAt.getTime() - Date.now()) / 1000)),
|
|
1800
|
+
})
|
|
1801
|
+
)
|
|
1802
|
+
return response
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
function matchesOidcStateCookie(request: Request, state: string): boolean {
|
|
1806
|
+
const held = getCookie(request, oidcStateCookieName(state))
|
|
1807
|
+
if (!held) return false
|
|
1808
|
+
const expected = Buffer.from(encodeURIComponent(state))
|
|
1809
|
+
const provided = Buffer.from(held)
|
|
1810
|
+
return expected.length === provided.length && timingSafeEqual(expected, provided)
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1753
1813
|
function bearerTokensBody(
|
|
1754
1814
|
tokens: { readonly accessToken: string; readonly refreshToken: string },
|
|
1755
1815
|
bearer: { readonly accessExpiresAt: Date },
|
|
@@ -1948,6 +2008,11 @@ function authSessionBody(
|
|
|
1948
2008
|
}
|
|
1949
2009
|
}
|
|
1950
2010
|
|
|
2011
|
+
/** No session. Says whether auth is on, so a browser shell knows whether to ask for sign-in. */
|
|
2012
|
+
function signedOutSessionResponse(host: SixbHostView): Response {
|
|
2013
|
+
return jsonResponse({ authenticated: false as const, authEnabled: host.auth.isEnabled() }, 200)
|
|
2014
|
+
}
|
|
2015
|
+
|
|
1951
2016
|
function authSessionJsonResponse(
|
|
1952
2017
|
body: AuthenticatedAuthSessionResponse,
|
|
1953
2018
|
setCookies: readonly string[]
|
package/src/routes/datasets.ts
CHANGED
|
@@ -184,32 +184,6 @@ function datasetVersionNotFound(datasetId: string, versionId?: string) {
|
|
|
184
184
|
})
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
function parseColumns(value: string | undefined): readonly string[] | undefined {
|
|
188
|
-
if (!value) return undefined
|
|
189
|
-
|
|
190
|
-
const columns = value
|
|
191
|
-
.split(",")
|
|
192
|
-
.map((column) => column.trim())
|
|
193
|
-
.filter(Boolean)
|
|
194
|
-
|
|
195
|
-
return columns.length > 0 ? columns : undefined
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function resolveColumns(version: DatasetVersion, requested: readonly string[] | undefined) {
|
|
199
|
-
const available = new Set(version.schema.columns.map((column) => column.name))
|
|
200
|
-
const selected = requested ?? version.schema.columns.map((column) => column.name)
|
|
201
|
-
|
|
202
|
-
for (const column of selected) {
|
|
203
|
-
if (!available.has(column)) {
|
|
204
|
-
throw new Error(
|
|
205
|
-
`Dataset '${version.datasetId}' does not have column '${column}' at version '${version.versionId}'`
|
|
206
|
-
)
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return selected
|
|
211
|
-
}
|
|
212
|
-
|
|
213
187
|
async function collectRows(rows: AsyncIterable<Readonly<Record<string, unknown>>>) {
|
|
214
188
|
const collected: Record<string, unknown>[] = []
|
|
215
189
|
for await (const row of rows) {
|
|
@@ -341,29 +315,23 @@ export function registerDatasetRoutes(app: Elysia, host: SixbHostView) {
|
|
|
341
315
|
const { params, query, set } = context
|
|
342
316
|
const sixb = requireRequestSixb(context)
|
|
343
317
|
try {
|
|
344
|
-
requireDataset(sixb, params.datasetId)
|
|
318
|
+
const dataset = requireDataset(sixb, params.datasetId)
|
|
345
319
|
const parsed = DatasetRowsQuerySchema.parse(query)
|
|
346
320
|
const limit = parseLimit(parsed.limit, DEFAULT_ROW_LIMIT, MAX_ROW_LIMIT)
|
|
347
321
|
const offset = parseOffset(parsed.offset)
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
322
|
+
const page = await sixb.datasets.readRows(dataset, {
|
|
323
|
+
versionId: parsed.versionId,
|
|
324
|
+
// An empty `?columns=` reads every column, like omitting it.
|
|
325
|
+
columns: parsed.columns?.length ? parsed.columns : undefined,
|
|
326
|
+
limit,
|
|
327
|
+
offset,
|
|
328
|
+
})
|
|
329
|
+
if (!page) {
|
|
353
330
|
throw datasetVersionNotFound(params.datasetId, parsed.versionId)
|
|
354
331
|
}
|
|
355
332
|
|
|
356
|
-
const
|
|
357
|
-
const
|
|
358
|
-
const rows = await collectRows(
|
|
359
|
-
host.lakeStorage.readRows({
|
|
360
|
-
datasetId: params.datasetId,
|
|
361
|
-
versionId: version.versionId,
|
|
362
|
-
columns: requestedColumns,
|
|
363
|
-
limit,
|
|
364
|
-
offset,
|
|
365
|
-
})
|
|
366
|
-
)
|
|
333
|
+
const { version, columns, redactions } = page
|
|
334
|
+
const rows = await collectRows(page.rows)
|
|
367
335
|
const hasMore =
|
|
368
336
|
version.rowCount === undefined
|
|
369
337
|
? rows.length === limit
|
|
@@ -374,6 +342,7 @@ export function registerDatasetRoutes(app: Elysia, host: SixbHostView) {
|
|
|
374
342
|
versionId: version.versionId,
|
|
375
343
|
version: serializeDatasetVersion(version),
|
|
376
344
|
columns: [...columns],
|
|
345
|
+
...(redactions === undefined ? {} : { redactions }),
|
|
377
346
|
rows,
|
|
378
347
|
count: rows.length,
|
|
379
348
|
limit,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { SixbHostView } from "@sixb/core"
|
|
2
|
+
import { FILE_DOWNLOAD_ROUTE_PREFIX, resolveFileDownload } from "@sixb/core/internal/blob-storage"
|
|
3
|
+
import type { Elysia } from "elysia"
|
|
4
|
+
import { createFileContentResponse } from "../files/content"
|
|
5
|
+
|
|
6
|
+
interface FileDownloadContext {
|
|
7
|
+
readonly params: { readonly token: string }
|
|
8
|
+
readonly request: Request
|
|
9
|
+
readonly set: { status?: number | string }
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Serves the URLs `blobs.createDownloadUrl()` issues. Public, because their readers — social
|
|
14
|
+
* networks fetching media to publish — cannot hold a session: the token is the credential.
|
|
15
|
+
* Hidden from OpenAPI like webhooks; clients receive these URLs, they never build them.
|
|
16
|
+
*/
|
|
17
|
+
export function registerFileDownloadRoutes(app: Elysia, host: SixbHostView) {
|
|
18
|
+
const path = `${FILE_DOWNLOAD_ROUTE_PREFIX}:token`
|
|
19
|
+
return app
|
|
20
|
+
.get(path, (context) => fileDownloadResponse(host, context), { detail: { hide: true } })
|
|
21
|
+
.head(path, (context) => fileDownloadResponse(host, context, { head: true }), {
|
|
22
|
+
detail: { hide: true },
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function fileDownloadResponse(
|
|
27
|
+
host: SixbHostView,
|
|
28
|
+
{ params, request, set }: FileDownloadContext,
|
|
29
|
+
options: { readonly head?: boolean } = {}
|
|
30
|
+
): Promise<Response | { readonly error: string }> {
|
|
31
|
+
// An unknown, expired, or revoked token and a deleted blob all answer the same 404.
|
|
32
|
+
const fileRef = await resolveFileDownload(host, params.token)
|
|
33
|
+
const response =
|
|
34
|
+
fileRef &&
|
|
35
|
+
(await createFileContentResponse({
|
|
36
|
+
blobStorage: host.blobStorage,
|
|
37
|
+
fileRef,
|
|
38
|
+
head: options.head,
|
|
39
|
+
rangeHeader: request.headers.get("range"),
|
|
40
|
+
ifNoneMatchHeader: request.headers.get("if-none-match"),
|
|
41
|
+
ifRangeHeader: request.headers.get("if-range"),
|
|
42
|
+
}))
|
|
43
|
+
if (!response) {
|
|
44
|
+
set.status = 404
|
|
45
|
+
return { error: "File not found" }
|
|
46
|
+
}
|
|
47
|
+
return response
|
|
48
|
+
}
|