bunsane 0.2.10 → 0.3.0

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/query/Query.ts CHANGED
@@ -12,6 +12,7 @@ import { getMetadataStorage } from "../core/metadata";
12
12
  import { shouldUseDirectPartition } from "../core/Config";
13
13
  import type { SQL } from "bun";
14
14
  import type { ComponentConstructor, TypedEntity, ComponentRecord } from "../types/query.types";
15
+ import { assertComponentTableName, assertFieldPath } from "./SqlIdentifier";
15
16
 
16
17
  export type FilterOperator = "=" | ">" | "<" | ">=" | "<=" | "!=" | "LIKE" | "ILIKE" | "IN" | "NOT IN" | string;
17
18
 
@@ -338,7 +339,13 @@ class Query<TComponents extends readonly ComponentConstructor[] = []> {
338
339
  throw new Error(`Component ${component.name} not registered`);
339
340
  }
340
341
 
341
- const tableName = ComponentRegistry.getPartitionTableName(typeId);
342
+ // Validate the resolved partition table name against the component
343
+ // table allow-list before passing to pg_class lookup. Although
344
+ // `relname` here is a bound parameter ($1) and cannot inject SQL
345
+ // directly, we still reject unexpected names so a registry
346
+ // poisoning bug cannot query arbitrary tables.
347
+ const rawTableName = ComponentRegistry.getPartitionTableName(typeId);
348
+ const tableName = rawTableName ? assertComponentTableName(rawTableName, 'estimatedCount.tableName') : null;
342
349
  const dbConn = this.getDb();
343
350
 
344
351
  // Use PostgreSQL's statistics for fast count estimate
