@kitsuden/shared 0.0.0-stage → 0.9.0-beta
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/LICENSE +21 -0
- package/README.md +65 -2
- package/dist/admin-registry-contract.d.ts +99 -0
- package/dist/admin-registry-contract.d.ts.map +1 -0
- package/dist/admin-view.d.ts +123 -0
- package/dist/admin-view.d.ts.map +1 -0
- package/dist/agent-permission.d.ts +161 -0
- package/dist/agent-permission.d.ts.map +1 -0
- package/dist/approvals.d.ts +164 -0
- package/dist/approvals.d.ts.map +1 -0
- package/dist/automation.d.ts +175 -0
- package/dist/automation.d.ts.map +1 -0
- package/dist/block-keys.d.ts +82 -0
- package/dist/block-keys.d.ts.map +1 -0
- package/dist/code-scan.d.ts +64 -0
- package/dist/code-scan.d.ts.map +1 -0
- package/dist/comment-subjects.d.ts +129 -0
- package/dist/comment-subjects.d.ts.map +1 -0
- package/dist/connections.d.ts +96 -0
- package/dist/connections.d.ts.map +1 -0
- package/dist/constants.d.ts +37 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/content-publishing.d.ts +34 -0
- package/dist/content-publishing.d.ts.map +1 -0
- package/dist/contracts.d.ts +87 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/dataset-catalog.d.ts +91 -0
- package/dist/dataset-catalog.d.ts.map +1 -0
- package/dist/deck-analysis.d.ts +63 -0
- package/dist/deck-analysis.d.ts.map +1 -0
- package/dist/env.d.ts +2 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/errors.d.ts +275 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/field-types.d.ts +304 -0
- package/dist/field-types.d.ts.map +1 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7785 -0
- package/dist/kon-api.d.ts +914 -0
- package/dist/kon-api.d.ts.map +1 -0
- package/dist/kon-entities.d.ts +204 -0
- package/dist/kon-entities.d.ts.map +1 -0
- package/dist/kon-events.d.ts +103 -0
- package/dist/kon-events.d.ts.map +1 -0
- package/dist/kon-package.d.ts +124 -0
- package/dist/kon-package.d.ts.map +1 -0
- package/dist/link-relations.d.ts +73 -0
- package/dist/link-relations.d.ts.map +1 -0
- package/dist/meta-levels.d.ts +81 -0
- package/dist/meta-levels.d.ts.map +1 -0
- package/dist/models-sql.d.ts +110 -0
- package/dist/models-sql.d.ts.map +1 -0
- package/dist/models.d.ts +354 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/operations.d.ts +428 -0
- package/dist/operations.d.ts.map +1 -0
- package/dist/promotion.d.ts +244 -0
- package/dist/promotion.d.ts.map +1 -0
- package/dist/public-site.d.ts +106 -0
- package/dist/public-site.d.ts.map +1 -0
- package/dist/render-blocks.d.ts +150 -0
- package/dist/render-blocks.d.ts.map +1 -0
- package/dist/reports.d.ts +109 -0
- package/dist/reports.d.ts.map +1 -0
- package/dist/rls-builder.d.ts +136 -0
- package/dist/rls-builder.d.ts.map +1 -0
- package/dist/safe-pattern.d.ts +47 -0
- package/dist/safe-pattern.d.ts.map +1 -0
- package/dist/schemas.d.ts +977 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/sensitivity.d.ts +37 -0
- package/dist/sensitivity.d.ts.map +1 -0
- package/dist/signer.d.ts +67 -0
- package/dist/signer.d.ts.map +1 -0
- package/dist/tier-ranges.d.ts +37 -0
- package/dist/tier-ranges.d.ts.map +1 -0
- package/dist/types.d.ts +261 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +71 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/wiki-links.d.ts +56 -0
- package/dist/wiki-links.d.ts.map +1 -0
- package/package.json +53 -3
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ============================================================================
|
|
3
|
+
* RLS QUERY BUILDER - Translator of JSON Rules to Drizzle ORM Conditions
|
|
4
|
+
* ============================================================================
|
|
5
|
+
*
|
|
6
|
+
* RESCUE PHASE: RAM Optimization and Application RLS
|
|
7
|
+
*
|
|
8
|
+
* This module eliminates the antipattern of filtering data in memory (.filter())
|
|
9
|
+
* and translates JSON permission rules into native SQL conditions using
|
|
10
|
+
* Drizzle ORM's operators.
|
|
11
|
+
*
|
|
12
|
+
* Before (RAM-killer):
|
|
13
|
+
* const all = await db.select().from(table); // 100,000 rows
|
|
14
|
+
* const filtered = all.filter(createRLSFilter(rules, user)); // In memory
|
|
15
|
+
*
|
|
16
|
+
* After (Drizzle Application RLS):
|
|
17
|
+
* const whereCondition = buildDrizzleWhere(rules, user);
|
|
18
|
+
* const filtered = await db.select().from(table).where(whereCondition); // 5 rows
|
|
19
|
+
*
|
|
20
|
+
* The business logic stays the same (application-level RLS), but
|
|
21
|
+
* the heavy filtering work moves to PostgreSQL.
|
|
22
|
+
*/
|
|
23
|
+
import { SQL } from "drizzle-orm";
|
|
24
|
+
export type RLSType = "admin" | "public" | "authenticated";
|
|
25
|
+
export type RLSRule = {
|
|
26
|
+
field: string;
|
|
27
|
+
operator: "eq" | "neq" | "contains" | "gt" | "lt";
|
|
28
|
+
value: string;
|
|
29
|
+
};
|
|
30
|
+
export type PolicyJson = {
|
|
31
|
+
readAccess: RLSType;
|
|
32
|
+
writeAccess: RLSType;
|
|
33
|
+
enableRLS: boolean;
|
|
34
|
+
rlsRules?: RLSRule[];
|
|
35
|
+
};
|
|
36
|
+
export type RLSUser = {
|
|
37
|
+
id: string;
|
|
38
|
+
role?: string;
|
|
39
|
+
} | null;
|
|
40
|
+
/**
|
|
41
|
+
* Builds a Drizzle ORM WHERE condition from JSON RLS rules.
|
|
42
|
+
*
|
|
43
|
+
* This function translates rules like:
|
|
44
|
+
* `{ field: "author_id", operator: "eq", value: "__USER_ID__" }`
|
|
45
|
+
*
|
|
46
|
+
* Into SQL conditions like:
|
|
47
|
+
* `data->>'author_id' = 'user-123'`
|
|
48
|
+
*
|
|
49
|
+
* @param rules - Array of RLS rules from policyJson.rlsRules
|
|
50
|
+
* @param user - Authenticated user (to replace __USER_ID__)
|
|
51
|
+
* @returns Drizzle SQL condition or undefined if there are no filters
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const rules = [
|
|
56
|
+
* { field: "author_id", operator: "eq", value: "__USER_ID__" },
|
|
57
|
+
* { field: "status", operator: "eq", value: "published" }
|
|
58
|
+
* ];
|
|
59
|
+
*
|
|
60
|
+
* const whereCondition = buildDrizzleWhere(rules, { id: "user-123" });
|
|
61
|
+
* // Result: AND(data->>'author_id' = 'user-123', data->>'status' = 'published')
|
|
62
|
+
*
|
|
63
|
+
* const results = await db
|
|
64
|
+
* .select()
|
|
65
|
+
* .from(dataEntries)
|
|
66
|
+
* .where(whereCondition);
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare function buildDrizzleWhere(rules: RLSRule[] | undefined, user: RLSUser): SQL | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Builds a WHERE condition that includes filtering by datasetId + RLS.
|
|
72
|
+
*
|
|
73
|
+
* Useful for endpoints that need to filter by dataset and apply RLS
|
|
74
|
+
* in a single query.
|
|
75
|
+
*
|
|
76
|
+
* @param datasetId - ID of the dataset to filter by
|
|
77
|
+
* @param rules - Optional RLS rules
|
|
78
|
+
* @param user - Authenticated user
|
|
79
|
+
* @param tableDataColumn - Name of the JSONB column (default: "data")
|
|
80
|
+
* @returns Object with separate or combined conditions
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const { where, hasRLS } = buildDatasetWhere(
|
|
85
|
+
* "dataset-123",
|
|
86
|
+
* policy.rlsRules,
|
|
87
|
+
* currentUser,
|
|
88
|
+
* dataEntries
|
|
89
|
+
* );
|
|
90
|
+
*
|
|
91
|
+
* const results = await db
|
|
92
|
+
* .select()
|
|
93
|
+
* .from(dataEntries)
|
|
94
|
+
* .where(where);
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export declare function buildDatasetWhere(datasetId: string, rules: RLSRule[] | undefined, user: RLSUser, dataColumn?: string): {
|
|
98
|
+
where: SQL | undefined;
|
|
99
|
+
hasRLS: boolean;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* The entries of a dataset that someone outside the team may see: the published
|
|
103
|
+
* ones and their own (they own them or created them; an agent's rows are owned
|
|
104
|
+
* by its sponsor, and `created_by_id` is the agent). No id: only the published.
|
|
105
|
+
*
|
|
106
|
+
* THE one place for this rule: every path that reads entries for a non-staff
|
|
107
|
+
* caller goes through it. `alias` qualifies the columns in a query with a join.
|
|
108
|
+
*/
|
|
109
|
+
export declare function publishedOrOwn(userId: string | null | undefined, alias?: string): SQL;
|
|
110
|
+
/** `publishedOrOwn` as a Kitsuden QL filter, for what goes through the query translator. */
|
|
111
|
+
export declare function publishedOrOwnQl(userId: string | null | undefined): Record<string, unknown>;
|
|
112
|
+
/**
|
|
113
|
+
* Checks whether the user has read access based on policyJson.
|
|
114
|
+
* Synchronous version that does not require a database query.
|
|
115
|
+
*
|
|
116
|
+
* @param policy - Policy configuration
|
|
117
|
+
* @param user - Authenticated user
|
|
118
|
+
* @returns true if they have access
|
|
119
|
+
*/
|
|
120
|
+
export declare function checkReadAccess(policy: PolicyJson | null, user: RLSUser): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Checks whether the user has write access based on policyJson.
|
|
123
|
+
*
|
|
124
|
+
* @param policy - Policy configuration
|
|
125
|
+
* @param user - Authenticated user
|
|
126
|
+
* @returns true if they have access
|
|
127
|
+
*/
|
|
128
|
+
export declare function checkWriteAccess(policy: PolicyJson | null, user: RLSUser): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* @deprecated Use buildDrizzleWhere() instead.
|
|
131
|
+
*
|
|
132
|
+
* Creates an in-memory filter function (antipattern).
|
|
133
|
+
* Kept only for compatibility with legacy code.
|
|
134
|
+
*/
|
|
135
|
+
export declare function createRLSFilter(policy: PolicyJson | null, user: RLSUser): ((entry: Record<string, any>) => boolean) | null;
|
|
136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BAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,GAAG,CAKrF;AAED,4FAA4F;AAC5F,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAK3F;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAkBjF;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAelF;AA2DD;;;;;GAKG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,UAAU,GAAG,IAAI,EACzB,IAAI,EAAE,OAAO,GACZ,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,OAAO,CAAC,GAAG,IAAI,CA+BlD"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation patterns written by an editor, without ReDoS
|
|
3
|
+
* ============================================================================
|
|
4
|
+
*
|
|
5
|
+
* A dataset field can declare `validation.pattern` / `validationRegex`,
|
|
6
|
+
* and it was compiled with `new RegExp(...)` as is to test every value that
|
|
7
|
+
* comes in (entry creation, CSV import). A catastrophic pattern such as
|
|
8
|
+
* `^(a+)+$` against `"aaaa…!"` hangs the API's event loop: with
|
|
9
|
+
* Bun that is the entire API (earlier audit).
|
|
10
|
+
*
|
|
11
|
+
* The first defence was a regular expression over the pattern's text, and it
|
|
12
|
+
* did not see through nested parentheses (`^((a+))+$`) nor polynomial
|
|
13
|
+
* patterns without groups (`^a*a*a*a*a*b$`) (security review F4). JS has no
|
|
14
|
+
* linear engine (re2) without a native dependency, so here the pattern is
|
|
15
|
+
* really PARSED (escapes, classes, nested groups) and:
|
|
16
|
+
*
|
|
17
|
+
* · the pattern has a length cap;
|
|
18
|
+
* · backreferences are rejected;
|
|
19
|
+
* · every repeated group (`+`, `*`, `{n,}`, `{n,m}` with m > 1) that holds
|
|
20
|
+
* something of variable length is rejected: another non-fixed quantifier
|
|
21
|
+
* or an alternation (`(a+)+`, `((a+))+`, `(a|aa)*`, `(\w*)*`, `(a?){20}`);
|
|
22
|
+
* · the DEGREE of polynomial backtracking is measured: how many quantifiers
|
|
23
|
+
* can share the same characters in a chain (`a*a*`, `.*x.*`, the implicit
|
|
24
|
+
* `.*?` of a pattern without `^`). Degree n costs ~length^n steps when the
|
|
25
|
+
* match fails; with the value cap, up to degree 2 is accepted;
|
|
26
|
+
* · the value being tested has a length cap (`testPattern`, and the zod
|
|
27
|
+
* validators apply it with `.max(MAX_LENGTH_TO_TEST)`).
|
|
28
|
+
*
|
|
29
|
+
* The analysis is conservative: when in doubt, it rejects. A rejected pattern
|
|
30
|
+
* is treated as "no pattern": it never takes the request down.
|
|
31
|
+
*/
|
|
32
|
+
export declare const MAX_PATTERN_LENGTH = 256;
|
|
33
|
+
/**
|
|
34
|
+
* With degree 2 the worst case is ~length² steps: 4096² ≈ 1.7·10⁷, tens of
|
|
35
|
+
* milliseconds. With 10 000 it was 10⁸.
|
|
36
|
+
*/
|
|
37
|
+
export declare const MAX_LENGTH_TO_TEST = 4096;
|
|
38
|
+
/** The compiled pattern, or `null` if it is invalid or risky. */
|
|
39
|
+
export declare function safePattern(pattern: unknown, flags?: string): RegExp | null;
|
|
40
|
+
/**
|
|
41
|
+
* Does `value` match the pattern? `true` if the pattern is not usable (it is ignored) and
|
|
42
|
+
* `false` if the value is longer than what is tested.
|
|
43
|
+
*/
|
|
44
|
+
export declare function testPattern(pattern: unknown, value: string): boolean;
|
|
45
|
+
/** Can this pattern (already compiled without error) backtrack out of control? */
|
|
46
|
+
export declare function riskyPattern(pattern: string, flags?: string): boolean;
|
|
47
|
+
//# sourceMappingURL=safe-pattern.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safe-pattern.d.ts","sourceRoot":"","sources":["../src/safe-pattern.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,eAAO,MAAM,kBAAkB,MAAM,CAAC;AACtC;;;GAGG;AACH,eAAO,MAAM,kBAAkB,OAAQ,CAAC;AAIxC,iEAAiE;AACjE,wBAAgB,WAAW,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAU3E;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAKpE;AAED,kFAAkF;AAClF,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,SAAK,GAAG,OAAO,CAUjE"}
|