Ruby SDK

Rack middleware and client IP

Pass the end user's IP address to Scute on secret-key calls, with the Rack middleware, a client option or a block.

When your server starts or completes a sign-in with the secret key (a step-up challenge, creating or signing in a user), Scute sees your server's address, not the person's. Pass the person's address and Scute checks it against the workspace's IP rules. Without it, those sign-ins are not checked against IP rules and Scute logs auth.login.ip_rules_skipped. On other secret-key calls the address is recorded as the person's for sessions and audit events.

The SDK sends it as the X-Scute-Client-IP header, only on calls made with the secret key.

Scute::ClientIP::Middleware#

Every Scute client used while handling a request passes that request's address, including a client you created once at boot, unless the client was given its own.

config/application.rbruby
config.middleware.use Scute::ClientIP::Middleware
config.ruruby
use Scute::ClientIP::Middleware

In Rails, add it after ActionDispatch::RemoteIp (config.middleware.use appends it to the end) so the trusted proxies apply. The middleware reads env["action_dispatch.remote_ip"] when Rails set it, otherwise Rack::Request#ip. An address that is not one IP is skipped, never sent.

Other ways to pass it#

# every secret-key call from this clientscute = Scute::Client.new(client_ip: request.remote_ip)# a copy, for one call or a fewscute.with_client_ip(request.remote_ip).users.create("ada@example.com")# any client without its own address, in this blockScute::ClientIP.with(request.remote_ip) do  scute.authz.start_step_up(user_id: id, decision: decision, method: "email_otp")end

Precedence: the client's own client_ip, then the address set by Scute::ClientIP.with or the middleware for the current thread.

The controller concerns pass it for you: scute_client uses scute_client_ip. See Rails integration.

What is accepted#

Scute takes one IPv4 or IPv6 address, never a range or a list.

InputValue you pass (client_ip:, with_client_ip, ClientIP.with)Value read from a request
One IPv4 or IPv6 addresssent, trimmedsent, trimmed
nil or blanknothing sentnothing sent
A range (10.0.0.0/8), a list, an IPv6 zone id (fe80::1%eth0), anything elseraises ArgumentErrorskipped

Behind a proxy#

The SDK never reads X-Forwarded-For itself. In Rails it uses request.remote_ip, so list your proxies in config.action_dispatch.trusted_proxies. In plain Rack it uses Rack::Request#ip, which skips Rack's trusted (private) proxies. If your proxy passes the address another way, read it yourself and pass it with client_ip:, with_client_ip or Scute::ClientIP.with.

Private networks

Rack, and Rails by default, treat every private-network peer (10/8, 172.16/12, 192.168/16, loopback, fc00::/7) as a trusted proxy. A client that reaches the app directly from a private address can then choose the address sent to Scute with its own X-Forwarded-For. If that is possible in your network, limit the trusted proxies to yours (config.action_dispatch.trusted_proxies, or Rack::Request.ip_filter).

The concerns in a plain Rack app#

Scute::Authentication and Scute::Authorization need only a request method. In a Rack app, wrap them in a small class:

app.rbruby
require "rack"require "json"require "scute"class ScuteController  include Scute::Authentication  include Scute::Authorization  attr_reader :request  def initialize(request) = @request = requestendApp = lambda do |env|  controller = ScuteController.new(Rack::Request.new(env))  controller.scute_authenticate!  [200, { "content-type" => "application/json" }, [JSON.generate(user_id: controller.scute_user_id)]]rescue Scute::Unauthenticated => e  [401, { "content-type" => "application/json" }, [JSON.generate(error: e.reason)]]rescue Scute::Forbidden => e  [403, { "content-type" => "application/json" }, [JSON.generate(error: e.decision.reason)]]end

Helpers#

MethodReturns
Scute::ClientIP.parse(value)The address, trimmed, or nil when it is not one IP address.
Scute::ClientIP.parse!(value)Like parse, but raises ArgumentError for a non-blank value that is not one address.
Scute::ClientIP.with(value) { }Sets the address for the block on the current thread.
Scute::ClientIP.currentThe address set by with or the middleware, or nil.
Scute::ClientIP.from_request(request)request.remote_ip, else request.ip, parsed.
Scute::ClientIP.from_env(env)env["action_dispatch.remote_ip"], else Rack::Request#ip, parsed.
Scute::ClientIP::HEADER"X-Scute-Client-IP"