Skip to content

Commit 7d4b098

Browse files
authored
fix(security): sanitize timezone parameter value to prevent code injection (#2608)
* fix(security): sanitize timezone parameter value to prevent code injection. Discovered by zhaoyudi (Nebulalab)
1 parent 2efd6ab commit 7d4b098

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

lib/parsers/binary_parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ function readCodeFor(field, config, options, fieldNum) {
4242
case Types.TIMESTAMP:
4343
case Types.NEWDATE:
4444
if (helpers.typeMatch(field.columnType, dateStrings, Types)) {
45-
return `packet.readDateTimeString(${field.decimals});`;
45+
return `packet.readDateTimeString(${parseInt(field.decimals, 10)});`;
4646
}
47-
return `packet.readDateTime('${timezone}');`;
47+
return `packet.readDateTime(${helpers.srcEscape(timezone)});`;
4848
case Types.TIME:
4949
return 'packet.readTimeString()';
5050
case Types.DECIMAL:

lib/parsers/text_parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ function readCodeFor(type, charset, encodingExpr, config, options) {
4848
if (helpers.typeMatch(type, dateStrings, Types)) {
4949
return 'packet.readLengthCodedString("ascii")';
5050
}
51-
return `packet.parseDate('${timezone}')`;
51+
return `packet.parseDate(${helpers.srcEscape(timezone)})`;
5252
case Types.DATETIME:
5353
case Types.TIMESTAMP:
5454
if (helpers.typeMatch(type, dateStrings, Types)) {
5555
return 'packet.readLengthCodedString("ascii")';
5656
}
57-
return `packet.parseDateTime('${timezone}')`;
57+
return `packet.parseDateTime(${helpers.srcEscape(timezone)})`;
5858
case Types.TIME:
5959
return 'packet.readLengthCodedString("ascii")';
6060
case Types.GEOMETRY:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, test, assert } from 'poku';
2+
import { createConnection, describeOptions } from '../../../common.test.cjs';
3+
4+
const connection = createConnection().promise();
5+
6+
describe('Binary Parser: timezone Sanitization', describeOptions);
7+
8+
Promise.all([
9+
test(async () => {
10+
process.env.TEST_ENV_VALUE = 'secure';
11+
await connection.execute({
12+
sql: 'SELECT NOW()',
13+
timezone: `'); process.env.TEST_ENV_VALUE = "not so much"; //`,
14+
});
15+
16+
assert.strictEqual(
17+
process.env.TEST_ENV_VALUE,
18+
'secure',
19+
'Timezone sanitization failed - code injection possible',
20+
);
21+
}),
22+
]).then(async () => {
23+
await connection.end();
24+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, test, assert } from 'poku';
2+
import { createConnection, describeOptions } from '../../../common.test.cjs';
3+
4+
const connection = createConnection().promise();
5+
6+
describe('Text Parser: timezone Sanitization', describeOptions);
7+
8+
Promise.all([
9+
test(async () => {
10+
process.env.TEST_ENV_VALUE = 'secure';
11+
await connection.query({
12+
sql: 'SELECT NOW()',
13+
timezone: `'); process.env.TEST_ENV_VALUE = "not so much"; //`,
14+
});
15+
16+
assert.strictEqual(
17+
process.env.TEST_ENV_VALUE,
18+
'secure',
19+
'Timezone sanitization failed - code injection possible',
20+
);
21+
}),
22+
]).then(async () => {
23+
await connection.end();
24+
});

0 commit comments

Comments
 (0)