惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
B
Blog
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
博客园 - Franky
V
V2EX
IT之家
IT之家
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
F
Fortinet All Blogs
I
InfoQ
云风的 BLOG
云风的 BLOG
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
The Tracking-Link Bug That Only Breaks Signed URLs
Nasrul Hazim Bin Mohamad · 2026-06-26 · via DEV Community

Here's a bug with a great property: it works perfectly for almost every link in your email, and silently breaks exactly the ones that matter most — the signed ones. Verify-email links. Signed download URLs. The links where a single wrong character means Laravel rejects the whole request. I shipped a one-line fix for it today in mail-history, and the why is more interesting than the diff.

The setup: self-hosted click tracking

mail-history does self-hosted open/click tracking. No third-party pixel service — it rewrites your outgoing email's HTML so every <a href> routes through a redirect endpoint first. The redirect records the click, then forwards the user to the real destination.

To survive the round trip, the original URL is encrypted into the tracking link and decrypted on the way out. The rewrite lives in one trait shared by both the Mailable concerns and the injection listener, so the logic exists in exactly one place:

protected function rewriteClickLinks(string $html, string $hash): string
{
    if ($hash === '' || $html === '') {
        return $html;
    }

    $excludePatterns = (array) config('mailhistory.tracking.click.exclude_patterns', ['*unsubscribe*']);

    return (string) preg_replace_callback(
        '/<a\s([^>]*?)href=["\']([^"\']+)["\']/i',
        function ($matches) use ($hash, $excludePatterns) {
            $attributes  = $matches[1];
            $originalUrl = $matches[2]; // <-- the bug lives here

            // skip mailto:/tel:/#/javascript: and excluded patterns...

            $trackingUrl = route('mailhistory.tracking.click', [
                'hash' => $hash,
                'url'  => Crypt::encryptString($originalUrl),
            ]);

            return '<a '.$attributes.'href="'.htmlspecialchars($trackingUrl).'"';
        },
        $html
    );
}

Read that $originalUrl = $matches[2] line again. We're pulling the URL straight out of rendered HTML.

The trap: rendered HTML is escaped HTML

By the time the trait sees the email body, Blade and Laravel's mail templates have already done their job — and part of that job is HTML-escaping attribute values. An ampersand in an href doesn't stay an ampersand. It becomes &amp;.

For a normal link that's invisible, because the browser decodes it right back. But we're not handing this to a browser. We're calling Crypt::encryptString() on the raw captured string. So when Laravel renders a signed URL:

https://example.com/email/verify/1/abc?expires=123&signature=deadbeef

what actually sits in the href attribute is:

https://example.com/email/verify/1/abc?expires=123&amp;signature=deadbeef

We encrypt that&amp; and all. On click, the redirect decrypts it and forwards the user to a URL whose query string is ?expires=123&amp;signature=deadbeef. Laravel's signed-URL validation recomputes the signature over the query string it sees, which now contains a literal amp; that was never part of what it signed. Verification fails.

The cruel part is the failure mode. Plain marketing links — https://example.com/page — have no query string, no ampersand, nothing to escape, so they pass straight through and look healthy. Everything appears fine in a quick test. It's only the signed URLs, the ones carrying expires and signature separated by &, that quietly die. The bug targets your most security-sensitive links and leaves the rest alone.

The fix: decode before you capture

Decode HTML entities the moment you read the URL, before anything downstream touches it:

// Decode HTML entities (e.g. &amp; -> &) captured from the rendered
// href so signed-URL query strings survive the encrypt/redirect round
// trip. Without this, "?expires=..&amp;signature=.." is redirected
// verbatim and breaks Laravel signature validation.
$originalUrl = html_entity_decode($matches[2], ENT_QUOTES | ENT_HTML5);

ENT_QUOTES | ENT_HTML5 so it handles both quote styles and the full HTML5 entity set, not just the basic four. Now &amp; becomes & before encryption, the decrypted redirect target matches the originally-signed URL byte for byte, and signature validation passes.

One line. The whole bug was a mismatch between where you read a value (rendered, escaped HTML) and what you assumed it was (a raw URL).

Pin it with a test

A one-line fix like this is exactly the kind that gets silently reverted by a future "cleanup" refactor. So it gets a Pest test that asserts the real symptom — the decrypted URL contains a real &, not &amp;:

it('decodes HTML entities in the tracked URL so signed query strings survive', function () {
    // As rendered by Laravel's mail templates, the ampersand in a signed URL
    // is HTML-escaped to &amp; inside the href attribute.
    $email = (new Email)->html(
        '<html><body><a href="https://example.com/email/verify/1/abc?expires=123&amp;signature=deadbeef">Verify</a></body></html>'
    );

    $rewritten = rewriteFixture($email, hash: 'abc123');

    preg_match('/url=([^"&]+)/', $rewritten, $m);
    $decrypted = Crypt::decryptString(urldecode($m[1]));

    expect($decrypted)
        ->toBe('https://example.com/email/verify/1/abc?expires=123&signature=deadbeef')
        ->not->toContain('&amp;');
});

The assertion is doing two jobs: it confirms the happy path (decrypts back to the exact signed URL) and it explicitly forbids the regression (not->toContain('&amp;')). If someone deletes the html_entity_decode call six months from now, this test goes red and tells them why — the comment is right there in the test.

The takeaway

Any time you extract data from rendered HTML and feed it into something that isn't a browser — encryption, signing, a redirect, a webhook — assume it's been entity-escaped and decode first. The browser would have forgiven you. Crypt::encryptString() won't. And when the fix is a single line, spend the extra ten minutes on a test that names the exact failure, because one-liners are the easiest changes for a future cleanup to quietly undo.