Skip to content

Feature Request: Rate-limiting / throttling for error emails (and prevent missing .cfm requests from triggering onError) #4694

Done

Description

@michaeldiederich

Problem Description

In production environments (sendEmailOnError = true), CFWheels sends an error email for every unhandled exception in onError.

When a production site is scanned by automated vulnerability bots (requesting hundreds of non-existent paths like /press/?rest_route=/, /wp/index.cfm, /admin.cfm, etc.) or when a recurring runtime error occurs under high traffic, CFWheels sends hundreds or thousands of error emails within minutes. This floods developer inboxes, can trigger SMTP rate limits / reputation blocks, and adds unnecessary load to the server.

Part 1: Feature Request — Error Email Rate-Limiting / Throttling

It would be great to have built-in rate-limiting / throttling for error emails in $runOnErrorSendEmail().

Proposed Behavior & Settings

Instead of sending an email on every single failure, CFWheels could throttle error emails using an in-memory counter in the application.wheels scope:

  1. Configurable limits, e.g.:
    • set(errorEmailRateLimit = 3) (maximum number of emails allowed within the time window; 0 = unlimited / current behavior)
    • set(errorEmailRateWindowMinutes = 15) (or cooldown duration in minutes, e.g., 15–60 minutes)
  2. Per-Error Fingerprinting (Recommended) or Global Throttling:
    • Ideally, throttling should be tracked per error signature (e.g. a hash of exception.type + exception.message + top tagContext template/line), with an optional global cap. That way, a high-frequency error does not silence a completely unrelated critical exception occurring during the same window.
  3. Suppression Summary:
    • Once the cooldown window expires and the error occurs again, include a short notice in the next email (e.g., "Note: 142 occurrences of this error were suppressed in the last 15 minutes.").

Part 2: Related Issue — Bot scans of non-existent .cfm files trigger onError (500) instead of onMissingTemplate (404)

