diff --git a/FILTER.md b/FILTER.md index 6a717808..5c5cae57 100644 --- a/FILTER.md +++ b/FILTER.md @@ -60,7 +60,7 @@ Each rule holds: | `hops` | Flood path length (number of path hashes); direct traffic never matches | | `len` | Payload length in bytes | | `snr` | Received SNR in dB (signed, quarter-dB resolution) | -| `path` | Sliding-window match of 1–4 adjacent repeater pubkey-hash prefixes in the flood path | +| `path` | Sliding-window match of 1–4 adjacent repeater pubkey-hash prefixes in the flood path (`^`/`$` anchor first/last entry; both anchors = whole path) | | `hsize` | Path hash size (1–4 bytes) used by the packet | | `chan` | Keyed channel match (identity proven by decryption) | | `chanhash` | Single 1-byte on-air channel hash | @@ -225,7 +225,7 @@ quotes is rejected) — work around one with `.` if a message text ever includes | `hops` | interval | Flood hop count (see interval syntax, §2) | | `len` | interval | Payload length, bytes | | `snr` | interval, signed dB | Packet SNR (e.g. `snr=[-100,-5]`, `snr=-2.25`) | -| `path` | `[^]HEX>HEX>…[$]` | Adjacent flood-path hash prefixes; `^` anchors first, `$` anchors last; 2–8 hex chars per entry, up to 4 entries | +| `path` | `[^]HEX>HEX>…[$]` | Adjacent flood-path hash prefixes; `^` anchors first, `$` anchors last, both together require the chain to span the whole path (e.g. `path=^10$` = 1-hop only); 2–8 hex chars per entry, up to 4 entries | | `hsize` | `1..4` (comma-combine) | Path hash size the packet carries | | `chan` | channel name(s) | Keyed store match, decryption-proven; `#` names auto-add | | `chanhash` | 2 hex chars | Bare 1-byte on-air channel hash (no decryption proof) | @@ -420,7 +420,9 @@ port is only needed for initial flashing and emergencies. with these predicates never fires on binary group data. - `chanhash` is a convenience for key-less cases; prefer keyed `chan` matching. - Regex evaluations are step-budgeted and fail open (see §2); watch the `aborted` - counter if you use heavy patterns like `.*.*.*`. + counter if you use heavy patterns like `.*.*.*`. Patterns are also rejected at + add time if they exceed the engine's 30-symbol compile budget or their storage + length (sender 24 / text 48 chars) — a too-long regex never silently truncates. - All counters (`hits`, `limiter`, `aborted`) and the advert cache are memory-only: they reset on reboot, and the cache can also be emptied via `filter ratelimit clear`. - Reply strings are capped at 160 bytes (remote CLI buffer); `filter list` shows a diff --git a/examples/simple_repeater/PacketFilter.cpp b/examples/simple_repeater/PacketFilter.cpp index 0ed7114b..5df7f782 100644 --- a/examples/simple_repeater/PacketFilter.cpp +++ b/examples/simple_repeater/PacketFilter.cpp @@ -266,6 +266,9 @@ static bool pathMatches(const FilterRule* r, const mesh::Packet* pkt) { switch (r->path.pos) { case FILTER_PATH_FIRST: start_min = 0; start_max = 0; break; case FILTER_PATH_LAST: start_min = n - r->path.count; start_max = start_min; break; + case FILTER_PATH_FIRST | FILTER_PATH_LAST: // ^A$ / ^A>B$: whole path only + if (n != r->path.count) return false; + break; // start_min == start_max == 0 default: break; // FILTER_PATH_ANY: every window position } for (int w = start_min; w <= start_max; w++) { @@ -625,6 +628,8 @@ static bool parseHexHash(const char* s, uint8_t* out, uint8_t* out_len) { } // path=^HEX>HEX>...>HEX$ (up to FILTER_PATH_HASH_SLOTS adjacent hashes) +// '^' anchors the first entry, '$' the last; both together mean the rule's +// chain must span the whole path (e.g. path=^10$ matches 1-hop paths only) static bool parsePath(const char* tok, FilterRule* r) { r->path.count = 0; r->path.pos = FILTER_PATH_ANY; @@ -636,7 +641,7 @@ static bool parsePath(const char* tok, FilterRule* r) { if (tlen == 0 || tlen >= sizeof(buf)) return false; memcpy(buf, s, tlen + 1); if (buf[tlen - 1] == '$') { - r->path.pos = FILTER_PATH_LAST; + r->path.pos |= FILTER_PATH_LAST; buf[tlen - 1] = 0; } @@ -935,12 +940,12 @@ static void cliGet(FilterRules& filter, int idx, char* reply) { if (r->len.flags) { formatInterval(r->len, ivs, sizeof(ivs), false); radd(&out, &remain, " len=%s", ivs); } if (r->snr.flags) { formatInterval(r->snr, ivs, sizeof(ivs), true); radd(&out, &remain, " snr=%s", ivs); } if (r->path.count) { - radd(&out, &remain, " path=%s", r->path.pos == FILTER_PATH_FIRST ? "^" : ""); + radd(&out, &remain, " path=%s", (r->path.pos & FILTER_PATH_FIRST) ? "^" : ""); for (int e = 0; e < r->path.count; e++) { radd(&out, &remain, "%s", e ? ">" : ""); for (int b = 0; b < r->path.len[e]; b++) radd(&out, &remain, "%02X", r->path.bytes[e][b]); } - radd(&out, &remain, "%s", r->path.pos == FILTER_PATH_LAST ? "$" : ""); + radd(&out, &remain, "%s", (r->path.pos & FILTER_PATH_LAST) ? "$" : ""); } if (r->hash_size_mask) { radd(&out, &remain, " hsize="); @@ -982,8 +987,10 @@ static void cliStats(FilterRules& filter, char* reply) { static bool cliRuleIdx(FilterRules& filter, char* arg, int& idx, char* reply) { if (arg == NULL || arg[0] < '0' || arg[0] > '9') { strcpy(reply, "Err - rule index required"); return false; } - idx = atoi(arg); - if (idx < 0 || idx >= filter.getNumRules()) { strcpy(reply, "Err - no such rule"); return false; } + char* end; + long v = strtol(arg, &end, 10); // full token must be consumed ("0x10" is not an index) + if (*end != 0 || v < 0 || v >= filter.getNumRules()) { strcpy(reply, "Err - no such rule"); return false; } + idx = (int)v; return true; } diff --git a/examples/simple_repeater/TinyRegex.cpp b/examples/simple_repeater/TinyRegex.cpp index af29b974..4346b81d 100644 --- a/examples/simple_repeater/TinyRegex.cpp +++ b/examples/simple_repeater/TinyRegex.cpp @@ -163,7 +163,11 @@ re_t re_compile(const char* pattern) } break; } } - /* '\\' as last char in pattern -> invalid regular expression. */ + else + { + /* '\\' as last char in pattern -> invalid regular expression. */ + return 0; + } } break; /* Character class: */ @@ -235,6 +239,12 @@ re_t re_compile(const char* pattern) i += 1; j += 1; } + if (pattern[i] != '\0') + { + /* pattern exceeds MAX_REGEXP_OBJECTS symbols -> reject rather than + silently compiling a truncated pattern (which would over/under-match) */ + return 0; + } /* 'UNUSED' is a sentinel used to indicate end-of-pattern */ re_compiled[j].type = UNUSED;