Skip to content

fix: Adding validation for raw data size parsing - #85

Open
IZ-sandwich wants to merge 1 commit into
foxssake:mainfrom
IZ-sandwich:main
Open

fix: Adding validation for raw data size parsing#85
IZ-sandwich wants to merge 1 commit into
foxssake:mainfrom
IZ-sandwich:main

Conversation

@IZ-sandwich

Copy link
Copy Markdown

This PR addresses an issue with TrimsockReader where it can be put into a state it can never leave. If queuedRawCommand (on js side, but similar issue exists with raw_command in gd) is set with a non-integer size or size > maxSize every subsequent read() returns undefined regardless of what other input is sent.

The way the reader gets into a bad state is a bad size in raw command sets queuedRawCommand but it is cleared only in the happy path so gets stuck.

I found this issue with my noray server hanging, seemingly being up but not responding to register commands. I suspect it was a crawler making a generic HTTP request containing \r\n and bricking it. Additionally, I found that the sockets were not being detached on close or errors, so fixed that.

I focused on fixing the js side since that's the used dependency of noray, but made similar changes on the godot side, exposing the ingest error as a signal.

Will need to send out a followup pr to upgrade noray to use the newer version of trimsock.

@elementbound elementbound left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huge kudos for the fix! 🎉 Added some thoughts and a few questions.

Comment thread trimsock.gd/addons/trimsock.gd/line_reader.gd
Comment thread trimsock.gd/addons/trimsock.gd/reader.gd
Comment thread trimsock.gd/addons/trimsock.gd/reader.gd
Comment thread trimsock.gd/addons/trimsock.gd/reader.gd
Comment thread trimsock.gd/tests/reactor.test.gd
]);
});

test("should reject raw data with a malformed terminator", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test("should reject raw data with a malformed terminator", () => {
test("should reject raw data with incorrect size", () => {

WDYT?


// The malformed terminator is consumed in its place, so parsing resumes
// on the next line
reader.ingest("\rcommand 4\n1234Xcommand foo\n");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
reader.ingest("\rcommand 4\n1234Xcommand foo\n");
reader.ingest("\rinvalid-command 4\n1234Xvalid-command foo\n");

reader.ingest("\rcommand 4\n1234Xcommand foo\n");
expect(() => [...reader.commands()]).toThrow(UnexpectedCharacterError);

expect([...reader.commands()].map((it) => it.name)).toEqual(["command"]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect([...reader.commands()].map((it) => it.name)).toEqual(["command"]);
expect([...reader.commands()].map((it) => it.name)).toEqual(["valid-command"]);

.createServer(options, connectionListener)
.on("connection", (socket: net.Socket) => {
socket.on("data", (data: Buffer) => this.ingest(data, socket));
socket.on("close", () => this.detach(socket));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need an "error" handler too?

): net.Socket {
const socket = net.createConnection(options, connectionListener);
socket.on("data", (data: Buffer) => this.ingest(data, socket));
socket.on("close", () => this.detach(socket));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need an "error" handler too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants