Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion tools/nginx/relay-limit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,24 @@
# services' own limits (AUDIT CC-01). Goes in the http context: /etc/nginx/conf.d/crosschain-relay-limit.conf.
# The site's pages poll relay/receipt and relay/info about once a second while an operation is under way,
# so 10 requests a second with a burst of 40 leaves normal use untouched and only stops floods.
limit_req_zone $binary_remote_addr zone=crosschain_relay:10m rate=10r/s;
#
# The key: an IPv4 address as it is, an IPv6 address by its /64 (one subscriber usually holds a whole /64,
# so a key per address would not bind; audit 2026-11-24 R16). nginx prints $remote_addr compressed (RFC
# 5952), so each pattern below reads the first 64 bits from one way of writing it; generated and checked
# against the relayer's ipKey() on random addresses. An address none of them matches keeps its own key.
map $remote_addr $crosschain_relay_key {
"~^(?<a>[0-9a-f]{1,4}):(?<b>[0-9a-f]{1,4}):(?<c>[0-9a-f]{1,4}):(?<d>[0-9a-f]{1,4}):" "$a:$b:$c:$d";
"~^(?<p0>[0-9a-f]{1,4}):(?<p1>[0-9a-f]{1,4}):(?<p2>[0-9a-f]{1,4})::([0-9a-f]{1,4}(:[0-9a-f]{1,4}){0,3})?$" "$p0:$p1:$p2:0";
"~^(?<p0>[0-9a-f]{1,4}):(?<p1>[0-9a-f]{1,4})::(?<s0>[0-9a-f]{1,4}):(?<s1>[0-9a-f]{1,4}):(?<s2>[0-9a-f]{1,4}):(?<s3>[0-9a-f]{1,4}):(?<s4>[0-9a-f]{1,4})$" "$p0:$p1:0:$s0";
"~^(?<p0>[0-9a-f]{1,4}):(?<p1>[0-9a-f]{1,4})::([0-9a-f]{1,4}(:[0-9a-f]{1,4}){0,3})?$" "$p0:$p1:0:0";
"~^(?<p0>[0-9a-f]{1,4})::(?<s0>[0-9a-f]{1,4}):(?<s1>[0-9a-f]{1,4}):(?<s2>[0-9a-f]{1,4}):(?<s3>[0-9a-f]{1,4}):(?<s4>[0-9a-f]{1,4}):(?<s5>[0-9a-f]{1,4})$" "$p0:0:$s0:$s1";
"~^(?<p0>[0-9a-f]{1,4})::(?<s0>[0-9a-f]{1,4}):(?<s1>[0-9a-f]{1,4}):(?<s2>[0-9a-f]{1,4}):(?<s3>[0-9a-f]{1,4}):(?<s4>[0-9a-f]{1,4})$" "$p0:0:0:$s0";
"~^(?<p0>[0-9a-f]{1,4})::([0-9a-f]{1,4}(:[0-9a-f]{1,4}){0,3})?$" "$p0:0:0:0";
"~^::(?<s0>[0-9a-f]{1,4}):(?<s1>[0-9a-f]{1,4}):(?<s2>[0-9a-f]{1,4}):(?<s3>[0-9a-f]{1,4}):(?<s4>[0-9a-f]{1,4}):(?<s5>[0-9a-f]{1,4}):(?<s6>[0-9a-f]{1,4})$" "0:$s0:$s1:$s2";
"~^::(?<s0>[0-9a-f]{1,4}):(?<s1>[0-9a-f]{1,4}):(?<s2>[0-9a-f]{1,4}):(?<s3>[0-9a-f]{1,4}):(?<s4>[0-9a-f]{1,4}):(?<s5>[0-9a-f]{1,4})$" "0:0:$s0:$s1";
"~^::(?<s0>[0-9a-f]{1,4}):(?<s1>[0-9a-f]{1,4}):(?<s2>[0-9a-f]{1,4}):(?<s3>[0-9a-f]{1,4}):(?<s4>[0-9a-f]{1,4})$" "0:0:0:$s0";
"~^::([0-9a-f]{1,4}(:[0-9a-f]{1,4}){0,3})?$" "0:0:0:0";
default $binary_remote_addr;
}
limit_req_zone $crosschain_relay_key zone=crosschain_relay:10m rate=10r/s;
limit_req_status 429;
19 changes: 11 additions & 8 deletions tools/relayer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ Only operations that:
The relay transaction uses the operation's own fee fields, so the relayer never pays a higher price than it gets back.

