Skip to content

Commit

Permalink
fix: beginDoomedTransactionScope
Browse files Browse the repository at this point in the history
  • Loading branch information
gxkl committed Jun 3, 2023
1 parent ae06fea commit 3550956
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class RDSClient extends Operator {
static get format() { return mysql.format; }
static get raw() { return mysql.raw; }

static #DEFAULT_STORAGE_KEY = Symbol.for('RDSClient#storage#default');
static #TRANSACTION_NEST_COUNT = Symbol.for('RDSClient#transaction#nestCount');
static #DEFAULT_STORAGE_KEY = Symbol('RDSClient#storage#default');
static #TRANSACTION_NEST_COUNT = Symbol('RDSClient#transaction#nestCount');

#pool: PoolPromisify;
#connectionStorage: AsyncLocalStorage<TransactionContext>;
Expand Down Expand Up @@ -220,16 +220,16 @@ export class RDSClient extends Operator {
const result = await scope(tran);
tran[RDSClient.#TRANSACTION_NEST_COUNT]--;

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input
.
if (tran[RDSClient.#TRANSACTION_NEST_COUNT] === 0) {
ctx._transactionConnection = null;
ctx[this.#connectionStorageKey] = null;
await tran.rollback();
}
return result;
} catch (err) {
if (ctx._transactionConnection) {
ctx._transactionConnection = null;
if (ctx[this.#connectionStorageKey]) {
ctx[this.#connectionStorageKey] = null;
await tran.rollback();
}
throw err;
} finally {
await tran.rollback();
}
}

Expand Down
56 changes: 56 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,62 @@ describe('test/client.test.ts', () => {
});
});

describe('beginDoomedTransactionScope(scope)', () => {
it('should rollback when query success', async () => {
mm.spy(RDSTransaction.prototype, 'rollback');
const inner = async () => {
return await db.beginDoomedTransactionScope(async conn => {
await conn.query(`insert into ??(name, email, gmt_create, gmt_modified)
values(?, ?, now(), now())`,
[ table, prefix + 'beginDoomedTransactionScopeCtx1', prefix + 'm@beginDoomedTransactionScope-success.com' ]);
return conn;
});
};

const [ conn, nestedConn ] = await db.beginDoomedTransactionScope(async conn => {
await conn.query(`insert into ??(name, email, gmt_create, gmt_modified)
values(?, ?, now(), now())`,
[ table, prefix + 'beginDoomedTransactionScopeCtx2', prefix + 'm@beginDoomedTransactionScope-success.com' ]);
const nestedConn = await inner();
return [ conn, nestedConn ];
});

assert.strictEqual(conn, nestedConn);
assert.strictEqual(mmSpy(RDSTransaction.prototype.rollback).called, 1);

const rows = await db.query('select * from ?? where email=? order by id',
[ table, prefix + 'm@beginDoomedTransactionScope-success.com' ]);
assert.equal(rows.length, 0);
});

it('should rollback when query fail', async () => {
mm.spy(RDSTransaction.prototype, 'rollback');
const inner = async () => {
return await db.beginDoomedTransactionScope(async conn => {
await conn.query(`insert into ??(name, email, gmt_create, gmt_modified)
valuefail(?, ?, now(), now())`,
[ table, prefix + 'beginDoomedTransactionScopeCtx1', prefix + 'm@beginDoomedTransactionScope-fail.com' ]);
});
};

await assert.rejects(
db.beginDoomedTransactionScope(async conn => {
await conn.query(`insert into ??(name, email, gmt_create, gmt_modified)
values(?, ?, now(), now())`,
[ table, prefix + 'beginDoomedTransactionScopeCtx2', prefix + 'm@beginDoomedTransactionScope-fail.com' ]);
await inner();
}),
(err: any) => err.code === 'ER_PARSE_ERROR',
);

assert.strictEqual(mmSpy(RDSTransaction.prototype.rollback).called, 1);

const rows = await db.query('select * from ?? where email=? order by id',
[ table, prefix + 'm@beginDoomedTransactionScope-fail.com' ]);
assert.equal(rows.length, 0);
});
});

describe('get(table, obj, options), select(table, options)', () => {
before(async () => {
let result = await db.insert(table, {
Expand Down

0 comments on commit 3550956

Please sign in to comment.