DEV Community

Cover image for Shorty v3.1.0: phishing links that take themselves down
Muhammad Sheharyar Butt
Muhammad Sheharyar Butt

Posted on Originally published at msyb.dev AI-assisted

Shorty v3.1.0: phishing links that take themselves down

In August I audited Shorty, my open-source URL shortener, and
wrote up the ten findings.
Fixing them made the moderation console correct. It did not make it fast. Every bad
link still waited for a person: someone had to report it, enough people had to report it
to cross the auto-flag threshold, or I had to sign in and block it myself.

That is a real problem for a link shortener, because short links are a phishing kit's
favourite accessory. They hide the destination, and the destination can change after the
link is shared. The page is harmless when someone shortens it, then the attacker swaps in
a fake login form. Waiting for reports means waiting for victims.

Shorty v3.1.0 closes that gap. It also comes with a redesigned public site that ships
half the JavaScript.

Try Shorty ·
Source on GitHub ·
Changelog

Asking services that actually crawl the web

Shorty already scored links on how they look: high-abuse TLDs, raw IP addresses, punycode
lookalikes, credentials
embedded in the URL. That cannot tell you whether a page is hosting a credential
harvester today. Two free services can:

  • Google Safe Browsing v4 covers phishing, malware and unwanted software. It accepts 500 URLs per request, which makes it the workhorse for scheduled scans.
  • URLhaus by abuse.ch tracks malware distribution URLs, one URL per request.

Each provider switches on when its key is present. With no keys set, nothing changes,
which matters for anyone self-hosting. There is no SDK either. Each provider is a single
POST, and an SDK would add a dependency tree to a serverless bundle for no extra
capability.

const response = await fetch(SAFE_BROWSING_ENDPOINT, {
  method: 'POST',
  // Header rather than `?key=`, so the key never lands in a proxy or access log.
  headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': env.SAFE_BROWSING_API_KEY! },
  body: JSON.stringify({ /* up to 500 threat entries */ }),
  signal: AbortSignal.timeout(timeoutMs),
});
💬 ชาวเน็ตแห่แชร์: shehari/shorty v phishing links that take themselves down aa - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shehari/shorty v phishing links that take themselves down aa - สรุปดราม่าล่าสุด

One URLhaus detail is worth copying. An entry marked offline is a takedown that has
already happened. Blocking it forever would punish a site that has been cleaned up, so
offline counts as clean.

Three triggers, no moderator

Trigger When it runs What happens
creation Someone shortens a URL A listed destination is refused with 403 URL_BLOCKED, and existing links to it come down too
report An abuse report arrives The destination is checked immediately; a confirmed threat comes down now, not after N more reports
scan On a schedule Live, recently used links are re-checked; any that have turned bad are taken down

The order of the creation check matters more than it looks. Shorty de-duplicates: shorten
a URL that is already in the system and you get the existing short code back. If the
threat check ran after that reuse, a destination that went bad after its first link was
created would keep being handed out to every new visitor. So it runs first:

// Checked before reuse, so an existing link to a destination that has since
// gone bad is taken down here rather than handed out again.
const { match: threat } = await checkUrl(destination);
if (threat) {
  await quarantineDestination(urlHash, threat, 'creation');
  throw new AppError(403, 'URL_BLOCKED',
    `${sourceLabel(threat.source)} lists this destination as phishing or malware, so it cannot be shortened.`);
}
💬 ชาวเน็ตแห่แชร์: shehari/shorty v phishing links that take themselves down aa - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shehari/shorty v phishing links that take themselves down aa - สรุปดราม่าล่าสุด

Every takedown marks the link blocked and flagged, keeps any note a moderator already
wrote, and adds a link.auto_blocked entry to the audit log with no human actor, plus
the source, the threat type and the host. The console shows what the system did and why,
and a moderator can reverse it.

Shorty's report page: confirmed threats now come down as soon as the report is checked

Failing open, deliberately, exactly once

In the audit I criticised Shorty's blocklist for failing open. Now I am doing it on
purpose, so the reasoning has to hold up.

If Safe Browsing or URLhaus is slow or down (the budget is 2.5 seconds), link creation
goes ahead and the miss is logged. An outage at Google should not take my shortener down
with it. The difference from the old bug is what happens next. Back then, nothing ever
re-checked a link, so one failure was permanent. Now the scheduled scan re-checks
everything that is in use, so a miss is bounded by the next scan.

The verdict cache follows the same rule. Clean results are cached for 30 minutes and
threats for 6 hours, but only when every provider answered. A clean result from half the
providers is not a clean result.

The scan

A scan only looks at links that are live and in use, meaning clicked or created in the
last 30 days, most recently active first, up to 1,000 destinations per run. A link nobody
has clicked in a month harms nobody, and its next click puts it straight back into the
window. Links are grouped by destination, so a URL shortened fifty times costs one lookup
rather than fifty, and URLhaus, at one request per URL, only gets the 100 most active.

There are two schedulers and one code path:

  • On Vercel, a daily cron calls GET /api/cron/threat-scan with Authorization: Bearer $CRON_SECRET, compared in constant time. With no secret configured the endpoint returns 404: it does not exist, rather than existing and being open.
  • On npm start, an in-process timer runs every 60 minutes, starting 30 seconds after boot, and is unref()'d so it never keeps a shutting-down process alive.

If a second scan is triggered while one is running, it joins the first instead of
starting another, which would otherwise write every takedown to the audit log twice:

let inFlight: Promise<ThreatScanSummary> | null = null;

export function runThreatScan(): Promise<ThreatScanSummary> {
  inFlight ??= scan().finally(() => {
    inFlight = null;
  });
  return inFlight;
}
💬 ชาวเน็ตแห่แชร์: shehari/shorty v phishing links that take themselves down aa - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shehari/shorty v phishing links that take themselves down aa - สรุปดราม่าล่าสุด

The new code comes with its own tests: Safe Browsing batching, keeping the API key out of
the URL, URLhaus offline handling, provider failures, and the rule that a partial answer
is never cached.

A lighter, calmer public site

The other half of the release is visual. Version 3.0 used Ant Design everywhere, public
pages included, and it looked like a template. Version 3.1 has its own identity: paper
and ink, Schibsted Grotesk with IBM Plex Mono for every URL and number, a single signal
colour, and a new logo mark.

The redesigned Shorty home page in dark mode

More importantly, Ant Design now loads only behind /admin:

// app/admin/layout.tsx: neither Ant Design nor the console styles ship to public pages.
export default function AdminLayout({ children }: { children: ReactNode }) {
  return <AntdProvider>{children}</AntdProvider>;
}
💬 ชาวเน็ตแห่แชร์: shehari/shorty v phishing links that take themselves down aa - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shehari/shorty v phishing links that take themselves down aa - สรุปดราม่าล่าสุด

The public components became plain HTML. The FAQ is a set of native <details> elements
that share a name, so the browser keeps only one open at a time, it works without
JavaScript, and every answer is in the served HTML. The QR dialog is a native <dialog>,
which brings focus trapping, Esc to close and the backdrop for free. Forms are
native with inline validation. The QR renderer and the analytics chart are loaded with
next/dynamic only when someone opens them.

The result: gzipped JavaScript on the home page dropped from 390 KB to 194 KB, the
terms page from 311 KB to 189 KB, and HTML is three to five times smaller.

Upgrading

There is no database migration. Everything new is opt-in on the API:

  1. Set SAFE_BROWSING_API_KEY and/or URLHAUS_AUTH_KEY to switch on the checks.
  2. On Vercel, set CRON_SECRET. The bundled vercel.json already schedules the daily scan.
  3. Self-hosted with npm start, scans run every THREAT_SCAN_INTERVAL_MIN minutes (60 by default, 0 to disable).

What I would tell past me

A moderation console is for the decisions a machine cannot make. If a feed maintained by
people who crawl the web all day can already answer "is this phishing?", asking a human to
answer it again just adds latency, and with phishing, latency is measured in victims.

Shorty is free, open source and MIT licensed. Shorten a link,
read the code, or
report one that is being abused.

Top comments (0)