@deepagents/text2sql 7.13.0 → 7.14.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/README.md +106 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/adapters/bigquery/index.js +24 -5
- package/dist/lib/adapters/bigquery/index.js.map +2 -2
- package/dist/lib/adapters/clickhouse/index.js.map +1 -1
- package/dist/lib/adapters/duckdb/index.js.map +1 -1
- package/dist/lib/adapters/mysql/index.js +24 -5
- package/dist/lib/adapters/mysql/index.js.map +2 -2
- package/dist/lib/adapters/policy/in-scope.d.ts.map +1 -1
- package/dist/lib/adapters/policy/index.js +24 -5
- package/dist/lib/adapters/policy/index.js.map +2 -2
- package/dist/lib/adapters/policy/source-policy.d.ts +6 -0
- package/dist/lib/adapters/policy/source-policy.d.ts.map +1 -1
- package/dist/lib/adapters/postgres/index.js +24 -5
- package/dist/lib/adapters/postgres/index.js.map +2 -2
- package/dist/lib/adapters/posthog/index.js.map +1 -1
- package/dist/lib/adapters/spreadsheet/index.js +24 -5
- package/dist/lib/adapters/spreadsheet/index.js.map +2 -2
- package/dist/lib/adapters/sqlite/index.js +24 -5
- package/dist/lib/adapters/sqlite/index.js.map +2 -2
- package/dist/lib/adapters/sqlserver/index.d.ts +1 -0
- package/dist/lib/adapters/sqlserver/index.d.ts.map +1 -1
- package/dist/lib/adapters/sqlserver/index.js +130 -5
- package/dist/lib/adapters/sqlserver/index.js.map +4 -4
- package/dist/lib/adapters/sqlserver/sqlserver-plan.d.ts +66 -0
- package/dist/lib/adapters/sqlserver/sqlserver-plan.d.ts.map +1 -0
- package/dist/lib/sql-scope-error.d.ts +12 -1
- package/dist/lib/sql-scope-error.d.ts.map +1 -1
- package/dist/lib/synthesis/index.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -391,7 +391,112 @@ const adapter = new sqlserver.SqlServer({
|
|
|
391
391
|
```
|
|
392
392
|
|
|
393
393
|
A source that cannot read the SQL throws `UnreadableSql`, and the policy gives
|
|
394
|
-
`SQL_SCOPE_PARSE_ERROR`. Any other error of a source rejects the check.
|
|
394
|
+
`SQL_SCOPE_PARSE_ERROR`. Any other error of a source rejects the check. A
|
|
395
|
+
source of one entry point, such as `SqlServerPlan` of `./sqlserver`, works in
|
|
396
|
+
a `SourcePolicy` of `./policy`.
|
|
397
|
+
|
|
398
|
+
### SQL Server's own plan
|
|
399
|
+
|
|
400
|
+
`textPolicy()` reads T-SQL with node-sql-parser, whose grammar refuses valid
|
|
401
|
+
T-SQL such as `STRING_AGG ... WITHIN GROUP`, `TRY_CAST`,
|
|
402
|
+
`COUNT_BIG`, `OPENJSON ... WITH`, `OFFSET` without `FETCH`, `OPTION`,
|
|
403
|
+
`LIKE ... ESCAPE` and `TOP ... WITH TIES`. `sqlserver.planPolicy(principal)`
|
|
404
|
+
asks SQL Server instead: a database user, the principal, compiles the SQL
|
|
405
|
+
under `SET SHOWPLAN_XML ON`, which plans each statement and runs none.
|
|
406
|
+
|
|
407
|
+
| Rule | Reads | Refuses |
|
|
408
|
+
| ------------------- | --------------- | ---------------------------------------------------------------- |
|
|
409
|
+
| `GrantedScope` | `Grants` | SQL that SQL Server refuses for the principal, as `OUT_OF_SCOPE` |
|
|
410
|
+
| `OneSelect` | `Statements` | a plan that is not exactly one SELECT |
|
|
411
|
+
| `NoRemoteOperators` | `PlanOperators` | a plan with a `Remote` operator, which reads a linked server |
|
|
412
|
+
|
|
413
|
+
A permission error of the compile on an object (229) is `OUT_OF_SCOPE`, and
|
|
414
|
+
its `error` is SQL Server's message, such as "The INSERT permission was denied
|
|
415
|
+
on the object 'orders' ...". SQL Server compiles DDL such as `DROP TABLE`
|
|
416
|
+
without a permission check, so DDL is a statement that is not a SELECT. Any
|
|
417
|
+
other error of the compile is `SQL_SCOPE_PARSE_ERROR` with SQL Server's
|
|
418
|
+
message: 102 for `LIMIT`, 208 for an unknown table and 262 for a principal
|
|
419
|
+
without `SHOWPLAN`.
|
|
420
|
+
|
|
421
|
+
The host creates the principal in its database: a user with `SELECT` on the
|
|
422
|
+
tables and views that the model may read, and `SHOWPLAN`. The grants decide the
|
|
423
|
+
scope, so `planPolicy` reads no grounding.
|
|
424
|
+
|
|
425
|
+
```sql
|
|
426
|
+
CREATE USER analyst WITHOUT LOGIN WITH DEFAULT_SCHEMA = marts;
|
|
427
|
+
GRANT SELECT ON SCHEMA::marts TO analyst;
|
|
428
|
+
GRANT SHOWPLAN TO analyst;
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
The principal gives its name and a session on a connection of its own. Each
|
|
432
|
+
check opens a session, runs `EXECUTE AS USER = N'<name>' WITH NO REVERT` and
|
|
433
|
+
`SET SHOWPLAN_XML ON` as batches of their own, compiles the SQL, and disposes
|
|
434
|
+
the session, so no SQL of one check reaches the next. The login of the session
|
|
435
|
+
needs `IMPERSONATE` on the user, which the database owner has.
|
|
436
|
+
|
|
437
|
+
```ts
|
|
438
|
+
import sql from 'mssql';
|
|
439
|
+
|
|
440
|
+
import * as sqlserver from '@deepagents/text2sql/sqlserver';
|
|
441
|
+
|
|
442
|
+
const principal: sqlserver.SqlServerPrincipal = {
|
|
443
|
+
name: 'analyst',
|
|
444
|
+
session: async () => {
|
|
445
|
+
const pool = await new sql.ConnectionPool({
|
|
446
|
+
...config,
|
|
447
|
+
pool: { max: 1, min: 1 },
|
|
448
|
+
}).connect();
|
|
449
|
+
return {
|
|
450
|
+
batch: async (text) =>
|
|
451
|
+
(await pool.request().batch(text)).recordsets.flat(),
|
|
452
|
+
[Symbol.asyncDispose]: () => pool.close(),
|
|
453
|
+
};
|
|
454
|
+
},
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
const adapter = new sqlserver.SqlServer({
|
|
458
|
+
execute: async (text) => {
|
|
459
|
+
await using session = await principal.session();
|
|
460
|
+
await session.batch(`EXECUTE AS USER = N'analyst' WITH NO REVERT`);
|
|
461
|
+
return [...(await session.batch(text))];
|
|
462
|
+
},
|
|
463
|
+
grounding: [sqlserver.tables()],
|
|
464
|
+
policy: sqlserver.planPolicy(principal),
|
|
465
|
+
});
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
`execute` must run the SQL as the same principal. SQL Server resolves a name
|
|
469
|
+
such as `users` in the default schema of the user that compiles it, so a check
|
|
470
|
+
as `analyst` and a run as another user can read two different tables.
|
|
471
|
+
|
|
472
|
+
A lock hint such as `WITH (UPDLOCK)` leaves the plan the same, so `planPolicy`
|
|
473
|
+
passes lock hints. To refuse them, compose the plan with the parser:
|
|
474
|
+
|
|
475
|
+
```ts
|
|
476
|
+
import {
|
|
477
|
+
FirstViolation,
|
|
478
|
+
Forbid,
|
|
479
|
+
SourcePolicy,
|
|
480
|
+
SqlText,
|
|
481
|
+
} from '@deepagents/text2sql/policy';
|
|
482
|
+
|
|
483
|
+
const policy = new FirstViolation(
|
|
484
|
+
new SourcePolicy(new SqlText({ dialects: ['transactsql'] }), [
|
|
485
|
+
new Forbid(['table-hint']),
|
|
486
|
+
]),
|
|
487
|
+
sqlserver.planPolicy(principal),
|
|
488
|
+
);
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
This composition also refuses all T-SQL that node-sql-parser cannot read,
|
|
492
|
+
because the parser reads first: `STRING_AGG ... WITHIN GROUP` is then
|
|
493
|
+
`SQL_SCOPE_PARSE_ERROR`.
|
|
494
|
+
|
|
495
|
+
`SqlServerPlan` gives `Statements`, `Grants` and `PlanOperators`. It gives no
|
|
496
|
+
`Relations`, because a plan names the base tables after views and functions
|
|
497
|
+
are expanded, and no `Constructs`, so `InScope` and `Forbid` do not compile
|
|
498
|
+
over it. A policy over `SqlServerPlan` without `GrantedScope` rejects the
|
|
499
|
+
check when SQL Server refuses the compile.
|
|
395
500
|
|
|
396
501
|
## Fragments
|
|
397
502
|
|