Update hivegrid-cloud reference to latest main

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
davidandClaude Fable 5 committed 2026-07-08 16:15:37 +02:00
1 parent e98d38e716
commit 7b21524c86
3781 files changed
+373484 -225607

No files matched your search

@@ -1,5 +1,5 @@
{
"name": "@toolctrl/storage",
"name": "@hivegrid/storage",
"version": "0.0.0",
"private": true,
"type": "module",
@@ -0,0 +1,120 @@
import { describe, expect, it } from "vitest";
import { authorizeStorageUploadConstraints } from "./index.js";
// Covers the normalizeByteSize() guard (returns null for non-number / non-finite
// / negative sizes). These cases are only reachable once maxBytes is a valid
// non-negative integer, so the size-vs-limit comparison short-circuits to null
// and the upload is NOT rejected as object_too_large.
describe("authorizeStorageUploadConstraints byteSize normalization", () => {
it("allows the upload when byteSize is omitted even though maxBytes is set", () => {
expect(
authorizeStorageUploadConstraints({
operation: "upload",
contentType: "image/png",
maxBytes: 2048,
}),
).toBeNull();
});
it("treats a negative byteSize as unknown and does not reject it", () => {
expect(
authorizeStorageUploadConstraints({
operation: "upload",
contentType: "image/png",
byteSize: -1,
maxBytes: 2048,
}),
).toBeNull();
});
it("treats a non-finite byteSize as unknown and does not reject it", () => {
expect(
authorizeStorageUploadConstraints({
operation: "upload",
contentType: "image/png",
byteSize: Number.POSITIVE_INFINITY,
maxBytes: 2048,
}),
).toBeNull();
expect(
authorizeStorageUploadConstraints({
operation: "upload",
contentType: "image/png",
byteSize: Number.NaN,
maxBytes: 2048,
}),
).toBeNull();
});
it("floors a fractional byteSize before comparing against the limit", () => {
// 2048.9 floors to 2048 which is exactly maxBytes -> not too large.
expect(
authorizeStorageUploadConstraints({
operation: "upload",
contentType: "image/png",
byteSize: 2048.9,
maxBytes: 2048,
}),
).toBeNull();
// 2049.9 floors to 2049 which exceeds maxBytes -> rejected.
expect(
authorizeStorageUploadConstraints({
operation: "upload",
contentType: "image/png",
byteSize: 2049.9,
maxBytes: 2048,
}),
).toEqual({ allowed: false, operation: "upload", reason: "object_too_large" });
});
it("ignores a negative maxBytes limit entirely", () => {
// maxBytes < 0 fails the guard on line 111, so byteSize is never inspected.
expect(
authorizeStorageUploadConstraints({
operation: "upload",
contentType: "image/png",
byteSize: 9999,
maxBytes: -1,
}),
).toBeNull();
});
});
describe("authorizeStorageUploadConstraints content-type normalization", () => {
it("treats a parameter-only content type that trims to empty as unsupported", () => {
// "; charset=utf-8" -> first segment is "" -> normalizes to null, which an
// active allow-list rejects as unsupported_content_type.
expect(
authorizeStorageUploadConstraints({
operation: "upload",
contentType: "; charset=utf-8",
allowedContentTypes: ["image/png"],
}),
).toEqual({ allowed: false, operation: "upload", reason: "unsupported_content_type" });
});
it("drops blank entries from the allow-list before matching", () => {
// The blank allow-list entry normalizes to null and is filtered out, so the
// set still only accepts image/png.
expect(
authorizeStorageUploadConstraints({
operation: "upload",
contentType: "image/png",
allowedContentTypes: [" ", "image/png"],
}),
).toBeNull();
});
it("allows any content type when the allow-list is empty", () => {
expect(
authorizeStorageUploadConstraints({
operation: "upload",
contentType: "application/x-msdownload",
allowedContentTypes: [],
}),
).toBeNull();
});
});
@@ -260,7 +260,7 @@ describe("createS3StorageAdapter", () => {
describe("createLocalStorageAdapter", () => {
it("stores objects under the configured bucket root and returns a public URL", async () => {
const rootDir = await mkdtemp(join(tmpdir(), "toolctrl-storage-"));
const rootDir = await mkdtemp(join(tmpdir(), "hivegrid-storage-"));
const adapter = createLocalStorageAdapter({
rootDir,
bucket: "uploads",
@@ -276,7 +276,7 @@ describe("createLocalStorageAdapter", () => {
});
it("rejects local object keys that escape the storage root", async () => {
const rootDir = await mkdtemp(join(tmpdir(), "toolctrl-storage-"));
const rootDir = await mkdtemp(join(tmpdir(), "hivegrid-storage-"));
const adapter = createLocalStorageAdapter({ rootDir, bucket: "uploads" });
await expect(adapter.putObject({ key: "../secret.txt", body: "secret" })).rejects.toMatchObject({
@@ -0,0 +1,129 @@
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { existsSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { StorageAdapterError, createLocalStorageAdapter } from "./index.js";
describe("createLocalStorageAdapter additional coverage", () => {
let rootDir: string;
const bucket = "uploads";
beforeEach(async () => {
rootDir = await mkdtemp(join(tmpdir(), "hg-local-store-"));
});
afterEach(async () => {
await rm(rootDir, { recursive: true, force: true });
});
function adapter(publicBaseUrl?: string) {
return createLocalStorageAdapter({ rootDir, bucket, publicBaseUrl });
}
it("throws config_missing when rootDir or bucket are absent", () => {
expect(() => createLocalStorageAdapter({ rootDir: "", bucket: "" })).toThrow(StorageAdapterError);
try {
createLocalStorageAdapter({ rootDir: "", bucket: "" });
} catch (error) {
expect(error).toMatchObject({
code: "config_missing",
details: { operation: "createLocalStorageAdapter", missingFields: ["rootDir", "bucket"] },
});
}
});
it("stores an ArrayBuffer body", async () => {
const bytes = new Uint8Array([1, 2, 3, 4]);
await adapter().putObject({ key: "files/buf.bin", body: bytes.buffer });
await expect(readFile(join(rootDir, bucket, "files/buf.bin"))).resolves.toEqual(Buffer.from(bytes));
});
it("stores a Uint8Array body", async () => {
const bytes = new Uint8Array([9, 8, 7]);
await adapter().putObject({ key: "files/u8.bin", body: bytes });
await expect(readFile(join(rootDir, bucket, "files/u8.bin"))).resolves.toEqual(Buffer.from(bytes));
});
it("stores a ReadableStream body by draining it", async () => {
const stream = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(new Uint8Array([104, 105])); // "hi"
controller.enqueue(new Uint8Array([33])); // "!"
controller.close();
},
});
await adapter().putObject({ key: "files/stream.txt", body: stream });
await expect(readFile(join(rootDir, bucket, "files/stream.txt"), "utf8")).resolves.toBe("hi!");
});
it("wraps put failures when the parent path is not a directory", async () => {
// Create a file where putObject expects a directory, so mkdir fails (ENOTDIR).
await mkdir(join(rootDir, bucket), { recursive: true });
await writeFile(join(rootDir, bucket, "blocker"), "i am a file");
await expect(
adapter().putObject({ key: "blocker/child.txt", body: "nope" }),
).rejects.toMatchObject({
code: "put_failed",
details: { operation: "putObject", bucket: "uploads", key: "blocker/child.txt" },
});
});
it("deletes an existing object and is a no-op for missing keys", async () => {
const a = adapter();
await a.putObject({ key: "del/me.txt", body: "bye" });
await expect(a.deleteObject({ key: "del/me.txt" })).resolves.toEqual({ bucket, key: "del/me.txt" });
expect(existsSync(join(rootDir, bucket, "del/me.txt"))).toBe(false);
// force:true makes a missing key a successful no-op.
await expect(a.deleteObject({ key: "del/me.txt" })).resolves.toEqual({ bucket, key: "del/me.txt" });
});
it("rejects deletes that target a different bucket than configured", async () => {
await expect(
adapter().deleteObject({ bucket: "other-bucket", key: "x.txt" }),
).rejects.toMatchObject({
code: "invalid_bucket",
details: { operation: "deleteObject", bucket: "other-bucket", key: "x.txt" },
});
});
it("wraps delete failures when the target is a non-empty directory", async () => {
// rm without recursive on a directory throws -> delete_failed.
await mkdir(join(rootDir, bucket, "dir-key"), { recursive: true });
await writeFile(join(rootDir, bucket, "dir-key", "inner.txt"), "x");
await expect(
adapter().deleteObject({ key: "dir-key" }),
).rejects.toMatchObject({
code: "delete_failed",
details: { operation: "deleteObject", bucket: "uploads", key: "dir-key" },
});
});
it("wraps uploadPartCopy failures when the source object is missing", async () => {
const a = adapter();
const handle = await a.createMultipartUpload({ key: "final/obj" });
await expect(
a.uploadPartCopy({ key: "final/obj", uploadId: handle.uploadId, partNumber: 1, sourceKey: "missing/source" }),
).rejects.toMatchObject({
code: "put_failed",
details: { operation: "uploadPartCopy", bucket: "uploads", key: "final/obj" },
});
});
it("wraps completeMultipartUpload failures when a staged part is missing", async () => {
const a = adapter();
const handle = await a.createMultipartUpload({ key: "final/obj" });
await expect(
a.completeMultipartUpload({
key: "final/obj",
uploadId: handle.uploadId,
parts: [{ partNumber: 1, etag: "local-1" }],
}),
).rejects.toMatchObject({
code: "put_failed",
details: { operation: "completeMultipartUpload", bucket: "uploads", key: "final/obj" },
});
});
});
@@ -0,0 +1,130 @@
import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from "vitest";
// Mock the AWS SDK so the *real* S3Client wrapper (and the default-client /
// default-quarantine-factory construction paths) can be exercised without any
// network round-trip. Each mocked command class records its constructor input.
vi.mock("@aws-sdk/client-s3", () => {
const send = vi.fn(async () => ({
UploadId: "aws-upload-1",
CopyPartResult: { ETag: "aws-etag" },
ok: true,
}));
class AwsS3Client {
config: unknown;
send = send;
constructor(config: unknown) {
this.config = config;
}
}
class Cmd {
input: unknown;
constructor(input: unknown) {
this.input = input;
}
}
return {
__send: send,
S3Client: AwsS3Client,
PutObjectCommand: class extends Cmd {},
DeleteObjectCommand: class extends Cmd {},
CreateMultipartUploadCommand: class extends Cmd {},
UploadPartCopyCommand: class extends Cmd {},
CompleteMultipartUploadCommand: class extends Cmd {},
AbortMultipartUploadCommand: class extends Cmd {},
CopyObjectCommand: class extends Cmd {},
};
});
import * as awsS3 from "@aws-sdk/client-s3";
import {
AbortMultipartUploadCommand,
CompleteMultipartUploadCommand,
CreateMultipartUploadCommand,
DeleteObjectCommand,
PutObjectCommand,
S3Client,
UploadPartCopyCommand,
createS3StorageAdapter,
type S3StorageAdapterConfig,
} from "./index.js";
import { createS3StorageQuarantineAdapter } from "./scan/index.js";
const awsSend = (awsS3 as unknown as { __send: Mock }).__send;
const clientConfig = {
endpoint: "https://s3.example.test",
region: "us-east-1",
credentials: { accessKeyId: "ak", secretAccessKey: "sk" },
};
const adapterConfig: S3StorageAdapterConfig = {
endpoint: "https://s3.example.test",
region: "us-east-1",
bucket: "uploads",
accessKeyId: "ak",
secretAccessKey: "sk",
};
describe("S3Client.send command dispatch", () => {
beforeEach(() => {
awsSend.mockClear();
});
afterEach(() => {
vi.restoreAllMocks();
});
it("maps a PutObjectCommand (normalizing ArrayBuffer bodies) to the AWS command", async () => {
const client = new S3Client(clientConfig);
await client.send(new PutObjectCommand({ Bucket: "u", Key: "k", Body: new ArrayBuffer(4) }));
expect(awsSend).toHaveBeenCalledOnce();
const sent = awsSend.mock.calls[0]?.[0] as { input: { Body: unknown } };
expect(sent).toBeInstanceOf(awsS3.PutObjectCommand);
// ArrayBuffer is normalized to a Uint8Array before reaching the AWS SDK.
expect(sent.input.Body).toBeInstanceOf(Uint8Array);
});
it("maps a DeleteObjectCommand to the AWS delete command (fallback branch)", async () => {
const client = new S3Client(clientConfig);
await client.send(new DeleteObjectCommand({ Bucket: "u", Key: "k" }));
expect(awsSend.mock.calls[0]?.[0]).toBeInstanceOf(awsS3.DeleteObjectCommand);
});
it("maps each multipart command to its AWS equivalent", async () => {
const client = new S3Client(clientConfig);
await client.send(new CreateMultipartUploadCommand({ Bucket: "u", Key: "k" }));
await client.send(
new UploadPartCopyCommand({ Bucket: "u", Key: "k", UploadId: "u1", PartNumber: 1, CopySource: "u/src" }),
);
await client.send(
new CompleteMultipartUploadCommand({ Bucket: "u", Key: "k", UploadId: "u1", MultipartUpload: { Parts: [] } }),
);
await client.send(new AbortMultipartUploadCommand({ Bucket: "u", Key: "k", UploadId: "u1" }));
expect(awsSend.mock.calls[0]?.[0]).toBeInstanceOf(awsS3.CreateMultipartUploadCommand);
expect(awsSend.mock.calls[1]?.[0]).toBeInstanceOf(awsS3.UploadPartCopyCommand);
expect(awsSend.mock.calls[2]?.[0]).toBeInstanceOf(awsS3.CompleteMultipartUploadCommand);
expect(awsSend.mock.calls[3]?.[0]).toBeInstanceOf(awsS3.AbortMultipartUploadCommand);
});
it("createS3StorageAdapter builds a default S3Client when none is injected", async () => {
const adapter = createS3StorageAdapter(adapterConfig);
await expect(adapter.putObject({ key: "k.txt", body: "x" })).resolves.toMatchObject({
bucket: "uploads",
key: "k.txt",
});
expect(awsSend).toHaveBeenCalledOnce();
expect(awsSend.mock.calls[0]?.[0]).toBeInstanceOf(awsS3.PutObjectCommand);
});
it("createS3StorageQuarantineAdapter builds a default AWS client when no factory is given", async () => {
const adapter = createS3StorageQuarantineAdapter({ region: "us-east-1", accessKeyId: "ak", secretAccessKey: "sk" });
const result = await adapter.quarantineObject({ bucket: "uploads", key: "bad.exe", signature: "Win.Test" });
expect(result).toMatchObject({ bucket: "uploads", key: "quarantine/bad.exe", action: "moved" });
// Copy into quarantine + delete original.
expect(awsSend).toHaveBeenCalledTimes(2);
expect(awsSend.mock.calls[0]?.[0]).toBeInstanceOf(awsS3.CopyObjectCommand);
expect(awsSend.mock.calls[1]?.[0]).toBeInstanceOf(awsS3.DeleteObjectCommand);
});
});
@@ -0,0 +1,210 @@
import { describe, expect, it, vi } from "vitest";
import {
AbortMultipartUploadCommand,
CompleteMultipartUploadCommand,
CreateMultipartUploadCommand,
UploadPartCopyCommand,
createS3StorageAdapter,
type S3StorageAdapterConfig,
} from "./index.js";
// AK-P4a §3: server-side chunk assembly via S3 multipart UploadPartCopy. The
// create -> uploadPartCopy -> complete -> abort sequence is exercised here with
// a fake S3 client so no network round-trip is required.
const baseConfig: S3StorageAdapterConfig = {
endpoint: "https://s3.example.test",
region: "us-east-1",
bucket: "uploads",
accessKeyId: "access-key",
secretAccessKey: "secret-key",
publicBaseUrl: "https://cdn.example.test/assets",
};
describe("createS3StorageAdapter multipart", () => {
it("createMultipartUpload sends a CreateMultipartUploadCommand and returns the UploadId", async () => {
const send = vi.fn().mockResolvedValue({ UploadId: "upload-1" });
const adapter = createS3StorageAdapter({ ...baseConfig, client: { send } });
await expect(
adapter.createMultipartUpload({
key: "big/object.bin",
contentType: "application/octet-stream",
metadata: { tenant: "acme" },
}),
).resolves.toEqual({ bucket: "uploads", key: "big/object.bin", uploadId: "upload-1" });
const command = send.mock.calls[0]?.[0];
expect(command).toBeInstanceOf(CreateMultipartUploadCommand);
expect(command.input).toEqual({
Bucket: "uploads",
Key: "big/object.bin",
ContentType: "application/octet-stream",
Metadata: { tenant: "acme" },
});
});
it("createMultipartUpload fails when S3 returns no UploadId", async () => {
const adapter = createS3StorageAdapter({
...baseConfig,
client: { send: vi.fn().mockResolvedValue({}) },
});
await expect(
adapter.createMultipartUpload({ key: "big/object.bin" }),
).rejects.toMatchObject({
code: "put_failed",
details: { operation: "createMultipartUpload", bucket: "uploads", key: "big/object.bin" },
});
});
it("createMultipartUpload wraps upstream errors with redacted diagnostics", async () => {
const upstreamError = Object.assign(new Error("denied for credential secret-key"), {
name: "AccessDenied",
$metadata: { httpStatusCode: 403, requestId: "req-create" },
});
const adapter = createS3StorageAdapter({
...baseConfig,
client: { send: vi.fn().mockRejectedValue(upstreamError) },
});
await expect(
adapter.createMultipartUpload({ key: "big/object.bin" }),
).rejects.toMatchObject({
code: "put_failed",
details: {
operation: "createMultipartUpload",
upstream: { name: "AccessDenied", message: "denied for credential [redacted]", status: 403, requestId: "req-create" },
},
});
});
it("uploadPartCopy URL-encodes the CopySource and returns the part ETag", async () => {
const send = vi.fn().mockResolvedValue({ CopyPartResult: { ETag: '"etag-part-1"' } });
const adapter = createS3StorageAdapter({ ...baseConfig, client: { send } });
await expect(
adapter.uploadPartCopy({
key: "big/object.bin",
uploadId: "upload-1",
partNumber: 1,
sourceKey: "tmp/chunk one",
}),
).resolves.toEqual({ partNumber: 1, etag: '"etag-part-1"' });
const command = send.mock.calls[0]?.[0];
expect(command).toBeInstanceOf(UploadPartCopyCommand);
expect(command.input).toMatchObject({
Bucket: "uploads",
Key: "big/object.bin",
UploadId: "upload-1",
PartNumber: 1,
// "uploads/tmp/chunk one" with each path segment percent-encoded.
CopySource: "uploads/tmp/chunk%20one",
});
});
it("uploadPartCopy fails when S3 returns no ETag", async () => {
const adapter = createS3StorageAdapter({
...baseConfig,
client: { send: vi.fn().mockResolvedValue({ CopyPartResult: {} }) },
});
await expect(
adapter.uploadPartCopy({ key: "k", uploadId: "u", partNumber: 1, sourceKey: "src" }),
).rejects.toMatchObject({ code: "put_failed", details: { operation: "uploadPartCopy" } });
});
it("uploadPartCopy wraps upstream errors", async () => {
const adapter = createS3StorageAdapter({
...baseConfig,
client: { send: vi.fn().mockRejectedValue(new Error("copy boom")) },
});
await expect(
adapter.uploadPartCopy({ key: "k", uploadId: "u", partNumber: 2, sourceKey: "src" }),
).rejects.toMatchObject({ code: "put_failed", details: { operation: "uploadPartCopy" } });
});
it("completeMultipartUpload sorts parts by number and returns the public URL", async () => {
const send = vi.fn().mockResolvedValue({});
const adapter = createS3StorageAdapter({ ...baseConfig, client: { send } });
await expect(
adapter.completeMultipartUpload({
key: "big/object.bin",
uploadId: "upload-1",
// Intentionally out of order to prove the adapter sorts them.
parts: [
{ partNumber: 2, etag: "etag-2" },
{ partNumber: 1, etag: "etag-1" },
],
}),
).resolves.toEqual({
bucket: "uploads",
key: "big/object.bin",
publicUrl: "https://cdn.example.test/assets/big/object.bin",
});
const command = send.mock.calls[0]?.[0];
expect(command).toBeInstanceOf(CompleteMultipartUploadCommand);
expect(command.input.MultipartUpload.Parts).toEqual([
{ ETag: "etag-1", PartNumber: 1 },
{ ETag: "etag-2", PartNumber: 2 },
]);
});
it("completeMultipartUpload wraps upstream errors", async () => {
const adapter = createS3StorageAdapter({
...baseConfig,
client: { send: vi.fn().mockRejectedValue(new Error("complete boom")) },
});
await expect(
adapter.completeMultipartUpload({ key: "k", uploadId: "u", parts: [] }),
).rejects.toMatchObject({ code: "put_failed", details: { operation: "completeMultipartUpload" } });
});
it("abortMultipartUpload sends an AbortMultipartUploadCommand", async () => {
const send = vi.fn().mockResolvedValue({});
const adapter = createS3StorageAdapter({ ...baseConfig, client: { send } });
await expect(
adapter.abortMultipartUpload({ key: "big/object.bin", uploadId: "upload-1" }),
).resolves.toBeUndefined();
const command = send.mock.calls[0]?.[0];
expect(command).toBeInstanceOf(AbortMultipartUploadCommand);
expect(command.input).toEqual({
Bucket: "uploads",
Key: "big/object.bin",
UploadId: "upload-1",
});
});
it("abortMultipartUpload wraps upstream errors", async () => {
const adapter = createS3StorageAdapter({
...baseConfig,
client: { send: vi.fn().mockRejectedValue(new Error("abort boom")) },
});
await expect(
adapter.abortMultipartUpload({ key: "k", uploadId: "u" }),
).rejects.toMatchObject({ code: "put_failed", details: { operation: "abortMultipartUpload" } });
});
it("omits upstream diagnostics when the thrown cause is not an object", async () => {
const adapter = createS3StorageAdapter({
...baseConfig,
client: { send: vi.fn().mockRejectedValue("plain string failure") },
});
try {
await adapter.putObject({ key: "x.txt", body: "x" });
throw new Error("expected putObject to fail");
} catch (error) {
expect(error).toMatchObject({ code: "put_failed" });
expect((error as { details: { upstream?: unknown } }).details.upstream).toBeUndefined();
}
});
});
@@ -0,0 +1,52 @@
// @vitest-environment node
import { Buffer } from "node:buffer";
import { describe, expect, test, vi } from "vitest";
// Mirror of clamav-default-client.test.ts but for the CommonJS-interop shape
// where `clamscan` exposes `init` directly on the module object with NO default
// export. This exercises the `?? mod` fallback and the non-constructor branch
// of defaultInitClient (`typeof Factory === "function" ? new ... : Factory`).
const ping = vi.fn(async () => "PONG");
const scanBuffer = vi.fn(async () => ({ isInfected: false as boolean, viruses: [] as string[] }));
const scanStream = vi.fn(async () => ({ isInfected: false as boolean, viruses: [] as string[] }));
const init = vi.fn(async () => ({ ping, scanBuffer, scanStream }));
// `default: undefined` keeps the property declared (so the source's `.default`
// access is legal under vitest's strict ESM namespace) while still being
// nullish, which forces the `?? mod` fallback and the non-constructor branch.
vi.mock("clamscan", () => ({ default: undefined, init }));
import { createClamAVStorageScanner } from "./clamav-scanner.js";
const baseConfig = { host: "clamav", port: 3310, timeoutMs: 30000 };
describe("ClamAVStorageScanner with a default-less clamscan module", () => {
test("uses the module object itself as the factory when there is no default export", async () => {
const scanner = createClamAVStorageScanner({
...baseConfig,
loadObject: async () => Buffer.from("hello"),
});
const result = await scanner.scanObject({ bucket: "uploads", key: "a.txt" });
expect(result.verdict).toBe("clean");
// init() is called directly on the module export, never via `new`.
expect(init).toHaveBeenCalledTimes(1);
expect(ping).toHaveBeenCalled();
expect(scanBuffer).toHaveBeenCalledWith(Buffer.from("hello"));
});
test("reuses the cached client across scans (init runs once per scanner)", async () => {
const before = init.mock.calls.length;
const scanner = createClamAVStorageScanner({
...baseConfig,
loadObject: async () => Buffer.from("again"),
});
await scanner.scanObject({ bucket: "uploads", key: "b.txt" });
await scanner.scanObject({ bucket: "uploads", key: "c.txt" });
// The per-scanner client cache means two scans trigger exactly one init.
expect(init.mock.calls.length - before).toBe(1);
});
});
@@ -0,0 +1,63 @@
// @vitest-environment node
import { Buffer } from "node:buffer";
import { describe, expect, test, vi } from "vitest";
// Exercises the production defaultInitClient path (no initClient override) by
// mocking the lazily-imported `clamscan` package. The factory exposes init(),
// which returns the ping/scanBuffer/scanStream surface the wrapper adapts.
const ping = vi.fn(async () => "PONG");
const scanBuffer = vi.fn(async () => ({ isInfected: false as boolean, viruses: [] as string[] }));
const scanStream = vi.fn(async () => ({ isInfected: false as boolean, viruses: [] as string[] }));
const init = vi.fn(async () => ({ ping, scanBuffer, scanStream }));
vi.mock("clamscan", () => {
class NodeClam {
init = init;
}
return { default: NodeClam };
});
import { createClamAVStorageScanner } from "./clamav-scanner.js";
const baseConfig = { host: "clamav", port: 3310, timeoutMs: 30000 };
describe("ClamAVStorageScanner with the real (mocked) clamscan client", () => {
test("initializes the clamscan factory and reports clean for a buffer payload", async () => {
const scanner = createClamAVStorageScanner({
...baseConfig,
loadObject: async () => Buffer.from("hello world"),
});
const result = await scanner.scanObject({ bucket: "uploads", key: "a.txt" });
expect(result.verdict).toBe("clean");
expect(init).toHaveBeenCalledTimes(1);
expect(ping).toHaveBeenCalled();
expect(scanBuffer).toHaveBeenCalledWith(Buffer.from("hello world"));
});
test("adapts an infected scanBuffer verdict into the scanner result", async () => {
scanBuffer.mockResolvedValueOnce({ isInfected: true, viruses: ["Win.Test.EICAR_HDB-1"] });
const scanner = createClamAVStorageScanner({
...baseConfig,
loadObject: async () => Buffer.from("infected"),
});
const result = await scanner.scanObject({ bucket: "uploads", key: "eicar.txt" });
expect(result.verdict).toBe("infected");
if (result.verdict === "infected") expect(result.signature).toBe("Win.Test.EICAR_HDB-1");
});
test("routes a stream payload through scanStream", async () => {
const { Readable } = await import("node:stream");
const stream = Readable.from([Buffer.from("payload")]);
const scanner = createClamAVStorageScanner({
...baseConfig,
loadObject: async () => stream,
});
const result = await scanner.scanObject({ bucket: "uploads", key: "s.bin" });
expect(result.verdict).toBe("clean");
expect(scanStream).toHaveBeenCalledWith(stream);
});
});
@@ -0,0 +1,165 @@
import { describe, expect, it, vi } from "vitest";
import {
createStorageScanService,
type StorageQuarantineAdapter,
type StorageScanner,
} from "./index.js";
const fixedNow = new Date("2026-02-03T04:05:06.000Z");
function createService(overrides: {
scanner?: StorageScanner;
quarantine?: StorageQuarantineAdapter;
} = {}) {
return createStorageScanService({
scanner: overrides.scanner ?? {
scanObject: vi.fn().mockResolvedValue({ verdict: "clean" }),
},
quarantine: overrides.quarantine,
now: () => fixedNow,
});
}
describe("createStorageScanService infected without a quarantine adapter", () => {
it("falls back to the scanner-supplied quarantineKey when no adapter ran", async () => {
const service = createService({
scanner: {
scanObject: vi.fn().mockResolvedValue({
verdict: "infected",
engine: "clamav-1.4",
signature: "Eicar-Test-Signature",
quarantineKey: "quarantine/uploads/avatar.png",
}),
},
// No quarantine adapter configured at all.
});
const result = await service.scanObject({ bucket: "uploads", key: "avatar.png" });
expect(result).toEqual({
verdict: "infected",
bucket: "uploads",
key: "avatar.png",
scannedAt: fixedNow,
engine: "clamav-1.4",
signature: "Eicar-Test-Signature",
quarantineKey: "quarantine/uploads/avatar.png",
quarantine: undefined,
});
});
it("leaves quarantineKey undefined when neither scanner nor adapter provide one", async () => {
const service = createService({
scanner: {
scanObject: vi.fn().mockResolvedValue({
verdict: "infected",
signature: "Eicar-Test-Signature",
}),
},
});
const result = await service.scanObject({ bucket: "uploads", key: "avatar.png" });
expect(result).toMatchObject({
verdict: "infected",
signature: "Eicar-Test-Signature",
quarantineKey: undefined,
quarantine: undefined,
});
});
});
describe("createStorageScanService quarantine result without a quarantineKey", () => {
it("falls back to the scanner quarantineKey when the adapter omits one", async () => {
const service = createService({
scanner: {
scanObject: vi.fn().mockResolvedValue({
verdict: "infected",
signature: "Eicar-Test-Signature",
quarantineKey: "quarantine/from-scanner.png",
}),
},
quarantine: {
// Adapter only tags the object in place; it returns no quarantineKey.
quarantineObject: vi.fn().mockResolvedValue({ action: "tagged" }),
},
});
const result = await service.scanObject({ bucket: "uploads", key: "avatar.png" });
expect(result).toMatchObject({
verdict: "infected",
quarantineKey: "quarantine/from-scanner.png",
quarantine: { action: "tagged" },
});
});
it("prefers the adapter quarantineKey over the scanner one when both exist", async () => {
const service = createService({
scanner: {
scanObject: vi.fn().mockResolvedValue({
verdict: "infected",
signature: "Eicar-Test-Signature",
quarantineKey: "quarantine/from-scanner.png",
}),
},
quarantine: {
quarantineObject: vi.fn().mockResolvedValue({
action: "moved",
quarantineKey: "quarantine/from-adapter.png",
}),
},
});
const result = await service.scanObject({ bucket: "uploads", key: "avatar.png" });
expect(result).toMatchObject({
verdict: "infected",
quarantineKey: "quarantine/from-adapter.png",
});
});
});
describe("createStorageScanService upstream redaction edge cases", () => {
it("yields all-undefined upstream fields for a record cause without diagnostics", async () => {
const service = createService({
scanner: {
scanObject: vi.fn().mockRejectedValue({ leakedField: "do-not-copy" }),
},
});
const result = await service.scanObject({ bucket: "uploads", key: "avatar.png" });
expect(result.verdict).toBe("error");
if (result.verdict !== "error") throw new Error("expected error verdict");
expect(result.error.code).toBe("scanner_failed");
expect(result.error.upstream?.name).toBeUndefined();
expect(result.error.upstream?.message).toBeUndefined();
expect(result.error.upstream?.status).toBeUndefined();
expect(result.error.upstream?.requestId).toBeUndefined();
expect(JSON.stringify(result)).not.toContain("do-not-copy");
});
it("ignores a non-record $metadata and non-string name/message on the cause", async () => {
const service = createService({
scanner: {
scanObject: vi.fn().mockRejectedValue({
$metadata: 42,
name: true,
message: ["array-not-string"],
}),
},
});
const result = await service.scanObject({ bucket: "uploads", key: "avatar.png" });
expect(result.verdict).toBe("error");
if (result.verdict !== "error") throw new Error("expected error verdict");
expect(result.error.upstream?.status).toBeUndefined();
expect(result.error.upstream?.requestId).toBeUndefined();
expect(result.error.upstream?.name).toBeUndefined();
expect(result.error.upstream?.message).toBeUndefined();
expect(JSON.stringify(result)).not.toContain("array-not-string");
});
});
@@ -0,0 +1,57 @@
// @vitest-environment node
import { Buffer } from "node:buffer";
import { describe, expect, test, vi } from "vitest";
// Force file-type's signature reader to throw so detectMimeType()'s catch
// returns undefined. The adapter must then fall through to the undetectable
// path rather than propagating the error to the caller.
vi.mock("file-type", () => ({
fileTypeFromBuffer: vi.fn(async () => {
throw new Error("corrupt signature table");
}),
}));
import { createMagicBytesScanAdapter } from "./magic-bytes-adapter.js";
const REAL_PNG = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
"base64",
);
const asUint8 = (buf: Buffer): Uint8Array => new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
describe("magic-bytes-adapter when file-type throws", () => {
test("treats a thrown detector error as an undetectable MIME (does not propagate)", async () => {
const adapter = createMagicBytesScanAdapter();
const result = await adapter.scan({
buffer: asUint8(REAL_PNG),
declaredMimeType: "image/png",
filename: "avatar.png",
context: "avatar",
});
expect(result.status).toBe("rejected");
expect(result.reason).toBe("mime_undetectable");
expect(result.detail).toContain("image/png");
});
test("still honors the text/plain fallback when the detector throws", async () => {
const adapter = createMagicBytesScanAdapter({
allowList: {
avatar: [],
mediaLibrary: [],
emailAttachment: ["text/plain"],
formUpload: [],
kbAsset: [],
},
});
const buffer = new TextEncoder().encode("plain text body");
const result = await adapter.scan({
buffer,
declaredMimeType: "text/plain",
filename: "notes.txt",
context: "emailAttachment",
});
expect(result.status).toBe("clean");
});
});
@@ -0,0 +1,69 @@
// @vitest-environment node
import { Buffer } from "node:buffer";
import { describe, expect, test } from "vitest";
import { createMagicBytesScanAdapter } from "./magic-bytes-adapter.js";
import type { PreUploadAllowList } from "./pre-upload-types.js";
const REAL_PNG = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
"base64",
);
const asUint8 = (buf: Buffer): Uint8Array => new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
describe("magic-bytes-adapter filename guard edge cases", () => {
test("rejects an empty filename before inspecting the buffer", async () => {
const adapter = createMagicBytesScanAdapter();
const result = await adapter.scan({
buffer: asUint8(REAL_PNG),
declaredMimeType: "image/png",
filename: "",
context: "avatar",
});
expect(result.status).toBe("rejected");
expect(result.reason).toBe("filename_invalid");
expect(result.detail).toBe("filename was empty");
});
test("rejects a non-string filename without throwing", async () => {
const adapter = createMagicBytesScanAdapter();
const result = await adapter.scan({
buffer: asUint8(REAL_PNG),
declaredMimeType: "image/png",
// Defensive: a caller passing a non-string from untyped JSON must be
// rejected, not crash the scanner.
filename: undefined as unknown as string,
context: "avatar",
});
expect(result.status).toBe("rejected");
expect(result.reason).toBe("filename_invalid");
});
test("falls back to an empty allow-list when the context key is missing", async () => {
// Defensive: a partial allow-list (e.g. built from incomplete env config)
// that omits the requested context must reject rather than throw — the
// `allowList[context] ?? []` guard yields an empty, deny-all list.
const partialAllowList = { mediaLibrary: ["image/png"] } as unknown as PreUploadAllowList;
const adapter = createMagicBytesScanAdapter({ allowList: partialAllowList });
const result = await adapter.scan({
buffer: asUint8(REAL_PNG),
declaredMimeType: "image/png",
filename: "avatar.png",
context: "avatar",
});
expect(result.status).toBe("rejected");
expect(result.reason).toBe("mime_not_allowed");
});
test("trims and lowercases the declared MIME before matching", async () => {
const adapter = createMagicBytesScanAdapter();
const result = await adapter.scan({
buffer: asUint8(REAL_PNG),
declaredMimeType: " IMAGE/PNG ",
filename: "avatar.png",
context: "avatar",
});
expect(result.status).toBe("clean");
expect(result.detectedMimeType).toBe("image/png");
});
});
@@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import { StorageScanError, type StorageScanDiagnostic } from "./index.js";
describe("StorageScanError", () => {
const details: StorageScanDiagnostic = { code: "scanner_failed", message: "scan boom" };
it("carries the code, details, and underlying cause", () => {
const cause = new Error("clamd down");
const error = new StorageScanError("scanner_failed", "Storage scanner failed.", details, { cause });
expect(error).toBeInstanceOf(Error);
expect(error.name).toBe("StorageScanError");
expect(error.message).toBe("Storage scanner failed.");
expect(error.code).toBe("scanner_failed");
expect(error.details).toEqual(details);
expect(error.cause).toBe(cause);
});
it("defaults cause to undefined when no options are passed", () => {
const error = new StorageScanError("invalid_target", "Bad target.", {
code: "invalid_target",
message: "Bad target.",
});
expect(error.cause).toBeUndefined();
});
});
@@ -0,0 +1,38 @@
import { describe, expect, it, vi } from "vitest";
import { StorageAdapterError, createS3SignedUrlAdapter } from "../index.js";
describe("createS3SignedUrlAdapter edge cases", () => {
it("throws config_missing when the bucket is blank", async () => {
const adapter = createS3SignedUrlAdapter({
bucket: " ",
uploadSigner: vi.fn(),
downloadSigner: vi.fn(),
});
await expect(
adapter.createSignedUploadUrl({ key: "a.png", expiresInSeconds: 60 }),
).rejects.toMatchObject({
code: "config_missing",
details: { operation: "createSignedUploadUrl", missingFields: ["bucket"] },
});
});
it("omits upstream details when the signer rejects with a non-object value", async () => {
const adapter = createS3SignedUrlAdapter({
bucket: "uploads",
uploadSigner: vi.fn().mockRejectedValue("plain failure string"),
downloadSigner: vi.fn(),
});
try {
await adapter.createSignedUploadUrl({ key: "a.png", expiresInSeconds: 60 });
throw new Error("expected signer to fail");
} catch (error) {
expect(error).toBeInstanceOf(StorageAdapterError);
const adapterError = error as StorageAdapterError;
expect(adapterError.code).toBe("signed_upload_failed");
expect(adapterError.details.upstream).toBeUndefined();
}
});
});
@@ -0,0 +1,108 @@
import { describe, expect, it, vi } from "vitest";
import { StorageAdapterError, createS3SignedUrlAdapter } from "../index.js";
function createAdapter(overrides: Partial<Parameters<typeof createS3SignedUrlAdapter>[0]> = {}) {
return createS3SignedUrlAdapter({
bucket: "uploads",
now: () => new Date("2026-01-01T00:00:00.000Z"),
uploadSigner: vi.fn().mockResolvedValue("https://signed.example.test/upload"),
downloadSigner: vi.fn().mockResolvedValue("https://signed.example.test/download"),
...overrides,
});
}
describe("createS3SignedUrlAdapter upload header assembly", () => {
it("omits content-type, content-length, and checksum headers when none were supplied", async () => {
const adapter = createAdapter();
const result = await adapter.createSignedUploadUrl({
key: "avatars/user.png",
expiresInSeconds: 60,
headers: { "x-amz-acl": "private" },
});
// Only the caller-provided header should survive; the conditional spreads
// must contribute nothing when the inputs are absent.
expect(result.headers).toEqual({ "x-amz-acl": "private" });
expect(result.headers).not.toHaveProperty("content-type");
expect(result.headers).not.toHaveProperty("content-length");
expect(result.headers).not.toHaveProperty("x-amz-checksum-sha256");
expect(result.metadata).toEqual({});
});
it("emits a content-length header for a zero-byte upload (typeof number, not truthiness)", async () => {
const adapter = createAdapter();
const result = await adapter.createSignedUploadUrl({
key: "avatars/empty.bin",
expiresInSeconds: 60,
contentLength: 0,
});
// contentLength is gated on `typeof === "number"`, so 0 must still appear.
expect(result.headers["content-length"]).toBe("0");
expect(result.headers).not.toHaveProperty("content-type");
});
it("drops blank metadata and header keys during normalization", async () => {
const adapter = createAdapter();
const result = await adapter.createSignedUploadUrl({
key: "avatars/user.png",
expiresInSeconds: 60,
metadata: { tenant: "acme", " ": "blank-key" },
headers: { "x-keep": "yes", "": "blank-header" },
});
expect(result.metadata).toEqual({ tenant: "acme" });
expect(result.headers).toEqual({ "x-keep": "yes" });
});
});
describe("createS3SignedUrlAdapter upstream redaction edge cases", () => {
it("returns all-undefined upstream fields when the cause is a record without diagnostics", async () => {
const adapter = createAdapter({
uploadSigner: vi.fn().mockRejectedValue({ internalSecret: "do-not-leak" }),
});
try {
await adapter.createSignedUploadUrl({ key: "a.png", expiresInSeconds: 60 });
throw new Error("expected signer to fail");
} catch (error) {
expect(error).toBeInstanceOf(StorageAdapterError);
const adapterError = error as StorageAdapterError;
expect(adapterError.code).toBe("signed_upload_failed");
expect(adapterError.details.upstream?.name).toBeUndefined();
expect(adapterError.details.upstream?.message).toBeUndefined();
expect(adapterError.details.upstream?.status).toBeUndefined();
expect(adapterError.details.upstream?.requestId).toBeUndefined();
// Arbitrary thrown fields are never copied into the diagnostic.
expect(JSON.stringify(adapterError.details)).not.toContain("do-not-leak");
}
});
it("ignores a non-record $metadata and non-string name/message on the cause", async () => {
const adapter = createAdapter({
downloadSigner: vi.fn().mockRejectedValue({
$metadata: "not-a-record",
name: 123,
message: { nested: true },
}),
});
try {
await adapter.createSignedDownloadUrl({ key: "a.png", expiresInSeconds: 60 });
throw new Error("expected signer to fail");
} catch (error) {
expect(error).toBeInstanceOf(StorageAdapterError);
const adapterError = error as StorageAdapterError;
expect(adapterError.code).toBe("signed_download_failed");
const upstream = adapterError.details.upstream;
expect(upstream?.status).toBeUndefined();
expect(upstream?.requestId).toBeUndefined();
expect(upstream?.name).toBeUndefined();
expect(upstream?.message).toBeUndefined();
}
});
});
@@ -0,0 +1,76 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { StorageAdapterError } from "../index.js";
import type { SignedDownloadSigner, SignedDownloadSignerInput } from "./index.js";
import { createSigV4DownloadSigner, type SigV4SignerConfig } from "./sigv4.js";
function sign(
signer: SignedDownloadSigner,
input: Omit<SignedDownloadSignerInput, "bucket" | "headers"> & Partial<Pick<SignedDownloadSignerInput, "headers">>,
): string {
const result = signer({ bucket: "uploads", headers: {}, ...input });
if (typeof result !== "string") {
throw new Error("SigV4 signer is expected to resolve synchronously");
}
return result;
}
const baseConfig: SigV4SignerConfig = {
endpoint: "https://s3.example.test",
region: "eu-central-1",
bucket: "uploads",
accessKeyId: "AKIAEXAMPLE",
secretAccessKey: "secret-key",
};
describe("createSigV4DownloadSigner config_missing diagnostics", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-01-02T03:04:05.000Z"));
});
afterEach(() => {
vi.useRealTimers();
});
it("echoes the present bucket while reporting other missing credential fields", () => {
// bucket is present here, so the error detail keeps it (the true branch of
// `isPresent(config.bucket) ? config.bucket : undefined`) while the missing
// access key / secret are surfaced via missingFields.
const signer = createSigV4DownloadSigner({
...baseConfig,
accessKeyId: "",
secretAccessKey: " ",
});
try {
sign(signer, { key: "private/report.pdf", expiresInSeconds: 60 });
throw new Error("expected signer to throw");
} catch (error) {
expect(error).toBeInstanceOf(StorageAdapterError);
const adapterError = error as StorageAdapterError;
expect(adapterError.code).toBe("config_missing");
expect(adapterError.details).toMatchObject({
operation: "createSigV4DownloadSigner",
bucket: "uploads",
key: "private/report.pdf",
missingFields: ["accessKeyId", "secretAccessKey"],
});
}
});
it("reports a missing region without leaking the secret access key", () => {
const signer = createSigV4DownloadSigner({ ...baseConfig, region: "" });
try {
sign(signer, { key: "x.png", expiresInSeconds: 60 });
throw new Error("expected signer to throw");
} catch (error) {
const adapterError = error as StorageAdapterError;
expect(adapterError.code).toBe("config_missing");
expect(adapterError.details.missingFields).toEqual(["region"]);
expect(adapterError.details.bucket).toBe("uploads");
expect(JSON.stringify(adapterError)).not.toContain("secret-key");
}
});
});
@@ -0,0 +1,123 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { StorageAdapterError } from "../index.js";
import type { SignedDownloadSigner, SignedDownloadSignerInput } from "./index.js";
import { createSigV4DownloadSigner, type SigV4SignerConfig } from "./sigv4.js";
// The SigV4 signer resolves synchronously; this helper narrows the declared
// `string | Promise<string>` signer return type for ergonomic assertions.
function sign(
signer: SignedDownloadSigner,
input: Omit<SignedDownloadSignerInput, "bucket" | "headers"> & Partial<Pick<SignedDownloadSignerInput, "headers">>,
): string {
const result = signer({ bucket: "uploads", headers: {}, ...input });
if (typeof result !== "string") {
throw new Error("SigV4 signer is expected to resolve synchronously");
}
return result;
}
const baseConfig: SigV4SignerConfig = {
endpoint: "https://s3.example.test",
region: "eu-central-1",
bucket: "uploads",
accessKeyId: "AKIAEXAMPLE",
secretAccessKey: "secret-key",
};
const fixedNow = new Date("2026-01-02T03:04:05.000Z");
describe("createSigV4DownloadSigner", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(fixedNow);
});
afterEach(() => {
vi.useRealTimers();
});
it("builds a virtual-hosted presigned GET URL with all SigV4 query params", () => {
const signer = createSigV4DownloadSigner(baseConfig);
const url = new URL(sign(signer, { key: "avatars/user.png", expiresInSeconds: 900 }));
// Virtual-hosted-style: bucket is folded into the host, key is the path.
expect(url.host).toBe("uploads.s3.example.test");
expect(url.pathname).toBe("/avatars/user.png");
expect(url.searchParams.get("X-Amz-Algorithm")).toBe("AWS4-HMAC-SHA256");
expect(url.searchParams.get("X-Amz-Credential")).toBe(
"AKIAEXAMPLE/20260102/eu-central-1/s3/aws4_request",
);
expect(url.searchParams.get("X-Amz-Date")).toBe("20260102T030405Z");
expect(url.searchParams.get("X-Amz-Expires")).toBe("900");
expect(url.searchParams.get("X-Amz-SignedHeaders")).toBe("host");
// Signature is a lowercase 64-char hex HMAC-SHA256 digest.
expect(url.searchParams.get("X-Amz-Signature")).toMatch(/^[0-9a-f]{64}$/);
// The signature itself is never part of the signed canonical query.
expect(url.searchParams.has("X-Amz-Security-Token")).toBe(false);
});
it("does not leak the secret access key into the produced URL", () => {
const signer = createSigV4DownloadSigner(baseConfig);
const url = sign(signer, { key: "private/report.pdf", expiresInSeconds: 60 });
expect(url).not.toContain("secret-key");
});
it("emits a deterministic signature for the same inputs (signing-key cache hit)", () => {
const signer = createSigV4DownloadSigner(baseConfig);
const first = new URL(sign(signer, { key: "a/b.png", expiresInSeconds: 120 }));
const second = new URL(sign(signer, { key: "a/b.png", expiresInSeconds: 120 }));
expect(second.searchParams.get("X-Amz-Signature")).toBe(
first.searchParams.get("X-Amz-Signature"),
);
});
it("uses path-style host/path and appends response overrides + session token", () => {
const signer = createSigV4DownloadSigner({
...baseConfig,
endpoint: "https://s3.example.test/",
forcePathStyle: true,
sessionToken: "session-token-value",
});
const url = new URL(
sign(signer, {
key: "folder/a b.png",
expiresInSeconds: 60,
responseContentType: "image/png",
responseContentDisposition: "inline",
}),
);
// Path-style: host stays the endpoint host, bucket is the first path segment.
expect(url.host).toBe("s3.example.test");
expect(url.pathname).toBe("/uploads/folder/a%20b.png");
expect(url.searchParams.get("response-content-type")).toBe("image/png");
expect(url.searchParams.get("response-content-disposition")).toBe("inline");
expect(url.searchParams.get("X-Amz-Security-Token")).toBe("session-token-value");
});
it("throws a redacted config_missing error when required fields are absent", () => {
const signer = createSigV4DownloadSigner({
...baseConfig,
endpoint: "",
bucket: "",
secretAccessKey: "super-secret",
});
try {
sign(signer, { key: "x.png", expiresInSeconds: 60 });
throw new Error("expected signer to throw");
} catch (error) {
expect(error).toBeInstanceOf(StorageAdapterError);
const adapterError = error as StorageAdapterError;
expect(adapterError.code).toBe("config_missing");
expect(adapterError.details).toMatchObject({
operation: "createSigV4DownloadSigner",
key: "x.png",
missingFields: ["endpoint", "bucket"],
});
expect(JSON.stringify(adapterError)).not.toContain("super-secret");
}
});
});