bunsane 0.2.9 → 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/CHANGELOG.md +266 -0
- package/config/cache.config.ts +12 -2
- package/core/App.ts +390 -66
- package/core/ApplicationLifecycle.ts +68 -4
- package/core/Entity.ts +407 -256
- package/core/EntityHookManager.ts +88 -21
- package/core/EntityManager.ts +12 -3
- package/core/Logger.ts +4 -0
- package/core/RequestContext.ts +4 -1
- package/core/SchedulerManager.ts +92 -9
- package/core/cache/CacheFactory.ts +3 -1
- package/core/cache/CacheManager.ts +54 -17
- package/core/cache/RedisCache.ts +38 -3
- package/core/decorators/EntityHooks.ts +24 -12
- package/core/middleware/RateLimit.ts +105 -0
- package/core/middleware/index.ts +1 -0
- package/core/remote/CircuitBreaker.ts +115 -0
- package/core/remote/OutboxWorker.ts +183 -0
- package/core/remote/RemoteManager.ts +400 -0
- package/core/remote/RpcCaller.ts +310 -0
- package/core/remote/StreamConsumer.ts +535 -0
- package/core/remote/decorators.ts +121 -0
- package/core/remote/health.ts +139 -0
- package/core/remote/index.ts +37 -0
- package/core/remote/metrics.ts +99 -0
- package/core/remote/outboxSchema.ts +41 -0
- package/core/remote/types.ts +151 -0
- package/core/scheduler/DistributedLock.ts +324 -266
- package/gql/builders/ResolverBuilder.ts +4 -4
- package/gql/complexityLimit.ts +95 -0
- package/gql/index.ts +15 -3
- package/gql/visitors/ResolverGeneratorVisitor.ts +16 -2
- package/package.json +1 -1
- package/query/ComponentInclusionNode.ts +13 -6
- package/query/OrNode.ts +2 -4
- package/query/Query.ts +30 -3
- package/query/SqlIdentifier.ts +105 -0
- package/query/builders/FullTextSearchBuilder.ts +19 -6
- package/service/ServiceRegistry.ts +21 -8
- package/storage/LocalStorageProvider.ts +12 -3
- package/storage/S3StorageProvider.ts +6 -6
- package/tests/e2e/http.test.ts +6 -2
- package/tests/helpers/MockRedisClient.ts +113 -0
- package/tests/helpers/MockRedisStreamServer.ts +448 -0
- package/tests/integration/entity/Entity.saveTimeout.test.ts +110 -0
- package/tests/integration/remote/dlq.test.ts +175 -0
- package/tests/integration/remote/event-dispatch.test.ts +114 -0
- package/tests/integration/remote/outbox.test.ts +130 -0
- package/tests/integration/remote/rpc.test.ts +177 -0
- package/tests/unit/remote/CircuitBreaker.test.ts +159 -0
- package/tests/unit/remote/RemoteError.test.ts +55 -0
- package/tests/unit/remote/decorators.test.ts +195 -0
- package/tests/unit/remote/metrics.test.ts +115 -0
- package/tests/unit/remote/mockRedisStreamServer.test.ts +104 -0
- package/tests/unit/storage/S3StorageProvider.test.ts +6 -10
- package/upload/FileValidator.ts +9 -6
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ASTVisitor,
|
|
3
|
+
type DocumentNode,
|
|
4
|
+
type FragmentDefinitionNode,
|
|
5
|
+
type ValidationContext,
|
|
6
|
+
GraphQLError,
|
|
7
|
+
Kind,
|
|
8
|
+
} from "graphql";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Lightweight GraphQL query complexity validator. Assigns each selected field
|
|
12
|
+
* a base cost of 1, multiplied by the value of a `first` / `limit` / `take`
|
|
13
|
+
* argument when present. Nested selections contribute their (multiplied) cost.
|
|
14
|
+
* Fragments are followed and deduped. Introspection queries are exempt.
|
|
15
|
+
*
|
|
16
|
+
* Not as expressive as `graphql-query-complexity` (no per-field estimators,
|
|
17
|
+
* no custom weights). Enough to block obviously abusive nested archetype
|
|
18
|
+
* relations without a heavyweight dep.
|
|
19
|
+
*/
|
|
20
|
+
export function complexityLimitRule(maxComplexity: number) {
|
|
21
|
+
return function ComplexityLimitValidationRule(
|
|
22
|
+
context: ValidationContext,
|
|
23
|
+
): ASTVisitor {
|
|
24
|
+
const document: DocumentNode = context.getDocument();
|
|
25
|
+
|
|
26
|
+
const fragments = new Map<string, FragmentDefinitionNode>();
|
|
27
|
+
for (const def of document.definitions) {
|
|
28
|
+
if (def.kind === Kind.FRAGMENT_DEFINITION) {
|
|
29
|
+
fragments.set(def.name.value, def);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const MULTIPLIER_ARGS = new Set(["first", "limit", "take"]);
|
|
34
|
+
|
|
35
|
+
function readMultiplier(args: readonly any[] | undefined): number {
|
|
36
|
+
if (!args) return 1;
|
|
37
|
+
for (const arg of args) {
|
|
38
|
+
if (!MULTIPLIER_ARGS.has(arg.name.value)) continue;
|
|
39
|
+
const v = arg.value;
|
|
40
|
+
if (v.kind === Kind.INT) {
|
|
41
|
+
const n = parseInt(v.value, 10);
|
|
42
|
+
if (Number.isFinite(n) && n > 0) return n;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return 1;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function cost(
|
|
49
|
+
node: { selectionSet?: { selections: readonly any[] } },
|
|
50
|
+
visited: Set<string>,
|
|
51
|
+
): number {
|
|
52
|
+
if (!node.selectionSet) return 0;
|
|
53
|
+
|
|
54
|
+
let total = 0;
|
|
55
|
+
for (const selection of node.selectionSet.selections) {
|
|
56
|
+
if (selection.kind === Kind.FIELD) {
|
|
57
|
+
const multiplier = readMultiplier(selection.arguments);
|
|
58
|
+
const childCost = cost(selection, visited);
|
|
59
|
+
total += multiplier * (1 + childCost);
|
|
60
|
+
} else if (selection.kind === Kind.INLINE_FRAGMENT) {
|
|
61
|
+
total += cost(selection, visited);
|
|
62
|
+
} else if (selection.kind === Kind.FRAGMENT_SPREAD) {
|
|
63
|
+
const name = selection.name.value;
|
|
64
|
+
if (visited.has(name)) continue;
|
|
65
|
+
visited.add(name);
|
|
66
|
+
const fragment = fragments.get(name);
|
|
67
|
+
if (fragment) total += cost(fragment, visited);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return total;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
OperationDefinition(node) {
|
|
75
|
+
if (node.selectionSet) {
|
|
76
|
+
const isIntrospection = node.selectionSet.selections.every(
|
|
77
|
+
(sel) =>
|
|
78
|
+
sel.kind === Kind.FIELD &&
|
|
79
|
+
sel.name.value.startsWith("__"),
|
|
80
|
+
);
|
|
81
|
+
if (isIntrospection) return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const total = cost(node, new Set());
|
|
85
|
+
if (total > maxComplexity) {
|
|
86
|
+
context.reportError(
|
|
87
|
+
new GraphQLError(
|
|
88
|
+
`Query complexity ${total} exceeds maximum allowed complexity of ${maxComplexity}`,
|
|
89
|
+
),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
}
|
package/gql/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {createSchema, createYoga, type Plugin} from 'graphql-yoga';
|
|
|
2
2
|
import { useValidationRule } from '@envelop/core';
|
|
3
3
|
import { GraphQLSchema, GraphQLError } from 'graphql';
|
|
4
4
|
import { depthLimitRule } from './depthLimit';
|
|
5
|
+
import { complexityLimitRule } from './complexityLimit';
|
|
5
6
|
import { GraphQLObjectType, GraphQLField, GraphQLOperation, GraphQLScalarType, GraphQLSubscription } from './Generator';
|
|
6
7
|
import {GraphQLFieldTypes} from "./types"
|
|
7
8
|
import {logger as MainLogger} from "../core/Logger"
|
|
@@ -144,6 +145,8 @@ export interface YogaInstanceOptions {
|
|
|
144
145
|
methods?: string[];
|
|
145
146
|
};
|
|
146
147
|
maxDepth?: number;
|
|
148
|
+
/** Maximum query complexity (default: 1000). 0 disables. */
|
|
149
|
+
maxComplexity?: number;
|
|
147
150
|
}
|
|
148
151
|
|
|
149
152
|
export function createYogaInstance(
|
|
@@ -152,10 +155,19 @@ export function createYogaInstance(
|
|
|
152
155
|
contextFactory?: (context: any) => any,
|
|
153
156
|
options?: YogaInstanceOptions
|
|
154
157
|
) {
|
|
155
|
-
// Prepend depth limit plugin
|
|
158
|
+
// Prepend depth limit plugin. Enforce a hard minimum so maxDepth: 0 or
|
|
159
|
+
// undefined cannot silently disable the guard (C06). If a deployment
|
|
160
|
+
// explicitly needs a higher bound, raise it — but we never allow it off.
|
|
161
|
+
const HARD_MIN_DEPTH = 15;
|
|
162
|
+
const effectiveDepth = Math.max(options?.maxDepth ?? HARD_MIN_DEPTH, HARD_MIN_DEPTH);
|
|
156
163
|
const allPlugins: Plugin[] = [];
|
|
157
|
-
|
|
158
|
-
|
|
164
|
+
allPlugins.push(useValidationRule(depthLimitRule(effectiveDepth)) as Plugin);
|
|
165
|
+
|
|
166
|
+
// Complexity budget: count per-field cost with `first`/`limit`/`take`
|
|
167
|
+
// multipliers. 0 disables, undefined defaults to 1000.
|
|
168
|
+
const complexityBudget = options?.maxComplexity ?? 1000;
|
|
169
|
+
if (complexityBudget > 0) {
|
|
170
|
+
allPlugins.push(useValidationRule(complexityLimitRule(complexityBudget)) as Plugin);
|
|
159
171
|
}
|
|
160
172
|
allPlugins.push(...plugins);
|
|
161
173
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { GraphVisitor } from "./GraphVisitor";
|
|
2
2
|
import { TypeNode, OperationNode, FieldNode, InputNode, ScalarNode } from "../graph/GraphNode";
|
|
3
3
|
import { ResolverBuilder } from "../builders/ResolverBuilder";
|
|
4
|
+
import { isSchemaInput } from "../schema";
|
|
5
|
+
import * as z from "zod";
|
|
4
6
|
import { logger } from "../../core/Logger";
|
|
5
7
|
|
|
6
8
|
/**
|
|
@@ -54,9 +56,21 @@ export class ResolverGeneratorVisitor extends GraphVisitor {
|
|
|
54
56
|
|
|
55
57
|
const type = node.operationType.charAt(0).toUpperCase() + node.operationType.slice(1).toLowerCase() as "Query" | "Mutation" | "Subscription";
|
|
56
58
|
|
|
57
|
-
// Extract Zod schema from input
|
|
59
|
+
// Extract Zod schema from input. If it's already a Zod type (`_def`),
|
|
60
|
+
// use directly. If it's a Schema DSL input (`t.` API), convert each
|
|
61
|
+
// field `.toZod()` into a Zod object so the resolver validates it
|
|
62
|
+
// instead of passing raw args through (H-GQL-4).
|
|
58
63
|
const input = node.metadata.input;
|
|
59
|
-
|
|
64
|
+
let zodSchema: any = null;
|
|
65
|
+
if (input && typeof input === 'object' && '_def' in input) {
|
|
66
|
+
zodSchema = input;
|
|
67
|
+
} else if (isSchemaInput(input)) {
|
|
68
|
+
const zodShape: Record<string, z.ZodType> = {};
|
|
69
|
+
for (const [key, field] of Object.entries(input)) {
|
|
70
|
+
zodShape[key] = (field as any).toZod();
|
|
71
|
+
}
|
|
72
|
+
zodSchema = z.object(zodShape);
|
|
73
|
+
}
|
|
60
74
|
|
|
61
75
|
// Create resolver definitions for operations
|
|
62
76
|
const resolverDef = {
|
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ import { shouldUseLateralJoins, shouldUseDirectPartition } from "../core/Config"
|
|
|
5
5
|
import { FilterBuilderRegistry } from "./FilterBuilderRegistry";
|
|
6
6
|
import { ComponentRegistry } from "../core/components";
|
|
7
7
|
import { getMetadataStorage } from "../core/metadata";
|
|
8
|
+
import { assertIdentifier } from "./SqlIdentifier";
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Check if a component property is numeric based on metadata
|
|
@@ -331,12 +332,16 @@ export class ComponentInclusionNode extends QueryNode {
|
|
|
331
332
|
const sortComponentTableName = this.getComponentTableName(typeId);
|
|
332
333
|
const nullsClause = sortOrder.nullsFirst ? 'NULLS FIRST' : 'NULLS LAST';
|
|
333
334
|
const isNumeric = isNumericProperty(sortOrder.component, sortOrder.property);
|
|
335
|
+
// Validate property name before interpolating into JSON path.
|
|
336
|
+
// Without this, a malicious or malformed sortOrder.property could
|
|
337
|
+
// inject SQL through the template (C08).
|
|
338
|
+
const safeProperty = assertIdentifier(sortOrder.property, 'sortOrder.property');
|
|
334
339
|
|
|
335
340
|
// Build scalar subquery to get sort value for each entity
|
|
336
341
|
// This avoids nested loop join by forcing row-by-row evaluation
|
|
337
342
|
const sortExpr = isNumeric
|
|
338
|
-
? `(sort_c.data->>'${
|
|
339
|
-
: `sort_c.data->>'${
|
|
343
|
+
? `(sort_c.data->>'${safeProperty}')::numeric`
|
|
344
|
+
: `sort_c.data->>'${safeProperty}'`;
|
|
340
345
|
|
|
341
346
|
const subquery = `(
|
|
342
347
|
SELECT ${sortExpr}
|
|
@@ -454,9 +459,10 @@ export class ComponentInclusionNode extends QueryNode {
|
|
|
454
459
|
|
|
455
460
|
const nullsClause = sortOrder.nullsFirst ? 'NULLS FIRST' : 'NULLS LAST';
|
|
456
461
|
const isNumeric = isNumericProperty(sortOrder.component, sortOrder.property);
|
|
462
|
+
const safeProperty = assertIdentifier(sortOrder.property, 'sortOrder.property');
|
|
457
463
|
const sortExpr = isNumeric
|
|
458
|
-
? `(c.data->>'${
|
|
459
|
-
: `c.data->>'${
|
|
464
|
+
? `(c.data->>'${safeProperty}')::numeric`
|
|
465
|
+
: `c.data->>'${safeProperty}'`;
|
|
460
466
|
|
|
461
467
|
let sql: string;
|
|
462
468
|
if (useDirectPartition) {
|
|
@@ -508,9 +514,10 @@ export class ComponentInclusionNode extends QueryNode {
|
|
|
508
514
|
|
|
509
515
|
const nullsClause = sortOrder.nullsFirst ? 'NULLS FIRST' : 'NULLS LAST';
|
|
510
516
|
const isNumeric = isNumericProperty(sortOrder.component, sortOrder.property);
|
|
517
|
+
const safeProperty = assertIdentifier(sortOrder.property, 'sortOrder.property');
|
|
511
518
|
const sortExpr = isNumeric
|
|
512
|
-
? `(sort_c.data->>'${
|
|
513
|
-
: `sort_c.data->>'${
|
|
519
|
+
? `(sort_c.data->>'${safeProperty}')::numeric`
|
|
520
|
+
: `sort_c.data->>'${safeProperty}'`;
|
|
514
521
|
|
|
515
522
|
// Use scalar subquery to avoid cartesian product explosion
|
|
516
523
|
// This forces PostgreSQL to evaluate sort value per-entity, preventing
|
package/query/OrNode.ts
CHANGED
|
@@ -305,15 +305,13 @@ export class OrNode extends QueryNode {
|
|
|
305
305
|
public execute(context: QueryContext): QueryResult {
|
|
306
306
|
// Try optimized UNION ALL path for direct partition access
|
|
307
307
|
// This avoids the slow multi-partition scanning by querying each partition directly
|
|
308
|
+
// (Verbose console.log debug traces removed — H-QUERY-2. Re-enable via
|
|
309
|
+
// a framework logger at debug level if needed.)
|
|
308
310
|
const canUseOptimized = this.canUseUnionAllOptimization() && this.dependencies.length === 0;
|
|
309
|
-
console.log(`OrNode: Using optimized path: ${canUseOptimized}, dependencies: ${this.dependencies.length}, direct partition: ${require("../core/Config").shouldUseDirectPartition()}`);
|
|
310
|
-
console.log(`OrNode: Component types:`, Array.from(this.orQuery.getComponentTypes()));
|
|
311
311
|
|
|
312
312
|
if (canUseOptimized) {
|
|
313
|
-
console.log("OrNode: Using optimized UNION path");
|
|
314
313
|
return this.executeUnionAllOptimized(context);
|
|
315
314
|
}
|
|
316
|
-
console.log("OrNode: Using fallback path");
|
|
317
315
|
|
|
318
316
|
// Fall back to original implementation for:
|
|
319
317
|
// - HASH partitioning (no direct partition access)
|
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
|
-
|
|
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
|
-
|
|
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('${
|
|
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}('${
|
|
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('${
|
|
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}('${
|
|
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('${
|
|
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}('${
|
|
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
|
-
|
|
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
|
-
//
|
|
55
|
-
const
|
|
56
|
-
|
|
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
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
await this.client.write(destKey,
|
|
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
|
}
|
package/tests/e2e/http.test.ts
CHANGED
|
@@ -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
|
-
//
|
|
80
|
-
|
|
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
|
});
|