-
Notifications
You must be signed in to change notification settings - Fork 5k
sql(mysql): reject out-of-range binary TIME fields instead of wrapping #34706
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
Open
robobun
wants to merge
2
commits into
main
Choose a base branch
from
farm/131c1b4c/mysql-time-binary-range-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Fault-injection test: requires a server that refuses / drops / sends malformed | ||
| // frames, which a healthy container will not do on demand. DO NOT COPY THIS | ||
| // PATTERN — anything a real server can produce belongs in describeWithContainer. | ||
| // All wire-protocol bytes come from test/js/sql/wire-frames.ts; do not inline | ||
| // Buffer.alloc frame construction here. | ||
| // | ||
| // Regression: the binary TIME decoder read the 4-byte `days` field as a raw u32 | ||
| // with no range check and computed total_hours = hours + days*24 in u32. MySQL | ||
| // TIME is bounded to +/-838:59:59 (days <= 34), so a hostile or buggy server | ||
| // sending days=178956971 made days*24 wrap past 2^32 to 8 and Bun returned the | ||
| // string "08:05:06" with no error. With the fix the decoder rejects any TIME | ||
| // field outside its documented range with ERR_MYSQL_INVALID_BINARY_VALUE. | ||
|
|
||
| import { SQL } from "bun"; | ||
| import { expect, test } from "bun:test"; | ||
| import { | ||
| listeningServer, | ||
| mysqlColumnDefinition, | ||
| mysqlHandshakeV10, | ||
| mysqlLenencInt, | ||
| mysqlOkPacket, | ||
| mysqlRawPacket, | ||
| mysqlReadPackets, | ||
| mysqlStmtPrepareOk, | ||
| } from "./wire-frames"; | ||
|
|
||
| const COM_STMT_PREPARE = 0x16; | ||
| const COM_STMT_EXECUTE = 0x17; | ||
| const COM_STMT_CLOSE = 0x19; | ||
| const COM_QUIT = 0x01; | ||
| const MYSQL_TYPE_TIME = 0x0b; | ||
|
|
||
| // MySQL binary-protocol TIME cell (page_protocol_binary_resultset.html): | ||
| // Int<1>(length = 8 | 12) Int<1>(is_negative) Int<4>(days) Int<1>(hours) | ||
| // Int<1>(minutes) Int<1>(seconds) [Int<4>(microseconds)] | ||
| // mysqlRawPacket is the low-level escape hatch because this is the malformed | ||
| // payload under test; every other frame goes through a typed helper. | ||
| function binaryTimeRow(seq: number, t: { days: number; hours: number; minutes: number; seconds: number }): Buffer { | ||
| const cell = Buffer.alloc(9); | ||
| cell[0] = 8; | ||
| cell[1] = 0; // is_negative | ||
| cell.writeUInt32LE(t.days >>> 0, 2); | ||
| cell[6] = t.hours; | ||
| cell[7] = t.minutes; | ||
| cell[8] = t.seconds; | ||
| // Binary Resultset Row: Int<1>(0x00 header) null_bitmap[(cols+7+2)/8] values | ||
| return mysqlRawPacket(seq, Buffer.concat([Buffer.from([0x00, 0x00]), cell])); | ||
| } | ||
|
|
||
| async function serveOneBinaryTime(t: { days: number; hours: number; minutes: number; seconds: number }) { | ||
| let sawExecute = false; | ||
| const { server, port } = await listeningServer(socket => { | ||
| let buffered = Buffer.alloc(0); | ||
| let authed = false; | ||
| socket.write(mysqlHandshakeV10()); | ||
| socket.on("data", chunk => { | ||
| buffered = mysqlReadPackets(Buffer.concat([buffered, chunk]), (seq, payload) => { | ||
| if (!authed) { | ||
| authed = true; | ||
| socket.write(mysqlOkPacket(seq + 1)); | ||
| return; | ||
| } | ||
| const cmd = payload[0]; | ||
| if (cmd === COM_STMT_PREPARE) { | ||
| socket.write( | ||
| Buffer.concat([ | ||
| mysqlStmtPrepareOk(1, 1, 1, 1), | ||
| mysqlColumnDefinition(2, { name: "p", type: MYSQL_TYPE_TIME }), | ||
| mysqlColumnDefinition(3, { name: "t", type: MYSQL_TYPE_TIME }), | ||
| ]), | ||
| ); | ||
| } else if (cmd === COM_STMT_EXECUTE) { | ||
| sawExecute = true; | ||
| socket.write( | ||
| Buffer.concat([ | ||
| mysqlRawPacket(1, mysqlLenencInt(1)), | ||
| mysqlColumnDefinition(2, { name: "t", type: MYSQL_TYPE_TIME }), | ||
| binaryTimeRow(3, t), | ||
| mysqlOkPacket(4, 0xfe), | ||
| ]), | ||
| ); | ||
| } else if (cmd !== COM_STMT_CLOSE && cmd !== COM_QUIT) { | ||
| socket.write(mysqlOkPacket(1)); | ||
| } | ||
| }); | ||
| }); | ||
| socket.on("error", () => {}); | ||
| }); | ||
| return { server, port, sawExecute: () => sawExecute }; | ||
| } | ||
|
|
||
| test.each([ | ||
| // 178956971 * 24 = 2^32 + 8: pre-fix this wrapped to total_hours=8 -> "08:05:06". | ||
| ["days wraps u32*24", { days: 178956971, hours: 0, minutes: 5, seconds: 6 }], | ||
| ["days = u32::MAX", { days: 0xffff_ffff, hours: 0, minutes: 5, seconds: 6 }], | ||
| ["days just over the 34-day bound", { days: 35, hours: 0, minutes: 0, seconds: 0 }], | ||
| ["days=34 hours=23 (839h, past the 838h cap)", { days: 34, hours: 23, minutes: 0, seconds: 0 }], | ||
| ["hours > 23", { days: 0, hours: 24, minutes: 0, seconds: 0 }], | ||
| ["minutes > 59", { days: 0, hours: 0, minutes: 60, seconds: 0 }], | ||
| ["seconds > 59", { days: 0, hours: 0, minutes: 0, seconds: 60 }], | ||
| ] as const)("MySQL: binary TIME with %s is rejected, not silently wrapped", async (_label, t) => { | ||
| const { server, port, sawExecute } = await serveOneBinaryTime(t); | ||
| try { | ||
| await using sql = new SQL({ url: `mysql://root@127.0.0.1:${port}/db`, max: 1 }); | ||
| const outcome = await sql`select ${1} as t`.values().then( | ||
| rows => ({ ok: true as const, rows }), | ||
| e => ({ ok: false as const, code: e?.code ?? String(e) }), | ||
| ); | ||
|
|
||
| // Pre-fix: outcome is { ok: true, rows: [["08:05:06"]] } (or similar garbage). | ||
| expect({ outcome, sawExecute: sawExecute() }).toEqual({ | ||
| outcome: { ok: false, code: "ERR_MYSQL_INVALID_BINARY_VALUE" }, | ||
| sawExecute: true, | ||
| }); | ||
| } finally { | ||
| await new Promise<void>(r => server.close(() => r())); | ||
| } | ||
| }); | ||
|
|
||
| test("MySQL: binary TIME at the documented maximum (838:59:59) still decodes", async () => { | ||
| const { server, port, sawExecute } = await serveOneBinaryTime({ days: 34, hours: 22, minutes: 59, seconds: 59 }); | ||
| try { | ||
| await using sql = new SQL({ url: `mysql://root@127.0.0.1:${port}/db`, max: 1 }); | ||
| const rows = await sql`select ${1} as t`.values(); | ||
| expect({ rows, sawExecute: sawExecute() }).toEqual({ rows: [["838:59:59"]], sawExecute: true }); | ||
| } finally { | ||
| await new Promise<void>(r => server.close(() => r())); | ||
| } | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.