Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -5,6 +5,14 @@ import { DEFAULT_ALLOW_LIST, resolveAllowList } from "./allow-list.js";
|
||||
describe("resolveAllowList", () => {
|
||||
it("returns restrictive defaults when no environment overrides are supplied", () => {
|
||||
expect(resolveAllowList()).toEqual(DEFAULT_ALLOW_LIST);
|
||||
expect(DEFAULT_ALLOW_LIST.formUpload).toEqual(
|
||||
expect.arrayContaining([
|
||||
"image/jpeg",
|
||||
"application/pdf",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"audio/mpeg",
|
||||
])
|
||||
);
|
||||
});
|
||||
|
||||
it("replaces a context default with the operator supplied env allow-list", () => {
|
||||
|
||||
@@ -7,6 +7,7 @@ const OFFICE_TYPES = [
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||
] as const;
|
||||
const AUDIO_TYPES = ["audio/mpeg", "audio/wav", "audio/x-wav", "audio/ogg", "audio/mp4", "audio/aac", "audio/flac"] as const;
|
||||
|
||||
/**
|
||||
* Restrictive defaults per upload context. Operator can extend per-context via the
|
||||
@@ -17,7 +18,7 @@ export const DEFAULT_ALLOW_LIST: PreUploadAllowList = {
|
||||
avatar: IMAGE_TYPES,
|
||||
mediaLibrary: [...IMAGE_TYPES_WITH_GIF, "image/svg+xml", "image/x-icon", "image/vnd.microsoft.icon"],
|
||||
emailAttachment: [...IMAGE_TYPES_WITH_GIF, "application/pdf", "text/plain", "text/csv", "application/zip", ...OFFICE_TYPES],
|
||||
formUpload: [...IMAGE_TYPES, "application/pdf"],
|
||||
formUpload: [...IMAGE_TYPES, "image/heic", "image/heif", "application/pdf", ...OFFICE_TYPES, ...AUDIO_TYPES],
|
||||
kbAsset: [...IMAGE_TYPES, "application/pdf"],
|
||||
};
|
||||
|
||||
|
||||
+12
-4
@@ -6,10 +6,18 @@ import { describe, expect, test, vi } from "vitest";
|
||||
// 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`).
|
||||
// Mirrors the real clamscan surface: ping + scanStream, no scanBuffer.
|
||||
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 }));
|
||||
const scanStream = vi.fn(async (stream: NodeJS.ReadableStream) => {
|
||||
const chunks: Buffer[] = [];
|
||||
for await (const chunk of stream) {
|
||||
chunks.push(Buffer.from(chunk as Buffer));
|
||||
}
|
||||
scannedPayloads.push(Buffer.concat(chunks));
|
||||
return { isInfected: false as boolean, viruses: [] as string[] };
|
||||
});
|
||||
const scannedPayloads: Buffer[] = [];
|
||||
const init = vi.fn(async () => ({ ping, scanStream }));
|
||||
|
||||
// `default: undefined` keeps the property declared (so the source's `.default`
|
||||
// access is legal under vitest's strict ESM namespace) while still being
|
||||
@@ -33,7 +41,7 @@ describe("ClamAVStorageScanner with a default-less clamscan module", () => {
|
||||
// init() is called directly on the module export, never via `new`.
|
||||
expect(init).toHaveBeenCalledTimes(1);
|
||||
expect(ping).toHaveBeenCalled();
|
||||
expect(scanBuffer).toHaveBeenCalledWith(Buffer.from("hello"));
|
||||
expect(scannedPayloads.at(-1)).toEqual(Buffer.from("hello"));
|
||||
});
|
||||
|
||||
test("reuses the cached client across scans (init runs once per scanner)", async () => {
|
||||
|
||||
+20
-9
@@ -3,12 +3,21 @@ 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.
|
||||
// mocking the lazily-imported `clamscan` package. The mock MUST mirror the real
|
||||
// client surface: NodeClam.init() returns ping/scanStream/scanFile/scanDir and
|
||||
// has NO scanBuffer. An earlier mock invented one, which hid a TypeError on
|
||||
// every buffer scan in production (fail-closed => every avatar upload rejected).
|
||||
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 }));
|
||||
const scanStream = vi.fn(async (stream: NodeJS.ReadableStream) => {
|
||||
const chunks: Buffer[] = [];
|
||||
for await (const chunk of stream) {
|
||||
chunks.push(Buffer.from(chunk as Buffer));
|
||||
}
|
||||
scannedPayloads.push(Buffer.concat(chunks));
|
||||
return { isInfected: false as boolean, viruses: [] as string[] };
|
||||
});
|
||||
const scannedPayloads: Buffer[] = [];
|
||||
const init = vi.fn(async () => ({ ping, scanStream }));
|
||||
|
||||
vi.mock("clamscan", () => {
|
||||
class NodeClam {
|
||||
@@ -22,7 +31,7 @@ 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 () => {
|
||||
test("initializes the clamscan factory and streams a buffer payload to clamd", async () => {
|
||||
const scanner = createClamAVStorageScanner({
|
||||
...baseConfig,
|
||||
loadObject: async () => Buffer.from("hello world"),
|
||||
@@ -33,11 +42,13 @@ describe("ClamAVStorageScanner with the real (mocked) clamscan client", () => {
|
||||
expect(result.verdict).toBe("clean");
|
||||
expect(init).toHaveBeenCalledTimes(1);
|
||||
expect(ping).toHaveBeenCalled();
|
||||
expect(scanBuffer).toHaveBeenCalledWith(Buffer.from("hello world"));
|
||||
// The bytes must arrive intact over INSTREAM, not as the integers a Buffer
|
||||
// iterates into (the Readable.from(buffer) trap).
|
||||
expect(scannedPayloads.at(-1)).toEqual(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"] });
|
||||
test("adapts an infected verdict into the scanner result", async () => {
|
||||
scanStream.mockResolvedValueOnce({ isInfected: true, viruses: ["Win.Test.EICAR_HDB-1"] });
|
||||
const scanner = createClamAVStorageScanner({
|
||||
...baseConfig,
|
||||
loadObject: async () => Buffer.from("infected"),
|
||||
|
||||
+30
-1
@@ -1,7 +1,13 @@
|
||||
// @vitest-environment node
|
||||
import { Buffer } from "node:buffer";
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
import { ClamAVUnreachableError, createClamAVStorageScanner, isClamAVUnreachableError, type ClamScanClient } from "./clamav-scanner.js";
|
||||
import {
|
||||
ClamAVUnreachableError,
|
||||
createClamAVBufferScanner,
|
||||
createClamAVStorageScanner,
|
||||
isClamAVUnreachableError,
|
||||
type ClamScanClient,
|
||||
} from "./clamav-scanner.js";
|
||||
|
||||
const EICAR_BUFFER = Buffer.from("X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*", "utf8");
|
||||
const CLEAN_BUFFER = Buffer.from("hello world", "utf8");
|
||||
@@ -64,6 +70,17 @@ describe("ClamAVStorageScanner", () => {
|
||||
if (result.verdict === "infected") expect(result.signature).toBe("unknown");
|
||||
});
|
||||
|
||||
test("fails closed when clamd returns an indeterminate verdict", async () => {
|
||||
const client = buildClient({ scanBuffer: vi.fn(async () => ({ isInfected: null })) });
|
||||
const scanner = createClamAVStorageScanner({
|
||||
...baseConfig,
|
||||
loadObject: async () => CLEAN_BUFFER,
|
||||
initClient: async () => client,
|
||||
});
|
||||
|
||||
await expect(scanner.scanObject({ bucket: "uploads", key: "x" })).rejects.toThrow(ClamAVUnreachableError);
|
||||
});
|
||||
|
||||
test("throws ClamAVUnreachableError when isReachable returns false", async () => {
|
||||
const client = buildClient({ isReachable: vi.fn(async () => false) });
|
||||
const scanner = createClamAVStorageScanner({
|
||||
@@ -119,6 +136,18 @@ describe("ClamAVStorageScanner", () => {
|
||||
expect(initClient).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("reuses one client for synchronous pre-upload buffer scans", async () => {
|
||||
const client = buildClient();
|
||||
const initClient = vi.fn(async () => client);
|
||||
const scanner = createClamAVBufferScanner({ ...baseConfig, initClient });
|
||||
|
||||
await expect(scanner.scanBuffer(CLEAN_BUFFER)).resolves.toMatchObject({ verdict: "clean" });
|
||||
await expect(scanner.scanBuffer(CLEAN_BUFFER)).resolves.toMatchObject({ verdict: "clean" });
|
||||
|
||||
expect(initClient).toHaveBeenCalledTimes(1);
|
||||
expect(client.scanBuffer).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
test("isClamAVUnreachableError narrows arbitrary errors", () => {
|
||||
expect(isClamAVUnreachableError(new ClamAVUnreachableError("nope"))).toBe(true);
|
||||
expect(isClamAVUnreachableError(new Error("regular"))).toBe(false);
|
||||
|
||||
+50
-8
@@ -4,6 +4,7 @@
|
||||
|
||||
/// <reference path="./clamscan.d.ts" />
|
||||
|
||||
import { Readable } from "node:stream";
|
||||
import type { StorageScanner, StorageScannerInput, StorageScannerResult } from "./index.js";
|
||||
|
||||
export interface ClamAVScannerConfig {
|
||||
@@ -34,6 +35,12 @@ export interface ClamAVScannerConfig {
|
||||
now?: () => Date;
|
||||
}
|
||||
|
||||
export type ClamAVBufferScannerConfig = Omit<ClamAVScannerConfig, "loadObject">;
|
||||
|
||||
export interface ClamAVBufferScanner {
|
||||
scanBuffer(buffer: Buffer): Promise<StorageScannerResult>;
|
||||
}
|
||||
|
||||
export interface ClamScanResult {
|
||||
isInfected: boolean | null;
|
||||
viruses?: readonly string[];
|
||||
@@ -51,6 +58,26 @@ export interface ClamScanClient {
|
||||
const ENGINE = "clamav";
|
||||
|
||||
export function createClamAVStorageScanner(config: ClamAVScannerConfig): StorageScanner {
|
||||
const payloadScanner = createClamAVPayloadScanner(config);
|
||||
|
||||
return {
|
||||
async scanObject(target: StorageScannerInput): Promise<StorageScannerResult> {
|
||||
const payload = await config.loadObject(target);
|
||||
return payloadScanner.scan(payload);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function createClamAVBufferScanner(config: ClamAVBufferScannerConfig): ClamAVBufferScanner {
|
||||
const payloadScanner = createClamAVPayloadScanner(config);
|
||||
return {
|
||||
scanBuffer(buffer) {
|
||||
return payloadScanner.scan(buffer);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function createClamAVPayloadScanner(config: ClamAVBufferScannerConfig) {
|
||||
const initClient = config.initClient ?? defaultInitClient;
|
||||
let cachedClient: ClamScanClient | undefined;
|
||||
|
||||
@@ -62,19 +89,19 @@ export function createClamAVStorageScanner(config: ClamAVScannerConfig): Storage
|
||||
}
|
||||
|
||||
return {
|
||||
async scanObject(target: StorageScannerInput): Promise<StorageScannerResult> {
|
||||
async scan(payload: Buffer | NodeJS.ReadableStream): Promise<StorageScannerResult> {
|
||||
const client = await getClient();
|
||||
const reachable = await client.isReachable().catch(() => false);
|
||||
if (!reachable) {
|
||||
throw new ClamAVUnreachableError(`clamd at ${config.host}:${config.port} is unreachable`);
|
||||
}
|
||||
|
||||
const payload = await config.loadObject(target);
|
||||
const result = await scanWithClient(client, payload);
|
||||
|
||||
if (result.isInfected) {
|
||||
const signature = result.viruses?.[0] ?? "unknown";
|
||||
return { verdict: "infected", signature, engine: ENGINE };
|
||||
return { verdict: "infected", signature: result.viruses?.[0] ?? "unknown", engine: ENGINE };
|
||||
}
|
||||
if (result.isInfected !== false) {
|
||||
throw new ClamAVUnreachableError("clamd returned an indeterminate scan verdict");
|
||||
}
|
||||
return { verdict: "clean", engine: ENGINE };
|
||||
},
|
||||
@@ -114,7 +141,11 @@ async function defaultInitClient({ host, port, timeoutMs }: { host: string; port
|
||||
return Boolean(status);
|
||||
},
|
||||
async scanBuffer(buffer) {
|
||||
const res = await client.scanBuffer(buffer);
|
||||
// clamscan has no scanBuffer — the daemon-backed client only exposes
|
||||
// ping/scanStream/scanFile/scanDir. Feed the in-memory bytes through
|
||||
// scanStream (clamd INSTREAM), which is what STANDARDS §13 mandates
|
||||
// anyway: no path scan, no shared filesystem with the AV host.
|
||||
const res = await client.scanStream(bufferToStream(buffer));
|
||||
return { isInfected: res.isInfected, viruses: res.viruses };
|
||||
},
|
||||
async scanStream(stream) {
|
||||
@@ -124,11 +155,22 @@ async function defaultInitClient({ host, port, timeoutMs }: { host: string; port
|
||||
};
|
||||
}
|
||||
|
||||
// A plain byte stream, not Readable.from(buffer) — a Buffer iterates as single
|
||||
// integers, so Readable.from would emit numbers instead of the payload.
|
||||
function bufferToStream(buffer: Buffer): NodeJS.ReadableStream {
|
||||
return new Readable({
|
||||
read() {
|
||||
this.push(buffer);
|
||||
this.push(null);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Subset of the clamscan npm package surface that we actually use. Avoids a
|
||||
// dependency on @types/clamscan and lets the lazy import stay loose.
|
||||
// dependency on @types/clamscan and lets the lazy import stay loose. Keep this
|
||||
// in sync with the real client: it does NOT have scanBuffer.
|
||||
interface ClamScanLib {
|
||||
ping(): Promise<unknown>;
|
||||
scanBuffer(buffer: Buffer): Promise<{ isInfected: boolean | null; viruses?: string[] }>;
|
||||
scanStream(stream: NodeJS.ReadableStream): Promise<{ isInfected: boolean | null; viruses?: string[] }>;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,12 @@ export { createMagicBytesScanAdapter, type MagicBytesScanAdapterOptions } from "
|
||||
export { DEFAULT_ALLOW_LIST, resolveAllowList } from "./allow-list.js";
|
||||
export {
|
||||
createClamAVStorageScanner,
|
||||
createClamAVBufferScanner,
|
||||
ClamAVUnreachableError,
|
||||
isClamAVUnreachableError,
|
||||
type ClamAVScannerConfig,
|
||||
type ClamAVBufferScanner,
|
||||
type ClamAVBufferScannerConfig,
|
||||
type ClamScanClient,
|
||||
type ClamScanResult,
|
||||
} from "./clamav-scanner.js";
|
||||
|
||||
+18
-4
@@ -2,16 +2,30 @@
|
||||
import { Buffer } from "node:buffer";
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { DEFAULT_ALLOW_LIST } from "./allow-list.js";
|
||||
import { createMagicBytesScanAdapter } from "./magic-bytes-adapter.js";
|
||||
import type { PreUploadAllowList } from "./pre-upload-types.js";
|
||||
|
||||
const REAL_PNG = Buffer.from(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
|
||||
"base64",
|
||||
);
|
||||
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("accepts plain markdown when the context explicitly allows it", async () => {
|
||||
const adapter = createMagicBytesScanAdapter({
|
||||
allowList: {
|
||||
...DEFAULT_ALLOW_LIST,
|
||||
formUpload: [...DEFAULT_ALLOW_LIST.formUpload, "text/markdown"],
|
||||
},
|
||||
});
|
||||
const result = await adapter.scan({
|
||||
buffer: new TextEncoder().encode("# Pilot notes\n"),
|
||||
declaredMimeType: "text/markdown",
|
||||
filename: "pilot.md",
|
||||
context: "formUpload",
|
||||
});
|
||||
expect(result.status).toBe("clean");
|
||||
});
|
||||
|
||||
test("rejects an empty filename before inspecting the buffer", async () => {
|
||||
const adapter = createMagicBytesScanAdapter();
|
||||
const result = await adapter.scan({
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ const FILENAME_BAD_PATTERN = /[\x00-\x1f\x7f]|\.\.[\\/]/;
|
||||
const FILENAME_MAX_LENGTH = 255;
|
||||
|
||||
const SVG_SIGNATURE = /^<\?xml[\s\S]*?<svg\b|^<svg\b/i;
|
||||
const TEXT_PLAIN_FALLBACK_MIMES = new Set(["text/plain", "text/csv"]);
|
||||
const TEXT_PLAIN_FALLBACK_MIMES = new Set(["text/plain", "text/csv", "text/markdown"]);
|
||||
|
||||
export interface MagicBytesScanAdapterOptions {
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,7 @@ export type PreUploadScanReason =
|
||||
| "mime_undetectable"
|
||||
| "filename_invalid"
|
||||
| "scanner_disabled"
|
||||
| "malware_detected"
|
||||
| "scanner_error";
|
||||
|
||||
export interface PreUploadScanInput {
|
||||
|
||||
Reference in new issue
Block a user