Skip to content

Conversation

@habbbe
Copy link
Contributor

@habbbe habbbe commented Jun 2, 2025

Fix parsing of headers with NULL or all-whitespace values when lifting

Add tests for NULL and all-whitespace header values

… values

Fix parsing of headers with NULL or all-whitespace values when lifting

Add tests for NULL and all-whitespace header values
Copy link
Member

@kozlovic kozlovic left a comment

Choose a reason for hiding this comment

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

I was going to say that we should simply fix the fact that we allow NULL or empty values, but I checked the Go client, and those are not rejected. Even worse, on the Go client, there is no error returned when a key is not present, we just return "", so user will not really know if the key was present but empty or the value is empty.
So I am willing to accept those changes. I have however made some comments on the tests.

Comment on lines 20112 to 20117
IFOK(s, natsMsgHeader_Add(msg, "NULL header", NULL))
testCond(s == NATS_OK);

IFOK(s, natsMsgHeader_Add(msg, "Whitespace header", " "))
testCond(s == NATS_OK);

Copy link
Member

Choose a reason for hiding this comment

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

Let's add some test() so that when running the test in verbose, we see what we are testing and the PASSED result. The name of the keys are wrong. They need to be of the form This-Is-Correct-Name. The last part I suggest adding is to make sure that your changes still work when adding more than one of those "bad" values to the same key, and later retrieve them with Values() API:

    test("Add NULL header: ");
    IFOK(s, natsMsgHeader_Add(msg, "Null-Header", NULL))
    testCond(s == NATS_OK);

    test("Add whitespace header: ");
    IFOK(s, natsMsgHeader_Add(msg, "Whitespace-Header", "  "))
    testCond(s == NATS_OK);

    test("Add NULL and whitespace values under same header: ");
    IFOK(s, natsMsgHeader_Add(msg, "Special-Headers", NULL))
    IFOK(s, natsMsgHeader_Add(msg, "Special-Headers", "  "))
    testCond(s == NATS_OK);

Comment on lines 20143 to 20151
s = natsMsgHeader_Get(rmsg, "NULL header", &val);
testCond((s == NATS_OK)
&& (val != NULL) && *val == '\0');


s = natsMsgHeader_Get(rmsg, "Whitespace header", &val);
testCond((s == NATS_OK)
&& (val != NULL) && *val == '\0');

Copy link
Member

Choose a reason for hiding this comment

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

I would change with this:

    test("Check null header: ");
    s = natsMsgHeader_Get(rmsg, "Null-Header", &val);
    testCond((s == NATS_OK)
                && (val != NULL) && *val == '\0');

    test("Check whitespace header: ");
    s = natsMsgHeader_Get(rmsg, "Whitespace-Header", &val);
    testCond((s == NATS_OK)
                && (val != NULL) && *val == '\0');

    test("Check getting values from special header: ");
    s = natsMsgHeader_Values(rmsg, "Special-Headers", &values, &count);
    testCond((s == NATS_OK) && (count == 2)
                && (values != NULL) && (*values[0] == '\0') && (*values[1] == '\0'));
    free(values);
    values = NULL;

@habbbe
Copy link
Contributor Author

habbbe commented Jun 2, 2025

I was going to say that we should simply fix the fact that we allow NULL or empty values, but I checked the Go client, and those are not rejected. Even worse, on the Go client, there is no error returned when a key is not present, we just return "", so user will not really know if the key was present but empty or the value is empty. So I am willing to accept those changes. I have however made some comments on the tests.

Thank you for the comments. I will fix these ASAP.

The reason for making the PR was that all functions for adding headers do say that the value can be NULL or empty, and empty (or all-whitespace) headers could already be broadcast (but not retrieved, at least not with the C client).

The fact that there were tests explicitly testing for non-support for NULL/empty though made me slightly worried that the comments were a typo, but in the name of not breaking existing API, I opted for adding support rather than removing it.

@habbbe
Copy link
Contributor Author

habbbe commented Jun 2, 2025

Added suggestions. I also added an additional test for completely empty value.

Copy link
Member

@kozlovic kozlovic left a comment

Choose a reason for hiding this comment

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

LGTM - Thank you for your contribution!

@kozlovic kozlovic merged commit cc2edc6 into nats-io:main Jun 4, 2025
github-actions bot pushed a commit that referenced this pull request Jun 4, 2025
kozlovic added a commit that referenced this pull request Jun 4, 2025
Made this a test instead. Also fixed a flapper that was due to a
recent change in the NATS Server where an INFO in response of the
CONNECT protocol is now sent, causing the override server version
that we use in the test to be replaced with the actual server
version.

Relates to #873

Signed-off-by: Ivan Kozlovic <[email protected]>
levb added a commit that referenced this pull request Jun 9, 2025
* Fixed compiler issue on macOS related to assert for status text

Made this a test instead. Also fixed a flapper that was due to a
recent change in the NATS Server where an INFO in response of the
CONNECT protocol is now sent, causing the override server version
that we use in the test to be replaced with the actual server
version.

Relates to #873

Signed-off-by: Ivan Kozlovic <[email protected]>

