Commit 210bfde
authored
Fix: phishing controller c2 optimization (#6388)
## Explanation
`isMaliciousC2Domain` previously checked each request against the C2
blocklist using `Array.includes`, which performs a linear O(n) scan of
hashed domain strings on every call. For a list in the
thousands-of-entries range, each call scans the full list up to 6 times
— once for the exact hostname hash, up to 5 times for parent domain
hashes.
This PR switches the internal representation to `Set<string>`, reducing
each of those scans to an O(1) hash lookup.
**Benchmark results (internal profiling):**
- Per-request time: 2–8ms → 0.05–0.5ms (~10–100x improvement depending
on page complexity; ~50x averaged across pages)
- Potential CPU savings: up to ~300ms per page load
- Background tabs also benefit — confirmed they generate significant
background network requests that all route through this check
**Implementation notes:**
- The conversion from `string[]` → `Set<string>` happens once at
construction time via a new unexported
`InternalPhishingDetectorConfiguration` type. The public
`PhishingDetectorConfiguration` type keeps `c2DomainBlocklist?:
string[]` unchanged — no API break for downstream consumers.
- `isMaliciousC2Domain` now uses `.size` / `.has` in place of `.length`
/ `.includes`
- `getDefaultPhishingDetectorConfig` accepts and threads
`c2DomainBlocklist` through as `string[]`; `processConfigs` no longer
performs a redundant intermediate `Set` construction
Fixes: MetaMask/MetaMask-planning#5611
## References
- Fixes MetaMask/MetaMask-planning#5611
## Checklist
- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs),
highlighting breaking changes as necessary
- [x] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Low Risk**
> Behavior-preserving internal data-structure change on a hot path;
public types and matching logic are unchanged.
>
> **Overview**
> **C2 domain blocklist checks** in
`PhishingDetector.isMaliciousC2Domain` now use a `Set` internally
instead of scanning a `string[]` with `includes`, so each hostname and
parent-domain hash lookup is O(1) rather than O(n) (up to several
lookups per request).
>
> Arrays from config are converted to `Set<string>` once in the
constructor via an internal `InternalPhishingDetectorConfiguration`
type; the exported `PhishingDetectorConfiguration` still exposes
`c2DomainBlocklist?: string[]`. `getDefaultPhishingDetectorConfig` now
accepts and forwards an optional `c2DomainBlocklist` override. The
changelog records the performance change.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
0efe622. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent a949073 commit 210bfde
3 files changed
Lines changed: 39 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
259 | 260 | | |
260 | 261 | | |
261 | 262 | | |
| 263 | + | |
262 | 264 | | |
263 | 265 | | |
264 | 266 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
55 | 54 | | |
| 55 | + | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
60 | 67 | | |
61 | | - | |
| 68 | + | |
62 | 69 | | |
63 | 70 | | |
64 | 71 | | |
| |||
75 | 82 | | |
76 | 83 | | |
77 | 84 | | |
78 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
79 | 89 | | |
80 | 90 | | |
81 | 91 | | |
82 | 92 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
89 | 102 | | |
90 | 103 | | |
91 | 104 | | |
| |||
296 | 309 | | |
297 | 310 | | |
298 | 311 | | |
299 | | - | |
| 312 | + | |
300 | 313 | | |
301 | 314 | | |
302 | 315 | | |
303 | | - | |
| 316 | + | |
304 | 317 | | |
305 | 318 | | |
306 | 319 | | |
| |||
311 | 324 | | |
312 | 325 | | |
313 | 326 | | |
314 | | - | |
| 327 | + | |
315 | 328 | | |
316 | 329 | | |
317 | 330 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
231 | 231 | | |
232 | 232 | | |
233 | 233 | | |
| 234 | + | |
234 | 235 | | |
235 | 236 | | |
236 | 237 | | |
237 | 238 | | |
238 | 239 | | |
| 240 | + | |
239 | 241 | | |
240 | 242 | | |
241 | 243 | | |
242 | 244 | | |
243 | 245 | | |
| 246 | + | |
244 | 247 | | |
245 | 248 | | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
256 | 258 | | |
257 | 259 | | |
258 | 260 | | |
| |||
0 commit comments