Skip to content

Remote IP available through context-object #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

COCPORN
Copy link

@COCPORN COCPORN commented Jun 28, 2025

PURPOSE

  • Enrich the context-object with getRemoteIp() for query/mutation/action, returning the remote IP on the connection
  • "Zero cost" if not used, called synchronously for speed
  • Opens for application level audit-logging, IP-based feature-blocking/rate-limiting, etc

CAVEAT

Depending on how Convex Cloud is load-balanced, this might or might not work. If the service is setup using Layer 7 load balancers, this will not work as it relies on X-Forwarded-For being set, which this PR doesn't honor (but can be made to, conceptually).

If it is balanced using Layer 4 with NLB, this will probably work with Convex Cloud as it usually passes on the source IP-address directly.

It will very likely work for standalone backend.

Also: I am not sure it needs to be implemented as a function call, but it was not obvious how to have this available when V8 is initialized. Implementing it as a property directly on ctx would incur some unwanted overhead as JavaScript needs to call back into Rust, which we want to avoid in the common case.

USAGE

  // In your Convex functions:
  export const logClientInfo = mutation({
    handler: async (ctx, args) => {
      // Only pays cost when you actually need it
      const clientIp = ctx.getRemoteIp();
      console.log("Request from: ", clientIp);

      // Functions that don't call getRemoteIp() have "zero overhead"
      return { success: true };
    }
  });

TEST RUN

test result: FAILED. 154 passed; 3 failed; 5 ignored; 0 measured; 0 filtered out; finished in 10.62s

For me, this is currently the same as the main-branch, so I don't think errors are introduced that tests would catch.

CODE QUALITY

This PR needs to be looked after by a seasoned Convex developer as it is my first attempt at making something in Convex core. If it is rejected, feel free to use it as inspiration for a proper implementation.

IMPROVEMENTS (potential)

  • Split into getRemoteIp and getRemotePort, perhaps, currently the port is returned with getRemoteIp on the format 127.0.0.1:57345.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@COCPORN
Copy link
Author

COCPORN commented Jun 28, 2025

BTW, I seem to have introduced a compile time warning, I will look at that today.

@nipunn1313
Copy link
Collaborator

thx for the PR. Check out the discussion in #130 - too.

@thomasballinger
Copy link
Contributor

Cool!

I don't want this to be available in queries yet (since it's not recorded in the cache key) and the way convex APIs generally handle different capabilities in actions vs mutations vs queries is by using the ctx objects, but that'll take more design; what you've done makes sense without those constraints.

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.

4 participants