* FIXED: alternate fix for macOS static_assert issue (#875)

* FIXED: alternate fix for macOS static_assert issue

* Removed the new test

---------

Signed-off-by: Ivan Kozlovic <[email protected]>
Co-authored-by: Lev <[email protected]>
kozlovic added a commit that referenced this pull request Jul 28, 2025
* Fixed compiler issue on macOS related to assert for status text

Made this a test instead. Also fixed a flapper that was due to a
recent change in the NATS Server where an INFO in response of the
CONNECT protocol is now sent, causing the override server version
that we use in the test to be replaced with the actual server
version.

Relates to #873

Signed-off-by: Ivan Kozlovic <[email protected]>

* FIXED: alternate fix for macOS static_assert issue (#875)

* FIXED: alternate fix for macOS static_assert issue

* Removed the new test

---------

Signed-off-by: Ivan Kozlovic <[email protected]>
Co-authored-by: Lev <[email protected]>
kozlovic added a commit that referenced this pull request Sep 26, 2025
Release notes will be:

This release contains some breaking changes. See the "Changed" section below.

* Build
  * Disable NATS Streaming by default by @mtmk in #770
  * TLS
    * Require OpenSSL 1.1.1+ to compile. Removed the `NATS_BUILD_TLS_USE_OPENSSL_1_1_API` CMake variable by @kozlovic in #905
    * The option `natsOptions_SetSSLVerificationCallback` signature was changed to replace the use of `SSL_verify_cb` (which required OpenSSL dependency in the `nats.h` file), to the new callback `natsSSLVerifyCb`. See documentation of `natsSSLVerifyCb` to see the cast needed to compile with this new header file by @kozlovic in #908
* Modification of a `natsOptions` object if it has TLS/SSL configuration and is actively used by connections will now return a `NATS_ILLEGAL_STATE` by @kozlovic in #912

* Options
  * Ability to load the trusted CA certificates from a directory using the new option `natsOptions_LoadCATrustedCertificatesPath` by @kerbert101 in #862
  * Ability to connect via HTTP proxy for instance by adding a proxy connection handler using the new option `natsOptions_SetProxyConnHandler` by @wolfkor in #871 and @kozlovic in #897
  * Ability to load the certificate chain and key from a file on every connection attempt using the new option `natsOptions_LoadCertificatesChainDynamic` by @Matus-p in #901
  * Ability to perform concurrent TLS handshakes that may improve time it takes for concurrent connections to be established using the new option `natsOptions_AllowConcurrentTLSHandshakes ` by @kozlovic in #914. Issue was reported by @yanyongcheng in #899
* JetStream
  * Per-message TTL support (a NATS Server v2.11 feature) by @levb in #863
  * Pull consumer priority groups (a NATS Server v2.11 feature) by @levb in #869
* ObjectStore support by @kozlovic in #902. Thanks to @jfflynn41 and @alex1891 for the feedback in #876

* JetStream
  * Handling of publish asynchronous timeouts by @kozlovic in #886. Issue reported by @yanyongcheng in #880
* Timer insertion by @kozlovic in #883. Issue reported by @yanyongcheng in #881

* EventLoop:
  * Handling of possible failure on initial attach by @kozlovic in #918
  * LibEvent: `natsConnection_Close()` not closing the TCP connection by @kozlovic in #882. Issue was reported by @yanyongcheng in #879
  * Libuv: Possible crash if connection is destroyed while receiving data by @kozlovic in #889. Issue was reported by @yanyongcheng in #888
* KeyValue
  * Keys, History or watcher's next may incorrectly return `NATS_TIMEOUT` by @kozlovic in #917/ Issue was reported by @ArashPartow in #916
* MicroServices:
  * Wrong marshaling of `average_processing_time` by @kozlovic in #892. Issue was reported by @Archie3d in #890
  * Statistics error was always incremented by @kozlovic in #894. Issue was reported by @Archie3d in #893
* TLS
  * Unknown type name `SSL_verify_cb` by @kozlovic in #878. Issue was reported by @philipfoulkes in #877
  * Initialization and cleanup code related to OpenSSL was removed since it was deprecated for versions post OpenSSL 1.1. A cleanup function pertinent to 1.1+ code was possibly causing a problem. By @kozlovic in #905. Issue was reported by @vdeters in #904
  * Possible hang during handshake by @kozlovic in #907. Issue was reported by @etrochim in #906
  * Protect calls to `SSL_read` and `SSL_write` with a mutex. Since the same `SSL` object is shared between different threads, the OpenSSL library requires a mutex to be used by @kozlovic in #913
* Memory allocation check by @wooffie in #868
* Add missing status text string by @oldnick85 in #872 and @kozlovic in #874 (the issue was not present in any published release and was introduced in #869)
* Parsing of message headers with `NULL` or all-whitespace values by @habbbe in #873
* Removed some unused code related to handling of responses and added custom inbox with very long prefix test by @kozlovic in #885. Issue was reported by @yanyongcheng in #884
* Connection drain could cause missed reply and/or a 100ms delay by @kozlovic in #915. Issue was reported by @T-Maxxx in #911

* Build
  * Deprecated Ubuntu 20.04 in GitHub actions by @levb in #864
  * Removed the older compiler jobs by @levb in #865
  * Fixed Windows build to use the NATS Server main branch by @levb in #866

* @kerbert101 made their first contribution in #862
* @habbbe made their first contribution in #873
* @wolfkor made their first contribution in #871
* @Matus-p made their first contribution in #901

Signed-off-by: Ivan Kozlovic <[email protected]>
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