Other limits:
- one pending operation per UP at a time: a UP stays blocked until its transaction is mined, and, if it is not mined within 3 minutes, while the EntryPoint has not used the operation's nonce, for up to 15 minutes (AUDIT CC-10);
- the relay transaction is signed and its hash recorded before it is broadcast: if the answer to the send is lost but the transaction reached the network, the page gets it, and the same operation sent again gets the same transaction, never a second one that would revert at the relayer's cost (AUDIT CC-10);
- rate limits (`limits` in the configuration): per IP (`perIpPerMinute`, default 6), for all clients together (`globalPerMinute`, default 60, before any RPC call), and per UP (`perSenderPerMinute`, default 3). The per-UP limit is charged only to operations that passed both simulations, so the UP's own signature: anyone can forge a request naming a UP, and such requests never use up that UP's quota (AUDIT CC-01);
- one pending operation per UP at a time, taken once the `handleOps` simulation has passed (so a forged operation never holds a UP's lock): a UP stays blocked until its transaction is mined, and, if it is not mined within 3 minutes, while the EntryPoint has not used the operation's nonce, for up to 15 minutes (AUDIT CC-10);
- the relay transaction is signed and written to a **record of sent transactions** before it is broadcast (AUDIT CC-10, audit 2026-11-24 R15): `relay-sends.json` in the service's state directory, `/var/lib/crosschain-relayer` (mode 600; `sendLogFile` in the configuration to put it elsewhere). If the answer to the send is lost but the transaction reached the network, the page gets it. If it is not found right after the error, its outcome is "unknown": the page gets a 502 that says it may have been sent, the UP stays blocked, and the same operation sent again gets that same signed transaction broadcast again, never a second one that would revert at the relayer's cost. A transaction is settled (and removed from the record) when it is mined, when its operation's nonce is used, or when another transaction of the relayer has used its nonce while it was never seen on the network (then it can never be mined: the UP is unblocked and the operation can be sent again); meanwhile one not seen is broadcast again. After a restart the record is read again: its transactions block their UPs and are settled as usual (dropped after 30 minutes). An unreadable record is moved aside (`relay-sends.json.unreadable-<time>`), never overwritten; the log says so, and its transactions can be looked up by hand on the explorer;
- rate limits (`limits` in the configuration): per IP (`perIpPerMinute`, default 6; an IPv6 address counts by its /64, audit 2026-11-24 R16), for all clients together (`globalPerMinute`, default 60, charged after the shape checks and the lookup of an operation already sent, before any RPC call), and per UP (`perSenderPerMinute`, default 3). The per-UP limit is charged only to operations that passed both simulations, so the UP's own signature: anyone can forge a request naming a UP, and such requests never use up that UP's quota (AUDIT CC-01);
- `GET /relay/info` is read from the chains at most every 10 seconds, whoever asks;
- nginx limits each IP to 10 requests a second (burst 40) on `/relay/` (step 6);
- nginx limits each IP (IPv6: each /64) to 10 requests a second (burst 40) on `/relay/` (step 6);
- the log keeps only the scheme and host of any URL (an RPC's path or query may hold a key; audit 2026-11-24 R17), the monitor's findings and emails too;
- requests up to 64 KB;
- the browser `Origin` must be the site's.

Expand Down Expand Up @@ -76,7 +77,7 @@ which node # e.g. /usr/bin/node: not under /root or /home (the service can
sudo useradd --system --no-create-home --shell /usr/sbin/nologin up-relayer
sudo git clone --filter=blob:none --no-checkout https://github.com/ChainIntegrate/Cross_Chain.git /opt/crosschain-relayer
cd /opt/crosschain-relayer
sudo git sparse-checkout set --no-cone '/tools/relayer/' '/usd-price.js' '/op-cost.js'
sudo git sparse-checkout set --no-cone '/tools/relayer/' '/tools/nginx/' '/usd-price.js' '/op-cost.js'
sudo git checkout main
cd tools/relayer && sudo npm ci --omit=dev --ignore-scripts
```
Expand Down Expand Up @@ -106,10 +107,10 @@ journalctl -u crosschain-relayer -n 20 # "chain 8453: ready (OP stack) …"

**6. Web server: pass `/relay/` to the service.** Only this path. The service listens on `127.0.0.1` only.

nginx: first the per-IP rate zone, once, in the http context (the content of `tools/nginx/relay-limit.conf`; the server's clone of the relayer holds only `tools/relayer`, so it is written directly):
nginx: first the per-IP rate zone, once, in the http context: `tools/nginx/relay-limit.conf` (an IPv6 address counts by its /64), copied from the relayer's clone. A clone made before 2026-11-24 holds only `tools/relayer`: add the folder first with `cd /opt/crosschain-relayer && sudo git sparse-checkout add /tools/nginx/`.

```bash
printf '%s\n' 'limit_req_zone $binary_remote_addr zone=crosschain_relay:10m rate=10r/s;' 'limit_req_status 429;' | sudo tee /etc/nginx/conf.d/crosschain-relay-limit.conf
sudo cp /opt/crosschain-relayer/tools/nginx/relay-limit.conf /etc/nginx/conf.d/crosschain-relay-limit.conf && sudo nginx -t && sudo systemctl reload nginx
```

then, inside the site's `server { … }` block:
Expand Down Expand Up @@ -196,7 +197,7 @@ sudo systemctl stop crosschain-relayer # the page shows "not reachable" and B

`--ignore-scripts`: no dependency of the relayer runs an install script, so none is run as root (AUDIT CC-08).

**The units** (AUDIT CC-08, 2026-11-24): both run as `up-relayer` with systemd's sandboxing (no capabilities, a system-call filter, no namespaces, a private `/proc` view, `UMask=0077`). The relayer cannot read the SMTP password (`smtp.env`); the monitor cannot read the relayer's key. When a commit changes them, install them again and check:
**The units** (AUDIT CC-08, 2026-11-24): both run as `up-relayer` with systemd's sandboxing (no capabilities, a system-call filter, no namespaces, a private `/proc` view, `UMask=0077`). The relayer cannot read the SMTP password (`smtp.env`); the monitor cannot read the relayer's key. Both write only in `/var/lib/crosschain-relayer` (mode 700): the monitor its state, the relayer its record of sent transactions (from 2026-11-24, R15). When a commit changes them, install them again and check:

```bash
cd /opt/crosschain-relayer/tools/relayer && sudo cp crosschain-relayer.service crosschain-relayer-monitor.service /etc/systemd/system/ && sudo systemctl daemon-reload && sudo systemctl restart crosschain-relayer
Expand Down Expand Up @@ -225,6 +226,8 @@ Tested on a local chain with the real EntryPoint v0.6, the LUKSO `UniversalProfi
- each refusal case above is rejected without any transaction;
- the start-up checks: key file mode, wrong RPC chain, missing paymaster.

`npm test` (`test/limits.js`, against a mocked chain, 32 of 32): the rate limits and their order (CC-01; an IPv6 /64 sharing one per-IP limit, refused shapes not using the global one, no lock for a forged operation: R16), a lost answer on sending and the UP kept blocked (CC-10), the record of sent transactions (R15: an unknown outcome kept and the same transaction broadcast again, the record read again after a restart, a transaction that never reached the network settled, an unreadable record moved aside), and URLs cut to their host in the log (R17).

The page test (`up-gas-relay-admin.html` in Chromium, with this service behind `relay/`) passes 63 of 63 on each version.

The monitor test (44 of 44) runs it against a UP laid out like the Base ones, with the real contracts at their real addresses and a fake SMTP server: no finding and no email on a correct setup; an EntryPoint given SETDATA is an error and sends one email, no repeat on the next run, a reminder after 24 hours, "all clear" once fixed; extension elsewhere, leftover extension permission, no 4337 signer, UP off the allowlist, low deposit, low relayer balance, relayer down and wrong paymaster owner are all reported; `--test-email` and `--dry-run` work. With a sponsor paymaster: a UP off the allowlist but accepted by the signing service gives no finding; a refusal by the service or no answer is a warning; signer zero, an unexpected signer, cap 0 and other code are errors; a low deposit is a warning. Drawdown: normal use gives no finding; a fall of more than 2 caps, or of more than half a small deposit, is a problem; a top-up or a first reading is not; the thresholds come from the config; end to end, the fall is emailed at the next run and not repeated after.
5 changes: 4 additions & 1 deletion tools/relayer/crosschain-relayer.service
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ WorkingDirectory=/opt/crosschain-relayer/tools/relayer
ExecStart=/usr/bin/env node relay.js serve --config /etc/crosschain-relayer/config.json
Restart=on-failure
RestartSec=5
# Hardening: the service reads its code and its config, and writes nothing.
# Hardening: the service reads its code and its config, and writes only its record of sent transactions
# (relay-sends.json, audit 2026-11-24 R15) in /var/lib/crosschain-relayer, shared with the monitor's state.
StateDirectory=crosschain-relayer
StateDirectoryMode=0700
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
Expand Down
6 changes: 4 additions & 2 deletions tools/relayer/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
const fs = require("fs");
const path = require("path");
const { ethers } = require("ethers");
const { readConfig, ENTRY_POINT } = require("./relay.js");
const { readConfig, ENTRY_POINT, redact } = require("./relay.js");
// USD value of each chain's gas token (Chainlink feeds; the same file the operator's page uses, at the
// repository's root). If it is missing (a sparse clone without /usd-price.js), the monitor keeps working
// with the native-amount rule and reports the missing file instead of stopping.
Expand Down Expand Up @@ -325,6 +325,8 @@ async function main(argv) {
const deposits = {}, balances = {};
const findings = await runChecks(config, deposits, balances);
findings.push(...drawdowns(state.deposits, deposits, config.monitor || {}));
// URLs in errors keep only scheme and host (an RPC URL may hold a key): in the log and in the email (R17).
for (const f of findings) f.msg = redact(f.msg);
const { subject, text } = report(findings, balances);
console.log(subject + "\n" + text);
if (has("--dry-run")) return findings.some((f) => f.level === "error") ? 2 : 0;
Expand All @@ -346,7 +348,7 @@ async function main(argv) {
}

if (require.main === module) {
main(process.argv.slice(2)).then((code) => process.exit(code)).catch((e) => { console.error(e.message || e); process.exit(1); });
main(process.argv.slice(2)).then((code) => process.exit(code)).catch((e) => { console.error(redact(e.message || e)); process.exit(1); });
}

module.exports = { runChecks, report, main, drawdowns };
Loading
Loading