Appendix D: Filtering Proxy (nginx)
An nginx example that meets the Tier 4 proxy requirements.
This example meets the Tier 4.2 requirements using nginx with its JavaScript module (njs). Test it on a staging node before production, and run all the tests at the end, including the ones that try to sneak past the filter.
Install. The nginx-module-njs package comes from nginx's own repository, not from Ubuntu or Debian. Add it first by following the instructions at https://nginx.org/en/linux_packages.html, then:
sudo apt install nginx nginx-module-njsTLS certificate. The proxy serves HTTPS, so it needs a certificate and key for its host name (e.g. rpc.example.com). Get one from Let's Encrypt (certbot) or your provider, and put the files where ssl_certificate and ssl_certificate_key point below.
/etc/nginx/njs/rpc_filter.js
// Methods every authenticated client may call
var BASIC = [
"eth_chainId", "eth_blockNumber", "eth_getBalance", "eth_getCode",
"eth_getStorageAt", "eth_getTransactionCount", "eth_call", "eth_estimateGas",
"eth_gasPrice", "eth_maxPriorityFeePerGas", "eth_feeHistory",
"eth_getBlockByNumber", "eth_getBlockByHash", "eth_getLogs",
"eth_getTransactionByHash", "eth_getTransactionReceipt",
"eth_sendRawTransaction", "net_version", "web3_clientVersion"
];
// Extra methods for clients that need tracing
var TRACING = [
"debug_traceCall", "debug_traceTransaction",
"debug_traceBlockByNumber", "debug_traceBlockByHash"
];
// Which client (from the API key map in nginx.conf) may call what
var ACCESS = {
"wallet-backend": BASIC,
"indexer": BASIC.concat(TRACING)
};
// Only built-in tracers that can't run code from the request.
// Never add "muxTracer": it accepts other tracer names inside its settings,
// and the node treats any name it doesn't recognize as JavaScript code.
var SAFE_TRACERS = ["callTracer", "prestateTracer", "4byteTracer", "flatCallTracer"];
var MAX_BATCH = 50;
var MAX_DEPTH = 32;
// The node ignores upper/lower case in field names ("Tracer" is the same as
// "tracer"). This filter does not, so anything ambiguous is rejected below.
var TOP_FIELDS = ["jsonrpc", "id", "method", "params"];
var SENSITIVE = ["tracer", "tracerConfig"];
// True if every field name, at every level, means the same to the node as to us.
function unambiguous(v, depth) {
if (depth > MAX_DEPTH) return false;
if (Array.isArray(v)) {
for (var i = 0; i < v.length; i++) {
if (!unambiguous(v[i], depth + 1)) return false;
}
return true;
}
if (v === null || typeof v !== "object") return true;
var seen = Object.create(null);
var keys = Object.keys(v);
for (var j = 0; j < keys.length; j++) {
var k = keys[j];
if (!/^[\x20-\x7e]*$/.test(k)) return false; // unusual characters
var lower = k.toLowerCase();
if (seen[lower]) return false; // e.g. "tracer" and "Tracer"
seen[lower] = true;
for (var s = 0; s < SENSITIVE.length; s++) { // e.g. "TRACER" on its own
if (lower === SENSITIVE[s].toLowerCase() && k !== SENSITIVE[s]) return false;
}
if (!unambiguous(v[k], depth + 1)) return false;
}
return true;
}
function tracerAllowed(params) {
if (!Array.isArray(params)) return true;
for (var i = 0; i < params.length; i++) {
var p = params[i];
if (p && typeof p === "object" && p.tracer !== undefined) {
if (typeof p.tracer !== "string" || SAFE_TRACERS.indexOf(p.tracer) === -1) return false;
}
}
return true;
}
function allowed(client, req) {
if (!req || typeof req !== "object" || Array.isArray(req)) return false;
var fields = Object.keys(req);
for (var i = 0; i < fields.length; i++) {
if (TOP_FIELDS.indexOf(fields[i]) === -1) return false; // e.g. "Method"
}
if (!unambiguous(req, 0)) return false;
if (typeof req.method !== "string") return false;
var list = ACCESS[client];
if (!list || list.indexOf(req.method) === -1) return false;
if (!tracerAllowed(req.params)) return false;
return true;
}
function deny(r, status, why) {
// JSON.stringify keeps request text on one log line, so it can't fake log entries
r.error("rpc-filter: rejected client=" + r.variables.rpc_client +
" ip=" + r.remoteAddress + " reason=" + JSON.stringify(why) +
" body=" + JSON.stringify((r.requestText || "").slice(0, 2000)));
r.headersOut["Content-Type"] = "application/json";
r.return(status, '{"jsonrpc":"2.0","id":null,"error":{"code":-32601,"message":"request not allowed"}}');
}
function filter(r) {
var client = r.variables.rpc_client;
if (!client) return deny(r, 401, "no-or-bad-api-key");
var body;
try { body = JSON.parse(r.requestText); } catch (e) { return deny(r, 400, "bad-json"); }
var items = Array.isArray(body) ? body : [body];
if (items.length === 0 || items.length > MAX_BATCH) return deny(r, 403, "batch-size");
for (var i = 0; i < items.length; i++) {
if (!allowed(client, items[i])) return deny(r, 403, "method-or-tracer:" + (items[i] && items[i].method));
}
r.internalRedirect("@sonic");
}
export default { filter };/etc/nginx/nginx.conf (relevant parts)
Test it:
The next three requests try to slip past the filter. Every one must be rejected (403). If any of them returns 200, don't use the filter until you've found out why.

