rhashimoto / wa-sqlite

WebAssembly SQLite with support for browser storage extensions
MIT License
891 stars 57 forks source link

set_authorizer callback is not called correctly #209

Closed hazelmeow closed 1 month ago

hazelmeow commented 1 month ago

It seems to me that set_authorizer's callback is not called correctly. I'm not using the function or familiar with it but I spotted this while reading the code.

In sqlite-api.js, sqlite3.set_authorizer function, adapt calls cvtArgs which returns an array of converted args. This array is passed as the first arg to the callback, when it should probably be spread instead: https://github.com/rhashimoto/wa-sqlite/blob/160e2fb6812604cdf4748f169c298f5c4cb11a99/src/sqlite-api.js#L633

The tests don't currently check what the callback was called with, but we can make them fail if we check that iActionCode was a defined value:

it('should call authorizer', async function() {
  let rc;

  const authorizations = [];
  rc = sqlite3.set_authorizer(db, (_, iActionCode, p3, p4, p5, p6) => {
    authorizations.push([iActionCode, p3, p4, p5, p6]);
    return SQLite.SQLITE_OK;
  });
  expect(rc).toEqual(SQLite.SQLITE_OK);

  rc = await sqlite3.exec(db, 'CREATE TABLE t(x)');
  expect(rc).toEqual(SQLite.SQLITE_OK);

  expect(authorizations.length).toBeGreaterThan(0);
  expect(authorizations[0][0]).toBeDefined(); // <-- added this line
});
rhashimoto commented 1 month ago

Thanks for the report! I'll take a look but might take up to a week before I can get to it.

rhashimoto commented 1 month ago

In sqlite-api.js, sqlite3.set_authorizer function, adapt calls cvtArgs which returns an array of converted args. This array is passed as the first arg to the callback, when it should probably be spread instead:

Exactly right. Fix incoming.