Skip to content

Commit

Permalink
[FIXED] JetStream: js_GetConsumerInfo should check consumer name
Browse files Browse the repository at this point in the history
The API should check that the provided consumer name is valid.
If it contains "." (which is illegal), the call will fail with
a timeout instead of `NATS_INVALID_ARG`.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
  • Loading branch information
kozlovic committed Apr 4, 2022
1 parent 28029e6 commit 5e891b5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/js.c
Original file line number Diff line number Diff line change
Expand Up @@ -1881,10 +1881,10 @@ _processConsInfo(const char **dlvSubject, jsConsumerInfo *info, jsConsumerConfig
}

natsStatus
js_checkDurName(const char *dur)
js_checkConsumerName(const char *cons, bool isDurable)
{
if (strchr(dur, '.') != NULL)
return nats_setError(NATS_INVALID_ARG, "invalid durable name '%s' (cannot contain '.')", dur);
if (strchr(cons, '.') != NULL)
return nats_setError(NATS_INVALID_ARG, "invalid %s name '%s' (cannot contain '.')", (isDurable ? "durable" : "consumer"), cons);
return NATS_OK;
}

Expand Down Expand Up @@ -1996,7 +1996,7 @@ _subscribe(natsSubscription **new_sub, jsCtx *js, const char *subject, const cha
// If a durable name is specified, check that it is valid
if (!nats_IsStringEmpty(durable))
{
if ((s = js_checkDurName(durable)) != NATS_OK)
if ((s = js_checkConsumerName(durable, true)) != NATS_OK)
return NATS_UPDATE_ERR_STACK(s);
}

Expand Down
2 changes: 1 addition & 1 deletion src/js.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void
js_cleanStreamState(jsStreamState *state);

natsStatus
js_checkDurName(const char *dur);
js_checkConsumerName(const char *cons, bool isDurable);

natsStatus
js_getMetaData(const char *reply,
Expand Down
6 changes: 5 additions & 1 deletion src/jsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,7 @@ js_AddConsumer(jsConsumerInfo **new_ci, jsCtx *js,

if (!nats_IsStringEmpty(cfg->Durable))
{
if ((s = js_checkDurName(cfg->Durable)) != NATS_OK)
if ((s = js_checkConsumerName(cfg->Durable, true)) != NATS_OK)
return NATS_UPDATE_ERR_STACK(s);
}

Expand Down Expand Up @@ -2140,6 +2140,10 @@ js_GetConsumerInfo(jsConsumerInfo **new_ci, jsCtx *js,
if (nats_IsStringEmpty(consumer))
return nats_setError(NATS_INVALID_ARG, "%s", jsErrConsumerNameRequired);

s = js_checkConsumerName(consumer, false);
if (s != NATS_OK)
return NATS_UPDATE_ERR_STACK(s);

s = js_setOpts(&nc, &freePfx, js, opts, &o);
if (s == NATS_OK)
{
Expand Down
6 changes: 6 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -22896,6 +22896,12 @@ test_JetStreamMgtConsumers(void)
&& (strstr(nats_GetLastError(NULL), jsErrConsumerNameRequired) != NULL));
nats_clearLastError();

test("Get consumer info (consumer name invalid): ");
s = js_GetConsumerInfo(&ci, js, "MY_STREAM", "bad.consumer.name", NULL, &jerr);
testCond((s == NATS_INVALID_ARG)
&& (strstr(nats_GetLastError(NULL), "invalid consumer name") != NULL));
nats_clearLastError();

test("Get consumer info: ");
s = js_GetConsumerInfo(&ci, js, "MY_STREAM", "dur", NULL, &jerr);
testCond((s == NATS_OK) && (jerr == 0) && (ci != NULL)
Expand Down

0 comments on commit 5e891b5

Please sign in to comment.