Skip to content
Open
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: 3 additions & 1 deletion core/src/nats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import type {
import { createInbox } from "./core.ts";
import { errors, InvalidArgumentError, TimeoutError } from "./errors.ts";

const whitespaceRegex = /[ \n\r\t]/;

export class NatsConnectionImpl implements NatsConnection {
options: ConnectionOptions;
protocol!: ProtocolHandler;
Expand Down Expand Up @@ -90,7 +92,7 @@ export class NatsConnectionImpl implements NatsConnection {
throw new errors.DrainingConnectionError();
}
subject = subject || "";
if (subject.length === 0) {
if (subject.length === 0 || whitespaceRegex.test(subject)) {
throw new errors.InvalidSubjectError(subject);
}
}
Expand Down
14 changes: 14 additions & 0 deletions core/tests/basics_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1800,3 +1800,17 @@ Deno.test("basics - close status", async () => {
await d;
await cleanup(ns, nc);
});

Deno.test("basics - pub subject verified", async () => {
const { ns, nc } = await setup();
assertThrows(
() => {
// subject that encodes a protocol pub...
nc.publish("foo 6\r\npwntus\r\nPUB bar");
},
Error,
"illegal subject",
);

await cleanup(ns, nc);
});
Loading