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

fix(security): sanitize timezone parameter value to prevent code injection #2608

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/parsers/binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ function readCodeFor(field, config, options, fieldNum) {
case Types.TIMESTAMP:
case Types.NEWDATE:
if (helpers.typeMatch(field.columnType, dateStrings, Types)) {
return `packet.readDateTimeString(${field.decimals});`;
return `packet.readDateTimeString(${parseInt(field.decimals, 10)});`;
}
return `packet.readDateTime('${timezone}');`;
return `packet.readDateTime(${helpers.srcEscape(timezone)});`;
case Types.TIME:
return 'packet.readTimeString()';
case Types.DECIMAL:
Expand Down
4 changes: 2 additions & 2 deletions lib/parsers/text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ function readCodeFor(type, charset, encodingExpr, config, options) {
if (helpers.typeMatch(type, dateStrings, Types)) {
return 'packet.readLengthCodedString("ascii")';
}
return `packet.parseDate('${timezone}')`;
return `packet.parseDate(${helpers.srcEscape(timezone)})`;
case Types.DATETIME:
case Types.TIMESTAMP:
if (helpers.typeMatch(type, dateStrings, Types)) {
return 'packet.readLengthCodedString("ascii")';
}
return `packet.parseDateTime('${timezone}')`;
return `packet.parseDateTime(${helpers.srcEscape(timezone)})`;
case Types.TIME:
return 'packet.readLengthCodedString("ascii")';
case Types.GEOMETRY:
Expand Down
24 changes: 24 additions & 0 deletions test/esm/unit/parsers/timezone-binary-sanitization.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, test, assert } from 'poku';
import { createConnection, describeOptions } from '../../../common.test.cjs';

const connection = createConnection().promise();

describe('Binary Parser: timezone Sanitization', describeOptions);

Promise.all([
test(async () => {
process.env.TEST_ENV_VALUE = 'secure';
await connection.execute({
sql: 'SELECT NOW()',
timezone: `'); process.env.TEST_ENV_VALUE = "not so much"; //`,
});

assert.strictEqual(
process.env.TEST_ENV_VALUE,
'secure',
'Timezone sanitization failed - code injection possible',
);
}),
]).then(async () => {
await connection.end();
});
24 changes: 24 additions & 0 deletions test/esm/unit/parsers/timezone-text-sanitization.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, test, assert } from 'poku';
import { createConnection, describeOptions } from '../../../common.test.cjs';

const connection = createConnection().promise();

describe('Text Parser: timezone Sanitization', describeOptions);

Promise.all([
test(async () => {
process.env.TEST_ENV_VALUE = 'secure';
await connection.query({
sql: 'SELECT NOW()',
timezone: `'); process.env.TEST_ENV_VALUE = "not so much"; //`,
});

assert.strictEqual(
process.env.TEST_ENV_VALUE,
'secure',
'Timezone sanitization failed - code injection possible',
);
}),
]).then(async () => {
await connection.end();
});
Loading