While investigating why bot scans triggered error emails in the first place, we noticed how requests like GET /press/?rest_route=/ end up in onError:

  1. The web server resolves /press/ via DirectoryIndex index.cfm to /press/index.cfm and forwards it to the CFML engine.
  2. In onRequestStart, $abortInvalidRequest() checks:
    local.applicationPath = Replace(ExpandPath("/wheels/Global.cfc"), "\", "/", "all");
    local.callingPath = Replace(GetBaseTemplatePath(), "\", "/", "all");
    if (
        !(GetFileFromPath(local.callingPath) == "runner.cfm")
        && ListLen(local.callingPath, "/") > ListLen(local.applicationPath, "/")
    )
    
    In a flat root installation where /wheels is at /<webroot>/wheels/Global.cfc (depth: 4 segments), any request to a 1st-level subdirectory like /<webroot>/press/index.cfm also has 4 segments (4 > 4 is false), and any non-existent root file like /<webroot>/login.cfm has 3 segments (3 > 4 is false).
  3. As a result, $abortInvalidRequest() does not intercept the request, and onRequest() executes:
    include "#arguments.targetpage#";
    
    which throws Page [/press/index.cfm] not found (missinginclude) inside onRequest(), triggering onError (HTTP 500 + error email) instead of onMissingTemplate (HTTP 404, no email).

If $abortInvalidRequest() (or onRequest()) also verified whether arguments.targetPage is an allowed entry point (index.cfm, rewrite.cfm, runner.cfm) or whether the target template actually exists before include, missing .cfm scans would cleanly route to onMissingTemplate (404) without triggering onError emails.

Activity

  1. bpamiri commented on Nov 14, 2026

    @bpamiri
    Collaborator

    Thanks for the detailed report — both parts are real, and they're separable.

    Part 2 (bug) is reproducible on current develop. When a missing physical .cfm reaches onRequest, $abortInvalidRequest()'s depth comparison doesn't catch it (/login.cfm and /press/index.cfm both compare false against the framework path in a flat install), so the include throws and onError renders the 500/fallback path — which is also what can send the email. The correct behavior is a 404 via onMissingTemplate, with no onError and no email. The fix is to validate that targetPage exists before the include (and set the 404 explicitly), while keeping real .cfm entry points working. I'm implementing that now with an HTTP-level regression test.

    Part 1 (throttling) is not covered by an existing setting or hook: sendEmailOnError is only on/off, onErrorCallbacks runs after the email, and excludeFromErrorEmail only redacts scopes. A backward-compatible opt-in looks feasible with per-fingerprint (type + message + first app stack frame) and global fixed-window caps defaulting to unlimited, a suppression count in the next email, and application-scope state under a lock. Two honest caveats: per-fingerprint caps alone won't stop unique bot paths (a global cap is needed), and application-scope state is per node, so exact caps on multi-server deployments need a shared store or provider-side limit.

    I'd like to land Part 2 as the bug fix, and treat Part 1 as a separate designed feature (settings names/defaults and multi-server semantics need a maintainer decision). I'll post the Part 2 PR here when it's green.

  2. bpamiri commented on Nov 14, 2026

    @bpamiri
    Collaborator

    Part 2 is fixed and shipped in #4702 (fdbae20e1): missing physical .cfm requests now return 404 via onMissingTemplate, with no onError fallback and no error email (the new HTTP-level MissingTemplateSpec fails on the old code and passes now).

    Reopening because Part 1 — error-email rate limiting — is still a separate feature decision, not shipped. My earlier comment explains the feasible shape (opt-in per-fingerprint + global fixed-window caps, suppression count, application-scope lock; per-node caveat on multi-server). It needs a maintainer call on settings/defaults before implementation.

  3. bpamiri commented on Nov 20, 2026

    @bpamiri
    Collaborator

    Update: the decision-ready implementation brief is written (settings, fingerprint definition, state/locking, code-path shape, tests, docs, MVP slice) and is with the maintainer for a yes/no on the defaults. Part 2 shipped earlier in #4702. Nothing is blocked on reporters; I will post the PR here once the defaults are approved.

  4. bpamiri commented on Dec 1, 2026

    @bpamiri
    Collaborator

    Maintainer decision: the Part 1 settings and defaults are approved as specified.

    setting range default meaning
    errorEmailRateLimit 0..1000 0 max emails for one fingerprint per window
    errorEmailRateWindowMinutes 1..1440 15 fixed window length
    errorEmailGlobalRateLimit 0..10000 0 max error emails across all fingerprints per window

    This changes nothing until an app opts in

    All three defaults preserve today's behaviour exactly. With both caps at 0 the throttle is bypassed and every configured error email is sent as it is now — so merging the implementation is behaviourally inert for every existing app. "Rate limit" in the title invites the assumption that this is a behaviour change; it is an opt-in point. An app turns it on explicitly:

    set(errorEmailRateLimit = 3);
    set(errorEmailGlobalRateLimit = 30);
    set(errorEmailRateWindowMinutes = 15);

    The global cap stays in the design

    errorEmailGlobalRateLimit is not redundant with the per-fingerprint cap, and it should not be cut later as duplication. The fingerprint is type + message + first application frame, so a bot walking unique paths produces a unique fingerprint per request — the per-fingerprint cap never fires. That is precisely the scenario in this issue. The global cap is the only thing that bounds it.

    The brief now lives in the repo

    The implementation brief was written to /tmp/issue-4694-throttle-brief.md and was destroyed by a host restart. It has been recovered from the authoring session and committed as .ai/wheels/design/error-email-throttling.md in #4719 — settings, fingerprint definition, state/locking, code-path shape, tests, docs and MVP slice, plus the approved status and the global-cap rationale above. Build from that file, not from a scratch path.

    Implementation will follow as its own PR against that brief. Part 2 (missing .cfm → 404 with no onError and no email) already shipped in #4702.

  5. bpamiri commented on Dec 1, 2026

    @bpamiri
    Collaborator

    Part 1 implemented in #4721, merged as c6fb3afe8aaad0dc76407fa416ba66a54f6f837c.

    What shipped

    The three approved settings, with the approved defaults — so an app that does nothing behaves exactly as before:

    setting range default
    errorEmailRateLimit (per-fingerprint cap) 0..1000 0
    errorEmailRateWindowMinutes 1..1440 15
    errorEmailGlobalRateLimit (global cap) 0..10000 0
    • Fingerprint = SHA-256 of type|message|first-application-frame, built from the root cause, skipping vendor/wheels frames, with no-frame fallback. Only the hash is stored.
    • State in application.wheels.errorEmailThrottle under a named exclusive lock, fixed window, 512-fingerprint cap with eviction — and fail-open: if the lock fails the email is still sent, because inside onError suppressing the email would be worse than the flood.
    • Suppressed occurrences are counted and summarised in the next allowed email (counts and window only, no exception text, no separate summary email).
    • Third-party surfaces updated: wheels/info settings list, the CLI settings inventory, the app template's config/settings.cfm as a commented example, consumer docs, and a changelog fragment.

    Verification

    • Behaviour (qa, Lucee 7.0.5.41 and Adobe 2023 / sqlite): defaults inert — 10 real sends produced 10 emails and no throttle state, and mutating the guard made the spec fail (226 pass/1 fail), so the property is enforced rather than assumed. Per-fingerprint cap 3 over 5 identical errors → 3 sent, with the summary counting the 2 suppressed. Global cap 3 with 6 unique fingerprints → 3 sent over 6 fingerprints, which is the proof the global cap is not redundant: unique-path traffic produces a unique fingerprint per request, so the per-fingerprint cap never fires.
    • Code (r2): conforms to the approved brief; no Cross-Engine Invariants hazard touched (no local. writes in catch, no finally loops, no function members on application, no closures) — which matters here because this code runs inside onError.
    • Delta (r2): lock warning fires once per application via a flag on application.wheels; errorEmailRateWindowMinutes now rejects 0 with a warning instead of silently clamping, while both caps still treat 0 as unlimited.

    One honest gap, tracked

    r2 found that the "warn once" spec asserts the flag but does not count log lines, so a regression that keeps the flag and removes the early return would still pass. The code is correct by inspection; the spec is a partial proof. That is being fixed separately rather than left as a claim of coverage.

    What this does not do

    Nothing is enabled by default, and there is no on/off switch — opting in is set(errorEmailRateLimit = …). A per-node deployment gets approximate global caps (stated, not solved). Part 2 (missing .cfm → 404 with no onError and no email) shipped earlier in #4702.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions