|
|
@@ -14,6 +14,7 @@
|
|
|
#include "functions/descriptor.hpp"
|
|
|
#include "function_error_dispatch.hpp"
|
|
|
#include "functions/fn_gate.hpp"
|
|
|
+#include "functions/request_headers.hpp"
|
|
|
#include "rate_limiter.hpp"
|
|
|
#include "json_http.hpp"
|
|
|
#include "server.hpp"
|
|
|
@@ -21,6 +22,7 @@
|
|
|
#include <chrono>
|
|
|
#include <ctime>
|
|
|
#include <mutex>
|
|
|
+#include <vector>
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
|
namespace svapi {
|
|
|
@@ -43,11 +45,6 @@ bool reservedResponseHeader(const std::string& lower) {
|
|
|
/// a logged-in admin's browser attaches the session cookie to /fn/* as well,
|
|
|
/// and forwarding it would hand that admin's session to every function author
|
|
|
/// in the project. Authorization goes for the same reason.
|
|
|
-bool secretRequestHeader(const std::string& lower) {
|
|
|
- return lower == "cookie" || lower == "set-cookie" || lower == "authorization"
|
|
|
- || lower == "proxy-authorization" || lower == "x-captcha-token";
|
|
|
-}
|
|
|
-
|
|
|
std::string lowerCase(std::string s) {
|
|
|
std::transform(s.begin(), s.end(), s.begin(),
|
|
|
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
|
|
@@ -135,16 +132,33 @@ bool hasDotDotSegment(const std::string& path) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-nlohmann::json headerJson(const httplib::Request& req) {
|
|
|
- nlohmann::json out = nlohmann::json::object();
|
|
|
- for (const auto& [name, value] : req.headers) {
|
|
|
- const std::string lower = lowerCase(name);
|
|
|
- if (secretRequestHeader(lower)) continue;
|
|
|
- // Lower-cased keys so a script can look one up without guessing the
|
|
|
- // casing a client happened to send.
|
|
|
- out[lower] = value;
|
|
|
- }
|
|
|
- return out;
|
|
|
+/// EVERY request header reaches a function except the five `secretRequestHeader`
|
|
|
+/// denies - there is no allow-list, and there never was. A script that cannot
|
|
|
+/// find a header it expected is almost always looking at a header the CLIENT
|
|
|
+/// did not send, not one we removed.
|
|
|
+///
|
|
|
+/// What was missing is the ability to tell those two apart. A withheld header
|
|
|
+/// was simply ABSENT, so "my cookie is not arriving" and "my proxy is not
|
|
|
+/// forwarding it" produced identical evidence - the absence-is-not-information
|
|
|
+/// trap this codebase keeps meeting. `headersWithheld` names what we took out,
|
|
|
+/// so the question is answerable from inside the function.
|
|
|
+///
|
|
|
+/// NAMES ONLY, NEVER VALUES, and the list is the fixed five rather than
|
|
|
+/// anything caller-controlled - so this discloses that a header was PRESENT
|
|
|
+/// and nothing about what it held. That is the whole point of the five: the
|
|
|
+/// web UI is served from this same origin, so a logged-in admin's browser
|
|
|
+/// attaches its session cookie to /fn/* too, and forwarding it would hand that
|
|
|
+/// admin's session to every function author in the project.
|
|
|
+void applyTriggerHeaders(nlohmann::json& t, const httplib::Request& req) {
|
|
|
+ std::vector<std::pair<std::string, std::string>> flat;
|
|
|
+ flat.reserve(req.headers.size());
|
|
|
+ for (const auto& [name, value] : req.headers) flat.emplace_back(name, value);
|
|
|
+ functions::TriggerHeaders split = functions::splitRequestHeaders(flat);
|
|
|
+ t["headers"] = std::move(split.forwarded);
|
|
|
+ // Emitted ALWAYS, empty array included, for the reason `ip_allow` is: an
|
|
|
+ // absent field cannot distinguish "nothing was withheld" from "this
|
|
|
+ // server does not tell you".
|
|
|
+ t["headersWithheld"] = std::move(split.withheld);
|
|
|
}
|
|
|
|
|
|
/// Repeated query keys collapse to the last value, because the trigger shape is
|
|
|
@@ -340,11 +354,20 @@ void dispatch(ServerDeps& d, const httplib::Request& req, httplib::Response& res
|
|
|
// the caller's address back - it is in our access log where an operator can
|
|
|
// read it, and reflecting it would hand a prober a free "what do you think
|
|
|
// my address is" oracle.
|
|
|
- if (!policies[idx]->ipAllow.empty()) {
|
|
|
+ //
|
|
|
+ // RESOLVED ONCE, HERE, for all four consumers: the allow-list gate below,
|
|
|
+ // the per-caller throttle after it, `ctx.clientIp`, and the access log.
|
|
|
+ // It used to be computed inside the allow-list branch, so a function
|
|
|
+ // without a list never had it - and the throttle and the ctx field both
|
|
|
+ // need it whether or not a list exists. One resolution also means the four
|
|
|
+ // cannot disagree about who the caller is, which is the property that
|
|
|
+ // makes the access log usable as evidence about the gate's decision.
|
|
|
+ const std::string clientIp = [&] {
|
|
|
auto snap = d.settings.snapshot();
|
|
|
- // The SAME resolution the access log uses, so the line an operator
|
|
|
- // reads names the address this gate actually acted on.
|
|
|
- const std::string clientIp = clientIpFor(req, *snap);
|
|
|
+ return clientIpFor(req, *snap);
|
|
|
+ }();
|
|
|
+
|
|
|
+ if (!policies[idx]->ipAllow.empty()) {
|
|
|
if (!functions::addressAllowedBy(policies[idx]->ipAllow, clientIp)) {
|
|
|
sendJson(res, 403,
|
|
|
errorBody("ip_not_allowed",
|
|
|
@@ -404,6 +427,27 @@ void dispatch(ServerDeps& d, const httplib::Request& req, httplib::Response& res
|
|
|
// and shut out legitimate ones. Per-caller throttling needs an identity,
|
|
|
// which is exactly what `auth: 'key'` provides and an anonymous request
|
|
|
// does not have.
|
|
|
+ // THE PER-CALLER CEILING, charged first - see fnCallerThrottleBucket for
|
|
|
+ // why the order is load-bearing rather than incidental. It is the missing
|
|
|
+ // half of the sentence below: the per-function cap bounds the damage and
|
|
|
+ // explicitly does NOT bound the denial, and this does.
|
|
|
+ //
|
|
|
+ // Only where a caller exists. An empty address means nothing trustworthy
|
|
|
+ // resolved, and bucketing those together would charge every such request
|
|
|
+ // to one shared counter - a throttle keyed on "" rather than on a caller.
|
|
|
+ if (policies[idx]->rateLimitPerCallerPerMin > 0 && !clientIp.empty() &&
|
|
|
+ !rateLimiter().allow(
|
|
|
+ functions::fnCallerThrottleBucket(project, functionId, clientIp),
|
|
|
+ policies[idx]->rateLimitPerCallerPerMin)) {
|
|
|
+ res.set_header("Retry-After", std::to_string(kRetryAfterRateLimitedSec));
|
|
|
+ // Deliberately the SAME slug as the per-function refusal. A caller
|
|
|
+ // learning which ceiling it hit learns the other ceiling's value by
|
|
|
+ // probing, and the remedy is identical either way: slow down.
|
|
|
+ sendJson(res, 429,
|
|
|
+ errorBody("rate_limited", "this function's per-minute limit is exhausted"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (policies[idx]->rateLimitPerMin > 0 &&
|
|
|
!rateLimiter().allow(functions::fnHttpThrottleBucket(project, functionId),
|
|
|
policies[idx]->rateLimitPerMin)) {
|
|
|
@@ -439,7 +483,14 @@ void dispatch(ServerDeps& d, const httplib::Request& req, httplib::Response& res
|
|
|
{"path", path},
|
|
|
{"params", routeParamsJson(match.params)},
|
|
|
{"query", queryJson(req)},
|
|
|
- {"headers", headerJson(req)}};
|
|
|
+ // The RESOLVED address, never a header the caller
|
|
|
+ // wrote. A throttle keyed on a caller-controlled
|
|
|
+ // value is not a throttle, which is the whole
|
|
|
+ // reason this is worth exposing rather than letting
|
|
|
+ // a script read X-Forwarded-For out of ctx.headers
|
|
|
+ // and believe it.
|
|
|
+ {"clientIp", clientIp}};
|
|
|
+ applyTriggerHeaders(trigger, req);
|
|
|
applyTriggerBody(trigger, req);
|
|
|
|
|
|
const RunnerRunResult r = d.runners->run(project, functionId, trigger);
|