Skip to content

Commit

Permalink
FIXED: nats_GetJWTOrSeed to understand Windows \r\n lines (#801)
Browse files Browse the repository at this point in the history
* FIXED: nats_GetJWTOrSeed to understand Windows \r\n lites

* Adjusted the test

* Adjusted the test, +1

* PR feedback
  • Loading branch information
levb authored Sep 30, 2024
1 parent 87d7472 commit f5a7e60
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 46 deletions.
95 changes: 50 additions & 45 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2064,18 +2064,16 @@ nats_HostIsIP(const char *host)
}

static bool
_isLineAnHeader(const char *ptr)
_isLineAnHeader(const char *ptr, int len)
{
char *last = NULL;
int len = 0;
int count = 0;
bool done = false;

// We are looking for a header. Based on the Go client's regex,
// the strict requirement is that it ends with at least 3 consecutive
// `-` characters. It must also have 3 consecutive `-` before that.
// So the minimum size would be 6.
len = (int) strlen(ptr);
if (len < 6)
return false;

Expand Down Expand Up @@ -2118,61 +2116,72 @@ _isLineAnHeader(const char *ptr)
return false;
}

// Finds the next entire line in next, sets start to the beginning of the line,
// updates next to point to the remaining bytes, and returns the line's length.
// If there are no more non-empty lines, returns 0.
static inline int
_scan_line(const char **start, const char **next)
{
const char *ptr = *next;
int n = 0;

// skip empty lines in the beginning
while (*ptr == '\r' || *ptr == '\n')
ptr++;
*start = ptr;

// Consume until we reach the end of the line
while (*ptr != '\r' && *ptr != '\n' && *ptr != '\0')
{
ptr++;
n++;
}

if (n == 0)
return 0;

// skip the subsequent empty lines, 'cause why not?
while (*ptr == '\r' || *ptr == '\n')
ptr++;
*next = ptr;

return n;
}

natsStatus
nats_GetJWTOrSeed(char **val, const char *content, int item)
{
natsStatus s = NATS_OK;
char *pch = NULL;
char *str = NULL;
char *saved = NULL;
int curItem = 0;
int orgLen = 0;
char *nt = NULL;

// First, make a copy of the original content since
// we are going to call strtok on it, which alters it.
str = NATS_STRDUP(content);
if (str == NULL)
return nats_setDefaultError(NATS_NO_MEMORY);

orgLen = (int) strlen(str);
natsStatus s = NATS_OK;
const char *next = content;
const char *line = NULL;
int lineLen = 0;
int curItem = 0;
const char *saved = NULL;
int savedLen = 0;

pch = nats_strtok(str, "\n", &nt);
while (pch != NULL)
while ((lineLen = _scan_line(&line, &next)) > 0)
{
if (_isLineAnHeader(pch))
if (_isLineAnHeader(line, lineLen))
{
// We got the start of the section. Save the next line
// as the possible returned value if the following line
// is a header too.
pch = nats_strtok(NULL, "\n", &nt);
saved = pch;
savedLen = _scan_line(&saved, &next);
if (savedLen == 0)
break; // premature end of file?

while (pch != NULL)
{
pch = nats_strtok(NULL, "\n", &nt);
if (pch == NULL)
break;

// We tolerate empty string(s).
if (*pch == '\0')
continue;

break;
}
if (pch == NULL)
break;

if (_isLineAnHeader(pch))
lineLen = _scan_line(&line, &next);
if (_isLineAnHeader(line, lineLen))
{
// Is this the item we were looking for?
if (curItem == item)
{
// Return a copy of the saved line
*val = NATS_STRDUP(saved);
*val = NATS_CALLOC(savedLen + 1, 1);
if (*val == NULL)
s = nats_setDefaultError(NATS_NO_MEMORY);

else
memcpy(*val, saved, savedLen);
break;
}
else if (++curItem > 1)
Expand All @@ -2181,12 +2190,8 @@ nats_GetJWTOrSeed(char **val, const char *content, int item)
}
}
}
pch = nats_strtok(NULL, "\n", &nt);
}

memset(str, 0, orgLen);
NATS_FREE(str);

// Nothing was found, return NATS_NOT_FOUND but don't set the stack error.
if ((s == NATS_OK) && (*val == NULL))
return NATS_NOT_FOUND;
Expand Down
3 changes: 2 additions & 1 deletion test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4759,10 +4759,11 @@ void test_natsGetJWTOrSeed(void)
char buf[256];
const char *valids[] = {
"--- START JWT ---\nsome value\n--- END JWT ---\n",
"\r\n\r\n--- START JWT ---\r\nsome value\r\n--- END JWT ---\r\n",
"--- ---\nsome value\n--- ---\n",
"------\nsome value\n------\n",
"---\nabc\n--\n---START---\nsome value\n---END---\n----\ndef\n--- ---\n",
"nothing first\nthen it starts\n --- START ---\nsome value\n--- END ---\n---START---\nof something else\n---END---\n",
"nothing first\nthen it starts\n\r\n --- START ---\r\n\n\n\r\nsome value\n--- END ---\n\n---START---\nof something else\n---END---\n",
"--- START ---\nsome value\n\n\n--- END ---\n",
};
const char *invalids[] = {
Expand Down

0 comments on commit f5a7e60

Please sign in to comment.