94 lines
3.1 KiB
TypeScript
94 lines
3.1 KiB
TypeScript
// S3-backed implementation of the StorageQuarantineAdapter contract from
|
|
// ./index.ts. Server-side copies an infected object into a `quarantine/`
|
|
// prefix and deletes the original, so the file is gone from any public
|
|
// listing of the source key without ever round-tripping through the worker
|
|
// process memory.
|
|
|
|
import { CopyObjectCommand, DeleteObjectCommand, S3Client as AwsS3Client } from "@aws-sdk/client-s3";
|
|
import type { StorageQuarantineAdapter, StorageQuarantineInput, StorageQuarantineResult } from "./index.js";
|
|
|
|
export interface S3QuarantineConfig {
|
|
endpoint?: string;
|
|
region: string;
|
|
accessKeyId: string;
|
|
secretAccessKey: string;
|
|
sessionToken?: string;
|
|
forcePathStyle?: boolean;
|
|
/**
|
|
* Bucket the quarantined object lives in. Defaults to the same bucket as
|
|
* the source. Pass a dedicated quarantine bucket if your S3 ACL strategy
|
|
* keeps it isolated from public listing.
|
|
*/
|
|
quarantineBucket?: string;
|
|
/**
|
|
* Override the key derivation. Defaults to prefixing the source key with
|
|
* `quarantine/` so the bucket layout looks like quarantine/<originalKey>.
|
|
*/
|
|
buildQuarantineKey?: (input: StorageQuarantineInput) => string;
|
|
/**
|
|
* Test seam. Defaults to a real AwsS3Client constructed from the config.
|
|
*/
|
|
clientFactory?: () => AwsS3Client;
|
|
}
|
|
|
|
const DEFAULT_PREFIX = "quarantine/";
|
|
|
|
export function createS3StorageQuarantineAdapter(config: S3QuarantineConfig): StorageQuarantineAdapter {
|
|
const buildKey = config.buildQuarantineKey ?? defaultBuildQuarantineKey;
|
|
const clientFactory =
|
|
config.clientFactory ??
|
|
(() =>
|
|
new AwsS3Client({
|
|
endpoint: config.endpoint,
|
|
region: config.region,
|
|
forcePathStyle: config.forcePathStyle,
|
|
credentials: {
|
|
accessKeyId: config.accessKeyId,
|
|
secretAccessKey: config.secretAccessKey,
|
|
sessionToken: config.sessionToken,
|
|
},
|
|
}));
|
|
let client: AwsS3Client | undefined;
|
|
|
|
return {
|
|
async quarantineObject(input: StorageQuarantineInput): Promise<StorageQuarantineResult> {
|
|
const targetBucket = config.quarantineBucket ?? input.bucket;
|
|
const targetKey = buildKey(input);
|
|
|
|
if (!client) client = clientFactory();
|
|
await client.send(
|
|
new CopyObjectCommand({
|
|
Bucket: targetBucket,
|
|
Key: targetKey,
|
|
CopySource: `${input.bucket}/${encodeURIComponent(input.key)}`,
|
|
MetadataDirective: "REPLACE",
|
|
Metadata: {
|
|
"scan-signature": input.signature,
|
|
"scan-quarantined-at": new Date().toISOString(),
|
|
"scan-source-bucket": input.bucket,
|
|
"scan-source-key": input.key,
|
|
},
|
|
})
|
|
);
|
|
await client.send(
|
|
new DeleteObjectCommand({
|
|
Bucket: input.bucket,
|
|
Key: input.key,
|
|
})
|
|
);
|
|
|
|
return {
|
|
bucket: targetBucket,
|
|
key: targetKey,
|
|
quarantineKey: targetKey,
|
|
action: "moved",
|
|
};
|
|
},
|
|
};
|
|
}
|
|
|
|
function defaultBuildQuarantineKey(input: StorageQuarantineInput): string {
|
|
if (input.quarantineKey) return input.quarantineKey;
|
|
return `${DEFAULT_PREFIX}${input.key}`;
|
|
}
|