Crest Design

Next.js image safe mode: protect sites that show user uploads

Next.js image safe mode arrived in 16.5 (December 2026). How it isolates untrusted image processing, the config to turn it on, and fixes for common problems.

Crest Design
Guide to turning on Next.js image safe mode for sites that optimize user-uploaded and third-party images.

This post is for teams running a Next.js site or web app that shows images other people supply: marketplace listings, member avatars, product photos pulled from a supplier feed, or a CMS where many editors upload files. If that's you, Next.js image safe mode, an experimental option that shipped in Next.js 16.5 on 2 December 2026, deserves a place on your next sprint. You'll learn what problem it addresses, how it changes the way next/image processes files, how to switch it on and test it, and what to check when images stop appearing.

The problem: your server decodes images it didn't make

Every time a visitor requests a resized image through next/image, your server fetches the original file, decodes it into raw pixels, resizes it and encodes it again as WebP or AVIF. For your own logo, that's harmless. For a file uploaded by a stranger, or fetched from a host you don't control, it means your server is running complex parsing code on input that someone else crafted.

Image decoders are large, low-level libraries that understand many formats and their odd corners. A deliberately malformed file can trigger bugs in them. With the default optimizer, that decoding happens inside the same Node.js process as your app, right next to your environment variables, sessions and database connections. That's why image handling keeps appearing in framework security releases, including the fixes we covered in applying the November 2026 Next.js security update.

Patching fixes the bugs that are already known. Safe mode is about shrinking what the next unknown one can reach.

How Next.js image safe mode works

The Next.js 16.5 release notes describe safe mode as a new, opt-in implementation of next/image optimization built for untrusted images. It adds three layers of protection around the same job:

  • Memory-safe decoders. Images are decoded with libraries written to avoid the memory errors (reading or writing past the end of a buffer, for example) behind many decoder vulnerabilities.
  • Process isolation. Decoding and resizing run in a separate worker process instead of your app's server process. If a file crashes or compromises the worker, your app's memory isn't sitting in the same place.
  • Sandboxing on Linux. The worker is restricted with seccomp, a Linux kernel feature that limits which system calls a process may make. A compromised worker has far fewer ways to open files or network connections.

Two things don't change. First, the allow-list: images.remotePatterns and images.localPatterns still decide which sources the optimizer will fetch at all. Safe mode protects the processing, not the gate. Second, hosting: the release notes say the option may be ignored in production when your platform supplies its own image optimization service, as Vercel does. In that case, the platform's service does the decoding, and its own isolation applies.

An image passes the allow-list, then is decoded inside the app process by default or in an isolated, sandboxed worker with Next.js image safe mode.
Safe mode moves decoding out of your app process. The allow-list still decides what gets fetched.

How to turn on image safe mode, step by step

Five steps to roll out Next.js image safe mode: check where images are optimized, tighten the allow-list, enable the flag, test in Linux, ship and watch.
The rollout we follow for self-hosted Next.js sites that show uploaded or third-party images.

1. Check where your images are optimized

Safe mode only matters where Next.js's own optimizer runs. That's the case when you self-host with next start, a Docker image or a Node.js server on your own cloud account. If next.config sets a custom loader or loaderFile, images go to an external service and the built-in optimizer isn't used. On a managed platform, read the platform's image docs before spending time here.

2. Upgrade and tighten the allow-list

Move to the latest 16.5 patch release on your current major line. Then look hard at remotePatterns. A wildcard hostname lets anyone point your optimizer at any file on the internet, which is the opposite of what you want. List the exact hosts and paths you expect, and keep SVG optimization off unless you have a strong reason.

3. Turn on the flag in a branch

This is the whole configuration for a typical self-hosted site. It goes in next.config.ts at the project root; change the hostname and path to match where your uploads live.

// next.config.ts (Next.js 16.5)
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  images: {
    // Only optimize images from sources you expect
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'uploads.example.com',
        pathname: '/listings/**',
      },
    ],
    // Leave SVG optimization off for untrusted files
    dangerouslyAllowSVG: false,
  },
  experimental: {
    // Decode images in an isolated, sandboxed worker
    imgOptSafeMode: true,
  },
}

export default nextConfig

In recent 16.5 patches, the safe optimizer ships as an optional package, @next/safe-image-optimization. If the build log warns that it's missing, install it with npm install @next/safe-image-optimization. Don't ignore that warning: without the package, Next.js serves images unoptimized rather than failing loudly.

4. Test in the same Linux image you deploy

Seccomp is a Linux feature, so a macOS laptop won't show you the real behavior. Build the production image, run it, and request a handful of real images from every source. This small script, run with node check-images.mjs http://localhost:3000, prints the status and format for each one:

// check-images.mjs
const base = process.argv[2] ?? 'http://localhost:3000'
const sources = [
  '/images/hero.jpg',
  'https://uploads.example.com/listings/sample.png',
]

for (const src of sources) {
  const url = `${base}/_next/image?url=${encodeURIComponent(src)}&w=640&q=75`
  const res = await fetch(url, { headers: { Accept: 'image/avif,image/webp,*/*' } })
  console.log(res.status, res.headers.get('content-type'), src)
}

You want a success status and a WebP or AVIF content type for each line. The width and quality must be values your config allows; the ones shown match the Next.js defaults.

5. Ship and watch the image endpoint

Keep the change in one commit so it's easy to revert. After release, watch server logs and error rates on /_next/image for a few days, and spot-check pages with lots of uploaded images.

What we've learned, and troubleshooting

We'd turn this on for any self-hosted Next.js site that optimizes uploads or third-party images, once it has passed testing in staging. For a brochure site whose images all come from the team's own repository, it's lower priority; a tight allow-list and prompt patching matter more there. Because the option is experimental, its name and behavior can change between minor releases, so pin your Next.js version and re-read the release notes on each upgrade.

The mistakes we see most: treating safe mode as a reason to delay patches, leaving a wildcard in remotePatterns, switching on dangerouslyAllowSVG for user content, and testing only on a laptop.

Troubleshooting

Symptom Likely cause Fix
Images load but in their original size and format The safe optimization package is missing, so Next.js falls back to unoptimized images Install @next/safe-image-optimization, rebuild and check the build log is clean
Build fails with "cannot resolve @next/safe-image-optimization-..." An adapter or bundler tries to trace the optional platform packages Update to the latest 16.5 patch and your adapter's latest release, and check its issue tracker
Works locally, images fail in the container The platform-specific package wasn't installed for Linux, often because node_modules was copied from a Mac Run the install inside the Linux build stage of your Dockerfile
400 error from /_next/image The URL doesn't match remotePatterns, or the width or quality isn't allowed Fix the pattern, and use sizes and qualities from your image config
No visible change on a managed platform The platform's own image service handles optimization, so the flag is ignored Expected behavior; review the platform's image settings instead

Questions people ask

Is Next.js image safe mode ready for production?

It's marked experimental, so treat it like any early feature: staging first, then production with monitoring and an easy rollback. For sites that process untrusted images, we think the extra isolation is worth that care.

Will it slow down image loading?

Optimized images are cached after the first request, so most visitors receive the stored file either way. The first, uncached request may take a different amount of time, so measure it on your own server rather than assuming.

Do we need it if we only use our own images?

It helps less. The bigger risk sits with files you didn't create. Keeping your allow-list strict and your Next.js version current covers most of the ground for a site with only in-house images.

Why it matters for your business

On a marketplace, directory or store with supplier photos, the image endpoint is a public door into your server, and anyone can send something through it. Safe mode narrows what a bad file can do once inside, which protects customer data and keeps a single upload from turning into an outage. It's also a good test of a development partner: a team that maintains your site properly should already be asking where your images come from. If you're weighing the bigger picture, our plan for moving off Next.js 15 before its end of life covers the upgrade path, and our routine for patching Node.js servers after a security release covers the runtime underneath.

Quick checklist

  • Confirm the built-in optimizer actually runs on your hosting.
  • Upgrade to the latest 16.5 patch and list exact hosts in remotePatterns.
  • Turn on experimental.imgOptSafeMode and install the package if warned.
  • Test real images in your Linux production image.
  • Ship in one commit and watch /_next/image errors.

If you'd like help reviewing image handling on a self-hosted site, our Next.js development service covers it, and you can tell us about your project.

(over to you)

Planning a build like this?

Tell us what you're building and we'll come back with ideas and next steps.