forked from evs-broadcast/node-emberplus
-
Notifications
You must be signed in to change notification settings - Fork 19
fix: hardening #49
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
Merged
Merged
fix: hardening #49
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dbc6851
fix: add max frame/packet size
Julusian eacdd5b
fix: guard against 0 values in reader causing infinite loops
Julusian 0bdb4c8
fix: disconnect connections that exceed size limits
Julusian a2dd77a
fix: packet size error handling
Julusian 8945393
Merge branch 'main' into fix/hardening
Julusian 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
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,33 @@ | ||
| import { Writer } from '../Writer' | ||
| import { BERDataTypes } from '../BERDataTypes' | ||
| import { Reader } from '../Reader' | ||
|
|
||
| describe('BER Reader.readReal', () => { | ||
| // Regression for the infinite-loop DoS: a non-canonical REAL with an explicit | ||
| // all-zero significand must short-circuit to 0 rather than spin the | ||
| // normalisation loops forever. Tight timeout so a regression fails fast. | ||
| test('zero-significand REAL returns 0 quickly', () => { | ||
| // 09 (REAL tag) 03 (length) 00 (preamble) 00 (exponent) 00 (significand) | ||
| const buf = Buffer.from([BERDataTypes.REAL, 0x03, 0x00, 0x00, 0x00]) | ||
|
|
||
| const r = new Reader(buf) | ||
| expect(r.readReal()).toEqual(0) | ||
| }, 2000) | ||
|
|
||
| test('zero-significand REAL via readValue returns 0 quickly', () => { | ||
| const buf = Buffer.from([BERDataTypes.REAL, 0x03, 0x00, 0x00, 0x00]) | ||
|
|
||
| const r = new Reader(buf) | ||
| expect(r.readValue().value).toEqual(0) | ||
| }, 2000) | ||
|
|
||
| test('normal REAL values still decode', () => { | ||
| for (const value of [8.32, -8.32, 1, -1, 1234.5, 0.0001]) { | ||
| const w = new Writer() | ||
| w.writeReal(value, BERDataTypes.REAL) | ||
|
|
||
| const r = new Reader(w.buffer) | ||
| expect(r.readReal()).toBeCloseTo(value) | ||
| } | ||
| }) | ||
| }) |
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
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
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,73 @@ | ||
| import { SmartBuffer } from 'smart-buffer' | ||
| import S101Codec from '../S101Codec' | ||
|
|
||
| const FLAG_FIRST_MULTI_PACKET = 0x80 | ||
| const FLAG_MULTI_PACKET = 0x00 | ||
|
|
||
| // Build a raw ember frame in the shape handleEmberFrame() expects: | ||
| // version, flags, dtd, appBytes, 2 app bytes, payload, then 2 CRC bytes | ||
| // (handleEmberFrame strips the trailing 2 bytes as CRC). | ||
| function makeEmberFrame(flags: number, payload: Buffer): SmartBuffer { | ||
| const sb = new SmartBuffer() | ||
| sb.writeUInt8(0x01) // version | ||
| sb.writeUInt8(flags) | ||
| sb.writeUInt8(0x01) // dtd (glow) | ||
| sb.writeUInt8(2) // number of app bytes | ||
| sb.writeUInt8(0x1f) // dtd minor version | ||
| sb.writeUInt8(0x02) // dtd major version | ||
| sb.writeBuffer(payload) | ||
| sb.writeUInt8(0x00) // CRC lo (stripped) | ||
| sb.writeUInt8(0x00) // CRC hi (stripped) | ||
| return sb | ||
| } | ||
|
|
||
| describe('S101Codec DoS hardening', () => { | ||
| test('unterminated frame is dropped, not buffered unbounded', () => { | ||
| const codec = new S101Codec() | ||
|
|
||
| // Begin-of-frame, then a stream of non-EOF bytes that never completes a frame. | ||
| codec.dataIn(Buffer.from([0xfe])) | ||
|
|
||
| const chunk = Buffer.alloc(1024 * 1024) // 1 MB of zeros, no 0xff | ||
| expect(() => { | ||
| for (let i = 0; i < 8; i++) { | ||
| codec.dataIn(chunk) | ||
| } | ||
| }).toThrow(/oversized/i) | ||
|
|
||
| // State must be reset so the next dataIn starts clean. | ||
| expect((codec as any).frameBuffer).toBeUndefined() | ||
| }, 5000) | ||
|
|
||
| test('unterminated multi-packet message is dropped', () => { | ||
| const codec = new S101Codec() | ||
|
|
||
| const payload = Buffer.alloc(1024 * 1024) // 1 MB per frame | ||
|
|
||
| // Start a multi-packet message... | ||
| codec.handleEmberFrame(makeEmberFrame(FLAG_FIRST_MULTI_PACKET, payload)) | ||
|
|
||
| // ...then stream continuation frames that never set the last-flag. | ||
| expect(() => { | ||
| for (let i = 0; i < 32; i++) { | ||
| codec.handleEmberFrame(makeEmberFrame(FLAG_MULTI_PACKET, payload)) | ||
| } | ||
| }).toThrow(/oversized/i) | ||
|
|
||
| // Reassembly state must be reset. | ||
| expect((codec as any).multiPacketBuffer).toBeUndefined() | ||
| expect((codec as any).isMultiPacket).toBe(false) | ||
| }, 5000) | ||
|
|
||
| test('sanity: a normal well-formed frame still decodes and emits', () => { | ||
| const codec = new S101Codec() | ||
|
|
||
| const events: string[] = [] | ||
| codec.on('keepaliveReq', () => events.push('req')) | ||
|
|
||
| // keepAliveRequest() produces a complete, CRC-valid S101 frame. | ||
| codec.dataIn(codec.keepAliveRequest()) | ||
|
|
||
| expect(events).toEqual(['req']) | ||
| }) | ||
| }) |
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.