Skip to content

Commit f86a4a4

Browse files
committed
[FIX] fixed an issue where permission errors related queue subscriptions was not properly notified to the client
1 parent 8b52908 commit f86a4a4

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

nats-base-client/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class NatsError extends Error {
143143
message: string;
144144
// TODO: on major version this should change to a number/enum
145145
code: string;
146-
permissionContext?: { operation: string; subject: string };
146+
permissionContext?: { operation: string; subject: string; queue?: string };
147147
chainedError?: Error;
148148
// these are for supporting jetstream
149149
api_error?: ApiError;

nats-base-client/protocol.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ export class Subscriptions {
342342
let sub;
343343
if (ctx.operation === "subscription") {
344344
sub = subs.find((s) => {
345-
return s.subject === ctx.subject;
345+
return s.subject === ctx.subject && s.queue === ctx.queue;
346346
});
347347
}
348348
if (ctx.operation === "publish") {
@@ -700,7 +700,13 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
700700
err.permissionContext = {
701701
operation: m[1].toLowerCase(),
702702
subject: m[2],
703+
queue: undefined,
703704
};
705+
706+
const qm = s.match(/using queue "(\S+)"/);
707+
if (qm) {
708+
err.permissionContext.queue = qm[1];
709+
}
704710
}
705711
return err;
706712
} else if (t.indexOf("authorization violation") !== -1) {

tests/auth_test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
assertArrayIncludes,
1818
assertEquals,
1919
assertRejects,
20+
assertStringIncludes,
2021
fail,
2122
} from "https://deno.land/std@0.221.0/assert/mod.ts";
2223
import {
@@ -1254,3 +1255,47 @@ Deno.test("auth - request context", async () => {
12541255

12551256
await cleanup(ns, nc, a);
12561257
});
1258+
1259+
Deno.test("auth - sub permission queue", async () => {
1260+
const conf = {
1261+
authorization: {
1262+
users: [{
1263+
user: "a",
1264+
password: "a",
1265+
permissions: { subscribe: ["q A"] },
1266+
}],
1267+
},
1268+
};
1269+
1270+
const { ns, nc } = await setup(conf, { user: "a", pass: "a" });
1271+
1272+
const qA = deferred();
1273+
nc.subscribe("q", {
1274+
queue: "A",
1275+
callback: (err, msg) => {
1276+
if (err) {
1277+
qA.reject(err);
1278+
}
1279+
},
1280+
});
1281+
1282+
const qBad = deferred<NatsError>();
1283+
nc.subscribe("q", {
1284+
queue: "bad",
1285+
callback: (err, msg) => {
1286+
if (err) {
1287+
qBad.resolve(err);
1288+
}
1289+
},
1290+
});
1291+
await nc.flush();
1292+
1293+
const err = await qBad;
1294+
qA.resolve();
1295+
1296+
await qA;
1297+
1298+
assertEquals(err.code, ErrorCode.PermissionsViolation);
1299+
assertStringIncludes(err.message, 'using queue "bad"');
1300+
await cleanup(ns, nc);
1301+
});

tests/helpers/launcher.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,11 @@ export function toConf(o: any, indent?: string): string {
792792
buf.push(`${pad}${k}: ${v}`);
793793
}
794794
} else {
795-
buf.push(pad + v);
795+
if (v.includes(" ")) {
796+
buf.push(`${pad}"${v}"`);
797+
} else {
798+
buf.push(pad + v);
799+
}
796800
}
797801
}
798802
}

0 commit comments

Comments
 (0)