@@ -557,10 +564,17 @@ class Query<TComponents extends readonly ComponentConstructor[] = []> {
557
564
  // Execute the DAG to get the base query
558
565
  const result = dag.execute(this.context);
559
566
 
560
- // Determine the component table name
561
- const componentTableName = shouldUseDirectPartition()
567
+ // Determine the component table name. Validate against allow-list so
568
+ // a poisoned registry cannot inject SQL through the embedded name.
569
+ const rawComponentTableName = shouldUseDirectPartition()
562
570
  ? (ComponentRegistry.getPartitionTableName(typeId) || 'components')
563
571
  : 'components';
572
+ const componentTableName = assertComponentTableName(rawComponentTableName, 'doAggregate.componentTableName');
573
+
574
+ // Validate the field path — each dotted segment must be a safe
575
+ // identifier. Without this, a caller-supplied field with quote or
576
+ // `->` metacharacters would corrupt the JSON path expression (C08).
577
+ assertFieldPath(field, 'doAggregate.field');
564
578
 
565
579
  // Build the JSON path for the field
566
580
  let jsonPath: string;
@@ -641,6 +655,19 @@ AND c.deleted_at IS NULL`;
641
655
  */
642
656
  @timed("Query.exec")
643
657
  public async exec(): Promise<TypedEntity<TComponents>[]> {
658
+ // Apply default LIMIT so unbounded queries cannot load entire tables
659
+ // into memory. Configurable via BUNSANE_DEFAULT_QUERY_LIMIT, 0 to
660
+ // disable. When the default is applied without an explicit .take(),
661
+ // warn once at execution so developers notice runaway queries
662
+ // (H-QUERY-1).
663
+ if (this.context.limit === null || this.context.limit === undefined) {
664
+ const envLimit = parseInt(process.env.BUNSANE_DEFAULT_QUERY_LIMIT ?? '10000', 10);
665
+ if (envLimit > 0) {
666
+ this.context.limit = envLimit;
667
+ logger.warn({ scope: 'Query.exec', defaultLimit: envLimit }, 'Query executed without explicit .take() — applying framework default LIMIT. Call .take(N) to suppress this warning.');
668
+ }
669
+ }
670
+
644
671
  return new Promise<TypedEntity<TComponents>[]>((resolve, reject) => {
645
672
  // Add timeout to prevent hanging queries
646
673
  const timeout = setTimeout(() => {
@@ -0,0 +1,105 @@
1
+ /**
2
+ * SQL identifier sanitization helpers.
3
+ *
4
+ * These helpers prevent SQL injection when interpolating caller-supplied or
5
+ * metadata-derived strings into SQL via `db.unsafe(...)` or template literals.
6
+ * Parameter binding (`$1`, `$2`) is always preferred for values, but column
7
+ * names, table names, ORDER BY fields, and JSON path segments cannot be
8
+ * parameterized — they are sanitized against a strict allow-list instead.
9
+ *
10
+ * Ticket C08.
11
+ */
12
+
13
+ /** Matches a safe identifier: letter/underscore followed by letters/digits/underscores. */
14
+ const IDENT_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
15
+
16
+ /** Matches a safe component table name: `components` or `components_<ident>`. */
17
+ const COMPONENT_TABLE_RE = /^components(?:_[a-z0-9_]+)?$/;
18
+
19
+ /**
20
+ * PostgreSQL text-search languages supported by `to_tsvector(config, ...)`.
21
+ * Extend as needed for deployments with additional dictionaries installed.
22
+ */
23
+ const ALLOWED_TS_LANGUAGES = new Set<string>([
24
+ 'simple',
25
+ 'english',
26
+ 'french',
27
+ 'german',
28
+ 'spanish',
29
+ 'italian',
30
+ 'portuguese',
31
+ 'dutch',
32
+ 'russian',
33
+ 'swedish',
34
+ 'norwegian',
35
+ 'danish',
36
+ 'finnish',
37
+ 'turkish',
38
+ 'hungarian',
39
+ 'arabic',
40
+ 'indonesian',
41
+ 'irish',
42
+ 'lithuanian',
43
+ 'nepali',
44
+ 'romanian',
45
+ 'tamil',
46
+ 'yiddish',
47
+ ]);
48
+
49
+ /**
50
+ * Assert a string is a safe SQL identifier (column name, alias). Throws
51
+ * `InvalidIdentifierError` if not.
52
+ */
53
+ export function assertIdentifier(value: unknown, context: string): string {
54
+ if (typeof value !== 'string' || !IDENT_RE.test(value)) {
55
+ throw new InvalidIdentifierError(context, String(value));
56
+ }
57
+ return value;
58
+ }
59
+
60
+ /**
61
+ * Assert a string is a safe component table name (e.g. `components`,
62
+ * `components_user`). Throws if not.
63
+ */
64
+ export function assertComponentTableName(value: unknown, context: string): string {
65
+ if (typeof value !== 'string' || !COMPONENT_TABLE_RE.test(value)) {
66
+ throw new InvalidIdentifierError(context, String(value));
67
+ }
68
+ return value;
69
+ }
70
+
71
+ /**
72
+ * Assert a dotted JSON field path is safe. Each segment must be a valid
73
+ * identifier. Empty paths / empty segments are rejected.
74
+ */
75
+ export function assertFieldPath(value: unknown, context: string): string {
76
+ if (typeof value !== 'string' || value.length === 0) {
77
+ throw new InvalidIdentifierError(context, String(value));
78
+ }
79
+ const parts = value.split('.');
80
+ for (const p of parts) {
81
+ if (!IDENT_RE.test(p)) {
82
+ throw new InvalidIdentifierError(context, value);
83
+ }
84
+ }
85
+ return value;
86
+ }
87
+
88
+ /**
89
+ * Assert a text-search language is in the allow-list. Defaults to `simple`
90
+ * when undefined (behavior preserved from the prior implementation).
91
+ */
92
+ export function assertTsLanguage(value: unknown, context: string = 'tsLanguage'): string {
93
+ if (value === undefined || value === null) return 'simple';
94
+ if (typeof value !== 'string' || !ALLOWED_TS_LANGUAGES.has(value.toLowerCase())) {
95
+ throw new InvalidIdentifierError(context, String(value));
96
+ }
97
+ return value.toLowerCase();
98
+ }
99
+
100
+ export class InvalidIdentifierError extends Error {
101
+ constructor(context: string, value: string) {
102
+ super(`Invalid SQL identifier in ${context}: ${JSON.stringify(value)}`);
103
+ this.name = 'InvalidIdentifierError';
104
+ }
105
+ }
@@ -8,6 +8,7 @@
8
8
  import type { FilterBuilder, FilterBuilderOptions } from "../FilterBuilder";
9
9
  import type { QueryFilter } from "../QueryContext";
10
10
  import type { QueryContext } from "../QueryContext";
11
+ import { assertTsLanguage, assertFieldPath } from "../SqlIdentifier";
11
12
 
12
13
  /**
13
14
  * Full-text search filter value interface
@@ -68,12 +69,16 @@ export const fullTextSearchBuilder: FilterBuilder = (
68
69
  const value = filter.value as FullTextFilterValue;
69
70
  const { query, language = 'english', type = 'plain' } = value;
70
71
 
72
+ // Validate identifiers (C08).
73
+ const safeLanguage = assertTsLanguage(language, 'fullTextSearchBuilder.language');
74
+ assertFieldPath(filter.field, 'fullTextSearchBuilder.field');
75
+
71
76
  // Build the text search vector from the specified field
72
77
  const fieldPath = filter.field.includes('.')
73
78
  ? filter.field.split('.').map(p => `'${p}'`).join('->')
74
79
  : `'${filter.field}'`;
75
80
 
76
- const vectorSql = `to_tsvector('${language}', ${alias}.data->${fieldPath})`;
81
+ const vectorSql = `to_tsvector('${safeLanguage}', ${alias}.data->${fieldPath})`;
77
82
 
78
83
  // Choose the appropriate query function based on type
79
84
  let queryFunction: string;
@@ -93,7 +98,7 @@ export const fullTextSearchBuilder: FilterBuilder = (
93
98
  break;
94
99
  }
95
100
 
96
- const querySql = `${queryFunction}('${language}', $${context.addParam(query)})`;
101
+ const querySql = `${queryFunction}('${safeLanguage}', $${context.addParam(query)})`;
97
102
 
98
103
  return {
99
104
  sql: `${vectorSql} @@ ${querySql}`,
@@ -120,12 +125,16 @@ export const fullTextSearchWithRankBuilder: FilterBuilder = (
120
125
  const value = filter.value as FullTextFilterValue;
121
126
  const { query, language = 'english', type = 'plain' } = value;
122
127
 
128
+ // Validate identifiers (C08).
129
+ const safeLanguage = assertTsLanguage(language, 'fullTextSearchWithRankBuilder.language');
130
+ assertFieldPath(filter.field, 'fullTextSearchWithRankBuilder.field');
131
+
123
132
  // Build the text search vector from the specified field
124
133
  const fieldPath = filter.field.includes('.')
125
134
  ? filter.field.split('.').map(p => `'${p}'`).join('->')
126
135
  : `'${filter.field}'`;
127
136
 
128
- const vectorSql = `to_tsvector('${language}', ${alias}.data->${fieldPath})`;
137
+ const vectorSql = `to_tsvector('${safeLanguage}', ${alias}.data->${fieldPath})`;
129
138
 
130
139
  // Choose the appropriate query function based on type
131
140
  let queryFunction: string;
@@ -145,7 +154,7 @@ export const fullTextSearchWithRankBuilder: FilterBuilder = (
145
154
  break;
146
155
  }
147
156
 
148
- const querySql = `${queryFunction}('${language}', $${context.addParam(query)})`;
157
+ const querySql = `${queryFunction}('${safeLanguage}', $${context.addParam(query)})`;
149
158
 
150
159
  // Include ranking in the condition (can be used for ordering)
151
160
  const rankSql = `ts_rank(${vectorSql}, ${querySql})`;
@@ -187,16 +196,20 @@ export function createFullTextSearchBuilder(
187
196
  language: string = 'english',
188
197
  searchType: 'plain' | 'phrase' | 'web' | 'tsquery' = 'plain'
189
198
  ): { builder: FilterBuilder; options: FilterBuilderOptions } {
199
+ // Validate the language at factory-construction time so callers get an
200
+ // immediate error on invalid input rather than a runtime SQL error.
201
+ const safeLanguage = assertTsLanguage(language, 'createFullTextSearchBuilder.language');
190
202
  const builder: FilterBuilder = (filter: QueryFilter, alias: string, context: QueryContext) => {
191
203
  const value = filter.value as FullTextFilterValue;
192
204
  const query = value.query;
205
+ assertFieldPath(filter.field, 'createFullTextSearchBuilder.field');
193
206
 
194
207
  // Build the text search vector from the specified field
195
208
  const fieldPath = filter.field.includes('.')
196
209
  ? filter.field.split('.').map(p => `'${p}'`).join('->')
197
210
  : `'${filter.field}'`;
198
211
 
199
- const vectorSql = `to_tsvector('${language}', ${alias}.data->${fieldPath})`;
212
+ const vectorSql = `to_tsvector('${safeLanguage}', ${alias}.data->${fieldPath})`;
200
213
 
201
214
  // Choose the appropriate query function based on type
202
215
  let queryFunction: string;
@@ -216,7 +229,7 @@ export function createFullTextSearchBuilder(
216
229
  break;
217
230
  }
218
231
 
219
- const querySql = `${queryFunction}('${language}', $${context.addParam(query)})`;
232
+ const querySql = `${queryFunction}('${safeLanguage}', $${context.addParam(query)})`;
220
233
 
221
234
  return {
222
235
  sql: `${vectorSql} @@ ${querySql}`,
@@ -1,5 +1,5 @@
1
1
  import type BaseService from "./Service";
2
- import ApplicationLifecycle, {ApplicationPhase} from "../core/ApplicationLifecycle";
2
+ import ApplicationLifecycle, {ApplicationPhase, type PhaseChangeEvent} from "../core/ApplicationLifecycle";
3
3
  import { generateGraphQLSchemaV2 } from "../gql";
4
4
  import { GraphQLSchema } from "graphql";
5
5
 
@@ -8,28 +8,41 @@ class ServiceRegistry {
8
8
 
9
9
  private services: Map<string, BaseService> = new Map();
10
10
  private schema: GraphQLSchema | null = null;
11
+ private phaseListener: ((event: PhaseChangeEvent) => void) | null = null;
11
12
 
12
13
 
13
14
  constructor() {
14
-
15
+
15
16
  }
16
17
 
17
18
  public init() {
18
- ApplicationLifecycle.addPhaseListener((event) => {
19
+ // Remove previous listener if re-init (tests) to prevent listener stacking.
20
+ if (this.phaseListener) {
21
+ ApplicationLifecycle.removePhaseListener(this.phaseListener);
22
+ }
23
+ this.phaseListener = (event: PhaseChangeEvent) => {
19
24
  switch(event.detail) {
20
25
  case ApplicationPhase.SYSTEM_REGISTERING: {
21
26
  const servicesArray = Array.from(this.services.values());
22
-
23
- const result = generateGraphQLSchemaV2(servicesArray, {
24
- enableArchetypeOperations: false
27
+
28
+ const result = generateGraphQLSchemaV2(servicesArray, {
29
+ enableArchetypeOperations: false
25
30
  });
26
-
31
+
27
32
  this.schema = result.schema;
28
33
  ApplicationLifecycle.setPhase(ApplicationPhase.SYSTEM_READY);
29
34
  break;
30
35
  };
31
36
  }
32
- });
37
+ };
38
+ ApplicationLifecycle.addPhaseListener(this.phaseListener);
39
+ }
40
+
41
+ public dispose(): void {
42
+ if (this.phaseListener) {
43
+ ApplicationLifecycle.removePhaseListener(this.phaseListener);
44
+ this.phaseListener = null;
45
+ }
33
46
  }
34
47
 
35
48
 
@@ -1,4 +1,6 @@
1
1
  import fs from "fs";
2
+ import { pipeline } from "stream/promises";
3
+ import { Readable } from "stream";
2
4
  import path from "path";
3
5
  import { StorageProvider } from "./StorageProvider";
4
6
  import type { UploadConfiguration, StorageResult, FileMetadata } from "../types/upload.types";
@@ -51,9 +53,16 @@ export class LocalStorageProvider extends StorageProvider {
51
53
  fs.mkdirSync(uploadDir, { recursive: true });
52
54
  }
53
55
 
54
- // Write file to disk
55
- const buffer = Buffer.from(await file.arrayBuffer());
56
- fs.writeFileSync(fullPath, buffer);
56
+ // Stream File → disk to avoid full in-memory buffering of large uploads.
57
+ const writeStream = fs.createWriteStream(fullPath);
58
+ try {
59
+ const webStream = file.stream() as ReadableStream<Uint8Array>;
60
+ const nodeStream = Readable.fromWeb(webStream as any);
61
+ await pipeline(nodeStream, writeStream);
62
+ } catch (streamError) {
63
+ try { fs.unlinkSync(fullPath); } catch { /* best-effort cleanup */ }
64
+ throw streamError;
65
+ }
57
66
 
58
67
  // Generate URL
59
68
  const url = this.buildUrl(relativePath);
@@ -213,11 +213,11 @@ export class S3StorageProvider extends StorageProvider {
213
213
  const destKey = this.resolveKey(destinationPath);
214
214
 
215
215
  try {
216
- const [data, stat] = await Promise.all([
217
- this.client.file(sourceKey).arrayBuffer(),
218
- this.client.stat(sourceKey),
219
- ]);
220
- await this.client.write(destKey, data, {
216
+ const stat = await this.client.stat(sourceKey);
217
+ const sourceFile = this.client.file(sourceKey);
218
+ // Pass the S3File directly so Bun streams bytes rather than loading
219
+ // the entire object into memory (previously `arrayBuffer()`).
220
+ await this.client.write(destKey, sourceFile, {
221
221
  type: stat.type,
222
222
  acl: this.acl,
223
223
  });
@@ -226,7 +226,7 @@ export class S3StorageProvider extends StorageProvider {
226
226
  } catch (error) {
227
227
  const msg =
228
228
  error instanceof Error ? error.message : "Unknown error";
229
- logger.error({ sourceKey, destKey }, "Failed to copy file");
229
+ logger.error({ sourceKey, destKey, err: msg }, "Failed to copy file");
230
230
  return false;
231
231
  }
232
232
  }
@@ -76,8 +76,12 @@ describe("E2E HTTP Routes", () => {
76
76
 
77
77
  it("OPTIONS /health returns 204 when CORS configured", async () => {
78
78
  app.setCors({ origin: "*" });
79
- // Re-compose middleware chain not needed CORS headers are added per-request in handleRequest
80
- const res = await fetch(`${BASE}/health`, { method: "OPTIONS" });
79
+ // Preflight must carry Origin header per CORS spec; otherwise the
80
+ // server emits no Access-Control-Allow-Origin (no `|| '*'` fallback).
81
+ const res = await fetch(`${BASE}/health`, {
82
+ method: "OPTIONS",
83
+ headers: { Origin: "https://client.example" },
84
+ });
81
85
  expect(res.status).toBe(204);
82
86
  expect(res.headers.get("Access-Control-Allow-Origin")).toBe("*");
83
87
  });
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Integration tests for Entity.save timeout and cancellation behavior.
3
+ *
4
+ * Regression coverage for the production incident where Entity.save's wall-
5
+ * clock timeout rejected the outer Promise but left the underlying Bun SQL
6
+ * transaction mid-flight. Under pgbouncer transaction-mode pooling this
7
+ * leaked backend PostgreSQL sessions into `idle in transaction` state,
8
+ * exhausting the pool.
9
+ *
10
+ * These tests prove the invariants the fix must uphold:
11
+ * 1. An aborted save leaves no partial rows — Bun SQL's transaction callback
12
+ * throws, auto-ROLLBACK fires, backend connection is released.
13
+ * 2. The connection pool stays healthy after repeated aborts — subsequent
14
+ * saves on fresh entities still succeed.
15
+ * 3. A save with no abort still commits normally.
16
+ *
17
+ * The wall-clock DB_QUERY_TIMEOUT path is module-cached at import time so it
18
+ * is not exercised here directly. Manual verification on a real Postgres +
19
+ * pgbouncer stack (with query_wait_timeout short enough to fire) should
20
+ * confirm pg_stat_activity shows no `idle in transaction` backends after
21
+ * this test suite runs. See the handoff doc (2026-04-18) for the repro steps.
22
+ */
23
+ import { describe, test, expect, beforeAll } from 'bun:test';
24
+ import { Entity } from '../../../core/Entity';
25
+ import db from '../../../database';
26
+ import { TestUser } from '../../fixtures/components';
27
+ import { createTestContext, ensureComponentsRegistered } from '../../utils';
28
+
29
+ describe('Entity.save timeout and cancellation', () => {
30
+ const ctx = createTestContext();
31
+
32
+ beforeAll(async () => {
33
+ await ensureComponentsRegistered(TestUser);
34
+ });
35
+
36
+ test('aborted doSave does not leave partial rows (transaction rolls back)', async () => {
37
+ const entity = ctx.tracker.create();
38
+ entity.add(TestUser, { name: 'aborted', email: 'a@example.com', age: 1 });
39
+
40
+ const controller = new AbortController();
41
+ // Abort immediately — the first in-flight query will be cancelled,
42
+ // the transaction callback throws, Bun SQL issues ROLLBACK.
43
+ queueMicrotask(() => controller.abort(new Error('simulated save timeout')));
44
+
45
+ const result = db.transaction(async (trx) => {
46
+ await entity.doSave(trx, controller.signal);
47
+ });
48
+
49
+ await expect(result).rejects.toBeDefined();
50
+
51
+ // Entity must NOT exist — rollback invariant.
52
+ const rows = await db`SELECT id FROM entities WHERE id = ${entity.id}`;
53
+ expect(rows.length).toBe(0);
54
+ });
55
+
56
+ test('connection pool stays healthy after multiple aborted saves', async () => {
57
+ // Repeatedly abort saves — if connections leaked, subsequent saves
58
+ // would eventually block on pool acquire.
59
+ for (let i = 0; i < 8; i++) {
60
+ const entity = Entity.Create();
61
+ entity.add(TestUser, { name: `aborted-${i}`, email: `a${i}@e.com`, age: i });
62
+
63
+ const controller = new AbortController();
64
+ queueMicrotask(() => controller.abort(new Error('simulated timeout')));
65
+
66
+ await db.transaction(async (trx) => {
67
+ await entity.doSave(trx, controller.signal);
68
+ }).catch(() => { /* expected */ });
69
+ }
70
+
71
+ // A fresh save must still succeed on the pool that serviced the aborts.
72
+ const healthy = ctx.tracker.create();
73
+ healthy.add(TestUser, { name: 'healthy', email: 'h@e.com', age: 99 });
74
+ await healthy.save();
75
+
76
+ expect(healthy._persisted).toBe(true);
77
+
78
+ const rows = await db`SELECT id FROM entities WHERE id = ${healthy.id}`;
79
+ expect(rows.length).toBe(1);
80
+ });
81
+
82
+ test('doSave without signal behaves normally (backwards compatible)', async () => {
83
+ const entity = ctx.tracker.create();
84
+ entity.add(TestUser, { name: 'no-signal', email: 'n@e.com', age: 5 });
85
+
86
+ await db.transaction(async (trx) => {
87
+ await entity.doSave(trx); // no signal passed
88
+ });
89
+
90
+ const rows = await db`SELECT id FROM entities WHERE id = ${entity.id}`;
91
+ expect(rows.length).toBe(1);
92
+ });
93
+
94
+ test('save() resolves even if post-commit cache work is slow (fire-and-forget)', async () => {
95
+ // Cache handler is queued via queueMicrotask; save() must resolve as
96
+ // soon as the DB transaction commits. We assert save resolves quickly
97
+ // even though handleCacheAfterSave is awaited separately.
98
+ const entity = ctx.tracker.create();
99
+ entity.add(TestUser, { name: 'fast', email: 'f@e.com', age: 10 });
100
+
101
+ const start = performance.now();
102
+ await entity.save();
103
+ const elapsed = performance.now() - start;
104
+
105
+ expect(entity._persisted).toBe(true);
106
+ // Generous bound — if cache were blocking save, timings under load
107
+ // could stretch past the budget. This just guards gross regressions.
108
+ expect(elapsed).toBeLessThan(5000);
109
+ });
110
+ });
@@ -378,11 +378,9 @@ describe("S3StorageProvider", () => {
378
378
 
379
379
  it("returns false on failure", async () => {
380
380
  const client = createMockS3Client({
381
- file: mock(() => ({
382
- arrayBuffer: async () => {
383
- throw new Error("Read failed");
384
- },
385
- })),
381
+ stat: mock(async () => {
382
+ throw new Error("Stat failed");
383
+ }),
386
384
  });
387
385
  const provider = new S3StorageProvider(
388
386
  { bucket: "my-bucket" },
@@ -413,11 +411,9 @@ describe("S3StorageProvider", () => {
413
411
 
414
412
  it("returns false if copy fails", async () => {
415
413
  const client = createMockS3Client({
416
- file: mock(() => ({
417
- arrayBuffer: async () => {
418
- throw new Error("Read failed");
419
- },
420
- })),
414
+ stat: mock(async () => {
415
+ throw new Error("Stat failed");
416
+ }),
421
417
  });
422
418
  const provider = new S3StorageProvider(
423
419
  { bucket: "my-bucket" },
@@ -258,31 +258,34 @@ export class FileValidator {
258
258
  * Check if file is potentially dangerous
259
259
  */
260
260
  public async isDangerous(file: File): Promise<boolean> {
261
- // Check for executable file extensions
262
261
  const dangerousExtensions = [
263
262
  '.exe', '.scr', '.bat', '.cmd', '.com', '.pif', '.vbs', '.js', '.jar',
264
- '.sh', '.py', '.pl', '.php', '.asp', '.aspx', '.jsp'
263
+ '.sh', '.py', '.pl', '.php', '.asp', '.aspx', '.jsp',
264
+ '.svg',
265
265
  ];
266
+ const dangerousMimeTypes = ['image/svg+xml'];
266
267
 
267
268
  const extension = this.getFileExtension(file.name);
268
269
  if (dangerousExtensions.includes(extension)) {
269
270
  return true;
270
271
  }
272
+ if (dangerousMimeTypes.includes(file.type)) {
273
+ return true;
274
+ }
271
275
 
272
- // Check for polyglot files (files that are valid in multiple formats)
273
276
  try {
274
277
  const buffer = await file.slice(0, 1024).arrayBuffer();
275
278
  const bytes = new Uint8Array(buffer);
276
279
  const content = new TextDecoder().decode(bytes);
277
-
278
- // Look for script patterns
280
+
279
281
  const scriptPatterns = [
280
282
  /<script/i,
281
283
  /javascript:/i,
282
284
  /vbscript:/i,
283
285
  /<iframe/i,
284
286
  /<object/i,
285
- /<embed/i
287
+ /<embed/i,
288
+ /on[a-z]+\s*=/i,
286
289
  ];
287
290
 
288
291
  return scriptPatterns.some(pattern => pattern.test(content));