Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sqlite3_set_authorizer #563

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [Database#pragma()](#pragmastring-options---results)
- [Database#backup()](#backupdestination-options---promise)
- [Database#function()](#functionname-options-function---this)
- [Database#setAuthorizer()](#setauthorizerfn---this)
- [Database#aggregate()](#aggregatename-options---this)
- [Database#loadExtension()](#loadextensionpath-entrypoint---this)
- [Database#exec()](#execstring---this)
Expand Down Expand Up @@ -176,6 +177,27 @@ db.prepare("SELECT void()").pluck().get(); // => null
db.prepare("SELECT void(?, ?)").pluck().get(55, 19); // => null
```

### .setAuthorizer(fn) -> *this*

Register a [compile-time authorization callback](https://sqlite.org/c3ref/set_authorizer.html) function.

```js
db.setAuthorizer(function(op, a0, a1, database, trigger){
// do something with the arg
// and return the decision as either SQLITE_OK, SQLITE_DENY or SQLITE_IGNORE
// not returning any value or throwing an error will cause the driver to return SQLITE_ERROR
return SQLITE_DENY; // for fun :)
});

db.prepare("SELECT * FROM users") // this will throw SqliteError: authorization error
```

The authorizer callback is invoked as SQL statements are being compiled by `sqlite3_prepare()` or its variants. The callback function receives as argument the [action code](https://sqlite.org/c3ref/c_alter_table.html)and the related parameters, along with the current _database_ and _trigger_ name.

The callback function _must_ accept exactly 5 arguments. Any given database can only have a single authorizer function attached to it at any given point in time.

To unset a previously registered authorizer invoke `setAuthorizer` with a `null` argument.

### .aggregate(*name*, *options*) -> *this*

Registers a user-defined [aggregate function](https://sqlite.org/lang_aggfunc.html).
Expand Down
17 changes: 17 additions & 0 deletions lib/authorizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

module.exports = (setAuthorizer) => {
return function defineAuthorizer(fn) {
if(fn !== null) { // must be a valid function if not null
if (typeof fn !== 'function') {
throw new TypeError('Expected argument to be a function');
}

if(fn.length != 5) {
throw new RangeError('Authorizer function must accept exactly 5 arguments')
}
}

return setAuthorizer.call(this, fn)
}
}
2 changes: 2 additions & 0 deletions lib/codes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Codes.js - export sqlite codes / constants https://sqlite.org/c3ref/constlist.html
module.exports = require('bindings')('better_sqlite3.node').Codes
1 change: 1 addition & 0 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function Database(filenameGiven, options) {
setErrorConstructor(require('./sqlite-error'));
util.wrap(CPPDatabase, 'pragma', require('./pragma'));
util.wrap(CPPDatabase, 'function', require('./function'));
util.wrap(CPPDatabase, 'setAuthorizer', require('./authorizer'));
util.wrap(CPPDatabase, 'aggregate', require('./aggregate'));
util.wrap(CPPDatabase, 'backup', require('./backup'));
CPPDatabase.prototype.transaction = require('./transaction');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"devDependencies": {
"chai": "^4.2.0",
"chai-spies": "^1.0.0",
"cli-color": "^2.0.0",
"fs-extra": "^8.1.0",
"mocha": "^7.0.1",
Expand Down
234 changes: 175 additions & 59 deletions src/better_sqlite3.cpp

Large diffs are not rendered by default.