@fabrika/db 0.9.0-dev.12 → 0.9.0-dev.13

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/src/registry.ts CHANGED
@@ -17,6 +17,15 @@ import type { StoredFiles } from './stored-files'
17
17
  */
18
18
  export type Locator = (dimension: string, id: string, using?: Session) => Promise<ReadonlyMap<string, string> | null>
19
19
 
20
+ /** Whether an active row of `relation`'s source names `principal` at `node`: what a Ref held through it is minted on. */
21
+ export type RelationCheck = (relation: string, node: string, principal: string) => Promise<boolean>
22
+
23
+ /**
24
+ * Whether a row `relation` reaches for `principal` reveals `node` of `dimension` now: what a Ref held through a reveal
25
+ * is minted on.
26
+ */
27
+ export type RevealCheck = (relation: string, dimension: string, node: string, principal: string) => Promise<boolean>
28
+
20
29
  /** What the framework writes itself: its own `fabrika_*` tables. Usable once bound. */
21
30
  export interface FrameworkTables {
22
31
  readonly webhooks: WebhookTables
@@ -32,6 +41,8 @@ export interface FrameworkTables {
32
41
  /** A connection of the layer and its locator. Only its holder can `release` it, which lets the layer be bound again. */
33
42
  export interface ProvisionalBinding {
34
43
  readonly locate: Locator
44
+ readonly relates: RelationCheck
45
+ readonly reveals: RevealCheck
35
46
  release(): void
36
47
  }
37
48
 
package/src/sql.ts CHANGED
@@ -238,6 +238,18 @@ function sqliteNameRefusal(tokens: readonly SqlToken[]): { readonly name: string
238
238
  return undefined
239
239
  }
240
240
 
241
+ /**
242
+ * The name of a table whose rows hand out access anywhere in a statement of a writing site, in any spelling: only the
243
+ * data layer's insert bounds the delegation, or generates the key a grant names. No statement kind is trusted to be a read (SQLite's `WITH … REPLACE`).
244
+ */
245
+ function sourceReference(tokens: readonly SqlToken[], sources: readonly string[]): { readonly name: string; readonly line: number } | undefined {
246
+ for (const token of tokens) {
247
+ const text = token.kind === 'word' || token.kind === 'quoted' || token.kind === 'string' ? token.text : undefined
248
+ if (text !== undefined && sources.includes(text.toLowerCase())) return { name: text, line: token.line }
249
+ }
250
+ return undefined
251
+ }
252
+
241
253
  /**
242
254
  * The SQL `parts` make, with a bound placeholder for each value, or a `QueryRefusedError` that names what is refused.
243
255
  * On both dialects: more than one statement, any statement but a query (and `INSERT`/`UPDATE`/`DELETE` for
@@ -246,7 +258,7 @@ function sqliteNameRefusal(tokens: readonly SqlToken[]): { readonly name: string
246
258
  * name outside `schema` and a function outside the reviewed built-ins; on SQLite also a `sqlite_*` name, and a
247
259
  * parameter the text writes itself. workerd's authorizer bounds SQLite's functions, PRAGMAs and DDL.
248
260
  */
249
- export function classifySql(parts: readonly string[], mode: SqlMode, database: SqlDatabase): string {
261
+ export function classifySql(parts: readonly string[], mode: SqlMode, database: SqlDatabase, sources: readonly string[] = []): string {
250
262
  const all = placedTokens(parts, database)
251
263
  const statements = statementsOf(all)
252
264
  const [statement] = statements
@@ -271,6 +283,13 @@ export function classifySql(parts: readonly string[], mode: SqlMode, database: S
271
283
  if (writes !== undefined) {
272
284
  refuse('sql_read_only', `sql: 'read' runs queries only, and the statement holds ${writes}; declare sql: 'write' on a mutation, a job or a webhook`)
273
285
  }
286
+ const source = mode === 'write' ? sourceReference(tokens, sources) : undefined
287
+ if (source !== undefined) {
288
+ refuse(
289
+ 'sql_name',
290
+ `'${source.name}' at line ${source.line} hands out access, so a statement that writes never names it: write its rows through ctx.db`,
291
+ )
292
+ }
274
293
  const framework = frameworkReference(tokens)
275
294
  if (framework !== undefined) {
276
295
  refuse('sql_name', `'${framework.name}' at line ${framework.line} is the framework's; ctx.sql reaches only the application's own tables`)
@@ -566,7 +585,7 @@ function scopeOptions(options: unknown): { readonly alias: string | undefined; r
566
585
  * `ctx.sql` of one site. Every call first checks that the work running now is the site's invocation, then classifies
567
586
  * the text, then binds the values; only then does it reach the session, so a refused statement sends nothing.
568
587
  */
569
- export function sqlTag(site: SqlSite, database: SqlDatabase, termsOf: ScopeOf): SqlTag {
588
+ export function sqlTag(site: SqlSite, database: SqlDatabase, termsOf: ScopeOf, sources: readonly string[]): SqlTag {
570
589
  const tag = <S extends z.core.$ZodType>(row: S) => {
571
590
  site.check()
572
591
  if (!(row instanceof z.core.$ZodType)) {
@@ -577,7 +596,7 @@ export function sqlTag(site: SqlSite, database: SqlDatabase, termsOf: ScopeOf):
577
596
  site.check()
578
597
  return failing(site, async () => {
579
598
  const expanded = expandScopes(site, templateParts(text), values, database)
580
- const statement = classifySql(expanded.parts, site.mode, database)
599
+ const statement = classifySql(expanded.parts, site.mode, database, sources)
581
600
  const parameters = expanded.values.map((value, index) => parameterOf(value, index + 1, database))
582
601
  const session = await site.session()
583
602
  if (session.readOnly && site.mode === 'write') {