Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+38
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
+108
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
+76
@@ -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");
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user