Skip to content

Conversation

@sampan-s-nayak
Copy link
Contributor

@sampan-s-nayak sampan-s-nayak commented Oct 23, 2025

Description

this pr sets up the helper classes and utils to enable token based authentication for ray core rpc calls.

Related issues

NA

Additional information

@sampan-s-nayak sampan-s-nayak requested a review from a team as a code owner October 23, 2025 16:19
@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Whitespace Handling Causes Undefined Behavior

The TrimWhitespace function relies on undefined behavior when an input string contains only whitespace. If find_last_not_of returns std::string::npos, adding 1 to it causes integer wraparound, leading to erase(0). While this happens to produce the correct empty string, the underlying logic is fragile.

Fix in Cursor Fix in Web

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Environment Variables Reference Invalid Memory

On Windows, _putenv is called with temporary strings, such as in set_env_var and the USERPROFILE setup. This results in environment variables pointing to invalid memory after the temporary string is destroyed.

Additional Locations (1)

Fix in Cursor Fix in Web

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces token-based authentication for Ray Core RPCs, which is a great security enhancement. The changes include a secure AuthenticationToken class, a loader for tokens from various sources, and related configuration. The implementation is well-structured and includes comprehensive tests.

I've found a critical issue in the test suite on Windows related to environment variable handling that needs to be addressed. I've also left a few other comments for improving code style, efficiency, and robustness. Overall, this is a solid contribution.

Comment on lines +110 to +117
void set_env_var(const char *name, const char *value) {
#ifdef _WIN32
std::string env_str = std::string(name) + "=" + std::string(value);
_putenv(env_str.c_str());
#else
setenv(name, value, 1);
#endif
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The Windows implementation of set_env_var has a critical bug. The _putenv function does not copy the string it's given; it only stores the pointer. Here, env_str is a local variable, so the pointer passed to _putenv becomes dangling as soon as this function returns, leading to undefined behavior.

The same issue exists in unset_env_var (lines 119-126) and SetUp (lines 52-64) for the _putenv call.

To fix this, the string buffer must remain valid for the duration of its use in the environment. A good approach for this test class would be to store these environment strings in a std::vector<std::string> member of the AuthenticationTokenLoaderTest fixture.

Example fix for set_env_var:

  1. Add std::vector<std::string> managed_env_strings_; to the test fixture.
  2. Modify set_env_var like this:
#ifdef _WIN32
    managed_env_strings_.emplace_back(std::string(name) + "=" + std::string(value));
    _putenv(managed_env_strings_.back().c_str());
#else
    setenv(name, value, 1);
#endif

You'll need to apply similar fixes to unset_env_var and SetUp.

Comment on lines +95 to +98
const std::string_view prefix(kBearerPrefix, sizeof(kBearerPrefix) - 1);
if (metadata_value.size() <= prefix.size() ||
metadata_value.substr(0, prefix.size()) != prefix) {
return AuthenticationToken(); // Invalid format, return empty
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The implementation for stripping the "Bearer " prefix can be made more robust and readable. Using sizeof(kBearerPrefix) - 1 is a C-style idiom that can be brittle if the constant definition changes. A more modern C++ approach using std::string_view's properties would be safer. Also, the size check can be simplified for better clarity.

Suggested change
const std::string_view prefix(kBearerPrefix, sizeof(kBearerPrefix) - 1);
if (metadata_value.size() <= prefix.size() ||
metadata_value.substr(0, prefix.size()) != prefix) {
return AuthenticationToken(); // Invalid format, return empty
const std::string_view prefix(kBearerPrefix);
if (metadata_value.size() < prefix.size() ||
metadata_value.substr(0, prefix.size()) != prefix) {
return AuthenticationToken(); // Invalid format, return empty
}

Comment on lines +23 to +26
#if defined(__APPLE__) || defined(__linux__)
#include <sys/stat.h>
#include <unistd.h>
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

These headers appear to be unused in this file. It's good practice to remove unnecessary includes to improve compilation times and reduce dependencies.

AuthenticationToken AuthenticationTokenLoader::LoadTokenFromSources() {
// Precedence 1: RAY_AUTH_TOKEN environment variable
const char *env_token = std::getenv("RAY_AUTH_TOKEN");
if (env_token != nullptr && std::string(env_token).length() > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Creating a std::string from a const char* just to check its length is inefficient as it can cause a memory allocation. You can check if the C-style string is non-empty more efficiently.

Suggested change
if (env_token != nullptr && std::string(env_token).length() > 0) {
if (env_token != nullptr && *env_token != '\0') {


// Precedence 2: RAY_AUTH_TOKEN_PATH environment variable
const char *env_token_path = std::getenv("RAY_AUTH_TOKEN_PATH");
if (env_token_path != nullptr && std::string(env_token_path).length() > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Creating a std::string from a const char* just to check its length is inefficient as it can cause a memory allocation. You can check if the C-style string is non-empty more efficiently.

Suggested change
if (env_token_path != nullptr && std::string(env_token_path).length() > 0) {
if (env_token_path != nullptr && *env_token_path != '\0') {

Comment on lines +102 to +108
std::string get_temp_token_path() {
#ifdef _WIN32
return "C:\\Windows\\Temp\\ray_test_token_" + std::to_string(_getpid());
#else
return "/tmp/ray_test_token_" + std::to_string(getpid());
#endif
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Hardcoding temporary paths like /tmp/ and C:\\Windows\\Temp\\ can make tests brittle, as the user running them may not have write permissions. It's better to use a standard temporary directory. You're already using TEST_TMPDIR in SetUp, which is a good practice to follow here as well. Using . as a fallback on Windows is safer than C:\\Windows\\Temp.

  std::string get_temp_token_path() {
    const char *tmp_dir_from_env = std::getenv("TEST_TMPDIR");
#ifdef _WIN32
    const char *tmp_dir = tmp_dir_from_env ? tmp_dir_from_env : ".";
    return std::string(tmp_dir) + "\\ray_test_token_" + std::to_string(_getpid());
#else
    const char *tmp_dir = tmp_dir_from_env ? tmp_dir_from_env : "/tmp";
    return std::string(tmp_dir) + "/ray_test_token_" + std::to_string(getpid());
#endif
  }

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Whitespace Handling Bug in TrimWhitespace Function

The TrimWhitespace function incorrectly handles strings containing only whitespace. When find_last_not_of returns std::string::npos after leading whitespace is removed, adding 1 causes integer wraparound. This leads to an erase call that fails to clear the string, leaving it with whitespace instead of empty.

Fix in Cursor Fix in Web

@sampan-s-nayak sampan-s-nayak added the go add ONLY when ready to merge, run all tests label Oct 23, 2025
@ray-gardener ray-gardener bot added the core Issues that should be addressed in Ray Core label Oct 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant