Skip to main content

SSRF, Uploads, Webhooks, Rate Limits & Abuse Defense

Once Next.js code runs on the server, it can reach resources the browser cannot.

That is usefulβ€”and dangerous.

Server-side capabilities include:

internal network access
database access
cloud metadata endpoints
provider APIs
filesystem/runtime resources
private credentials
high-trust network paths

Any attacker-controlled input that influences those capabilities deserves explicit threat modeling.

SSRF mental model​

Server-Side Request Forgery happens when an attacker influences a server-side request target.

Vulnerable pattern:

export async function POST(request: Request) {
const { url } = await request.json()
const response = await fetch(url)
return new Response(await response.text())
}

The browser may be unable to reach internal systems, but your server may reach them on the attacker's behalf.

High-risk SSRF targets​

Depending on infrastructure:

localhost
private RFC1918 networks
cloud metadata services
internal admin panels
service discovery names
Kubernetes/internal DNS
unix/socket-adjacent proxies
redirect chains that end internally

Do not rely on β€œthe URL looks normal.”

Prefer allow-lists​

Strongest design:

user selects provider ID
β†’ server maps provider ID to trusted base URL
β†’ server constructs allowed path

rather than:

user supplies arbitrary URL
β†’ server fetches it

If arbitrary external URLs are a product requirement, build a dedicated hardened fetch service with clear policy.

URL validation is more than string prefixes​

This is not enough:

url.startsWith('https://example.com')

because URL parsing, credentials, ports, encoding, redirects, and subdomain tricks complicate string checks.

Use the URL parser and validate:

  • protocol
  • hostname
  • port
  • path policy
  • redirect behavior
  • resolved address policy where needed

Redirects matter​

Even if the first URL is allowed, a server may follow a redirect to a disallowed target.

Control redirect behavior and validate each hop when building sensitive fetch systems.

Phase 12 covered Next.js image optimizer redirect bounds and private-IP protections. The same threat model applies to custom server-side fetchers.

DNS rebinding and resolution​

For high-risk URL fetch services, validating only the hostname string may not be enough.

A hardened design may need to validate resolved IP ranges and re-check behavior across redirects/connections.

This is infrastructure/security engineering beyond a normal route helper.

Image optimizer as a fetch boundary​

next/image remote optimization fetches external resources server-side.

Next.js provides security controls including:

  • remotePatterns
  • localPatterns
  • local/private IP blocking by default
  • redirect limits
  • source response-size controls

Treat source allow-lists as security configuration, not convenience configuration.

Avoid broad patterns such as accepting any remote host unless the product truly requires it.

Custom image loaders​

A custom loader changes who owns source validation.

If your loader builds a third-party CDN URL from user input, validate:

source identifier
path
transformation parameters
quality/width bounds
signature policy

Do not turn the loader into an open proxy.

Upload threat model​

Uploads are untrusted binary input.

Risks include:

oversized files
malformed parsers
content-type spoofing
active SVG/HTML content
malware
archive bombs
image decompression bombs
path traversal
filename injection
storage abuse
public-content XSS

File extension is not enough​

An uploaded profile.jpg can contain data that does not match the extension.

Validate according to use case:

  • accepted media type
  • magic bytes/file signature where appropriate
  • parser/decode success
  • dimensions
  • size
  • filename/path handling
  • active content policy

Do not trust only Content-Type supplied by the client.

Prefer direct object-storage upload for large files​

Architecture:

browser
↓ request upload authorization
Next.js server
↓ authorize user + object key + size/type policy
object storage signed upload URL
↓
browser uploads directly

Benefits:

  • avoids routing large bytes through app compute
  • reduces memory/time pressure
  • keeps storage policy explicit
  • scales better under concurrent uploads

The app must still validate final object state before treating it as trusted content.

Signed upload URLs are capabilities​

A signed URL grants a temporary ability.

Scope it narrowly:

specific bucket/object key
short expiry
expected method
content constraints where platform supports them

Do not generate arbitrary object keys from untrusted tenant IDs without authorization.

User-generated filenames​

Store generated internal object keys rather than trusting raw filenames as filesystem paths.

Keep original filename as metadata only after sanitization/escaping for presentation.

Never do:

/uploads/${formData.filename}

on a local filesystem without robust path controls.

SVG is active content​

SVG can contain scripts, external references, and active behavior depending on how served/embedded.

If accepting SVG uploads:

  • sanitize with a mature SVG-aware tool if transformation is needed
  • consider serving as attachment or from an isolated origin
  • set appropriate CSP/content disposition
  • avoid inline rendering of untrusted SVG

Phase 12 covered Next.js image SVG settings; Phase 13 owns the broader content-security threat model.

Malware scanning​

For products accepting documents or executable formats, scanning/quarantine may be part of the upload pipeline.

A safer architecture can be:

upload β†’ quarantine bucket
β†’ asynchronous scan/validation
β†’ mark approved
β†’ publish/move to trusted serving path

Do not claim a file is safe before validation finishes.

Public file serving​

User-generated files may deserve a separate origin/domain from your main authenticated application.

That limits the impact of active content and cookie/origin interactions.

Webhook authentication​

Webhooks are public endpoints but often authenticate via a provider signature rather than a user session.

Typical secure flow:

receive raw body
β†’ read signature/timestamp headers
β†’ verify cryptographic signature with provider secret
β†’ enforce replay window
β†’ deduplicate event ID
β†’ enqueue/commit idempotently
β†’ return acknowledgement

Verify the raw body when protocol requires it​

Many webhook signatures cover the exact bytes sent by the provider.

If you parse JSON and then stringify it again before verification, whitespace/order differences can break verificationβ€”or worse, produce incorrect custom logic.

Follow the provider's signature library and raw-body requirements.

Replay protection​

A valid signature does not necessarily mean a request is fresh.

Use provider-supported timestamp checks and deduplication.

Store processed event IDs when duplicate delivery would cause harm.

Webhooks are usually at-least-once delivery​

Assume retries and duplicates.

Design handlers to be idempotent.

Examples:

unique event ID constraint
idempotency table
state-machine guard
transactional outbox/inbox

Acknowledge before slow work when possible​

Provider webhook timeouts can trigger duplicate retries.

For heavy work:

verify
persist/enqueue durably
respond 2xx
process asynchronously

Do not use an in-process promise as the only durable queue.

Rate limiting mental model​

Rate limiting is not one global number.

Different operations need different policies:

login
password reset
signup
search
file upload
webhook
API read
expensive export
AI generation
billing mutation

Rate-limit identities​

Possible dimensions:

  • IP/network
  • user ID
  • organization
  • API key
  • device/session
  • route/action
  • resource

Use combinations appropriate to the abuse model.

IP-only limits can harm users behind NAT and can be bypassed by distributed attackers.

Distributed storage​

Multi-instance deployments need shared rate-limit state or infrastructure-enforced limits.

Do not rely on:

const limits = new Map()

for production global enforcement.

Fail-open vs fail-closed rate limiting​

If the rate-limit store is unavailable:

fail open
β†’ preserves availability, weakens abuse defense

fail closed
β†’ protects expensive/sensitive endpoint, risks denying legitimate traffic

Choose per endpoint.

Password reset and money-moving operations may have a different policy than a public search page.

Timeouts and resource budgets​

Every server-side operation should have bounded resource assumptions:

body bytes
file bytes
response bytes
upstream timeout
database timeout
concurrency
CPU work
memory

Attackers target expensive paths.

Algorithmic abuse​

A small request can trigger large work:

regex backtracking
huge search fan-out
N+1 queries
image transformations
recursive parsing
large JSON nesting
expensive export
LLM generation

Rate limits alone do not fix unbounded algorithms.

Bound the work itself.

Pagination limits​

Never accept unlimited page size:

?limit=100000000

Validate and cap.

Similarly cap:

batch size
IDs per request
filter clauses
sort keys
upload count
concurrent jobs

SQL injection​

Use parameterized queries or safe ORM APIs.

Do not build SQL from untrusted strings:

`SELECT * FROM users WHERE email = '${email}'`

Even with an ORM, raw SQL escape hatches require the same caution.

NoSQL / query injection​

Object-shaped query APIs can also become dangerous when client objects are passed directly into database filters.

Validate and project an allowed query schema.

Do not pass arbitrary JSON into ORM/database where clauses.

Command injection​

If server code invokes shell commands, never concatenate untrusted input into a command string.

Prefer APIs that avoid the shell and use explicit argument arrays.

Most web application features should not need shell execution at request time.

Path traversal​

Any server filesystem path influenced by user input must be treated as dangerous.

Prefer object storage and generated identifiers.

If local file access is required, canonicalize and enforce an allowed root.

Callback URLs​

External providers may call webhook/callback endpoints.

Your application may also call user-configured webhooks.

For outbound webhooks, SSRF protections are mandatory because the customer controls the destination URL.

A dedicated egress service or network policy may be appropriate.

API keys​

For programmatic clients, API keys should be:

  • generated with strong randomness
  • shown once where possible
  • stored hashed if verification design permits
  • scoped by permissions/tenant
  • revocable
  • rotatable
  • rate-limited
  • audited

Never use a predictable database ID as an API secret.

Security monitoring signals​

Useful abuse metrics:

failed login rate
password reset rate
429 rate
invalid webhook signatures
upload rejection reason
SSRF validation rejection
API key failures
resource authorization denials
body-size rejections
upstream timeout rate

Phase 14 will add observability implementation depth.

Incident containment​

Prepare controls for:

disable endpoint
revoke key
block provider integration
reduce upload size
raise auth assurance
invalidate sessions
rotate secret
change allow-list

Security architecture should support emergency response without a full rewrite.

Interview questions​

What is SSRF?
An attacker influences a server-side request so the server accesses a target the attacker could not reach directly, often internal or privileged infrastructure.

Why verify webhook signatures before parsing business data?
Signature verification establishes that the request came from the expected provider and often depends on the exact raw bytes.

Why are direct-to-storage uploads common?
They keep large bytes off application compute while allowing the app to authorize a narrowly scoped temporary upload capability.

Exercise​

Threat-model:

URL preview service
avatar upload
PDF upload
billing webhook
customer-configured outbound webhook
login endpoint
public search endpoint
bulk export endpoint

For each specify:

  • validation
  • authentication
  • authorization
  • SSRF policy
  • size/time/concurrency bounds
  • rate-limit identity
  • durable/idempotent behavior
  • logging/metrics
  • emergency kill switch

Primary references​