WordPress · Drupal · Site Maintenance · Technical SEO · Editorial Policy

How to Find and Remove Injected Code on a Sanity Site

Sanity is a headless platform, so there is no single theme folder to inspect. Your content lives in the hosted Content Lake. Your visitors see whatever your own front-end renders. Sanity Studio is an open-source React app your team configures and deploys. Malicious scripts, hidden links or redirects can therefore enter in several places, and cleanup means checking each one in turn.

Where injected code can live on a Sanity site

Start by deciding which layer is serving the bad code. Your answer decides where you look.

  • Your front-end project: templates, layout files, tracking snippets, third-party script tags and build configuration. This is the most common place visitors are affected.
  • Dependencies: npm packages used by the front-end or by Sanity Studio, including Studio plugins.
  • Content in the Content Lake: text, rich text or custom fields that your front-end renders without escaping. This only matters if your code allows raw HTML or script output.
  • Hosting and delivery: environment variables, build settings, redirects and headers at your front-end host or CDN.
  • Studio deployment: custom Studio code or plugins, which affect editors rather than public visitors.

Sanity does not run your front-end for you, so injected code in a public page nearly always traces back to your repository, your build pipeline, a dependency or content you render unsafely.

Confirm what visitors actually receive

Before you change anything, collect evidence of the problem as a visitor sees it.

  1. Load the affected page in a private window and view the raw page source, not only the inspected DOM.
  2. Search the source for unfamiliar script tags, iframe elements, eval, atob, long encoded strings, and links to domains you do not recognise.
  3. Open the browser developer tools network panel and list every domain that loads scripts, then compare it with the services you intentionally use.
  4. Test from a phone on mobile data and with a search-engine style referrer. Some malicious redirects only fire for certain visitors.
  5. Check the same page on the production URL and on any preview or staging URL. A difference points you to the layer that is compromised.

Save a copy of the bad source and the list of suspicious domains. You will use them to search your code and content.

Compare your code against version control

The strongest tool you have is your Git history. A headless front-end should be deployed from a repository, so the deployed code should match a known commit.

  • Run a diff between the deployed commit and the last commit you trust. Read every changed file, especially layouts, document heads, middleware and build scripts.
  • Search the whole repository for the suspicious domains and strings you noted. Include lockfiles, configuration files and hidden folders.
  • Review recent commits and pull requests for authors or times you do not recognise. A stolen access token can push code that looks routine.
  • Check for lifecycle scripts in package.json such as postinstall that run commands during install or build.
  • Compare your lockfile with an earlier version. A new or changed dependency you did not add deserves a closer look.

If the deployed output differs from what your repository would build, the problem is in the build or hosting layer, not in your source. Check environment variables, build commands, redirect rules and any edge functions at your host.

Review custom code, embeds and content

Next, look at code paths that deliberately accept outside input.

Embeds and tag managers. Review every script included through a tag manager, analytics snippet, chat widget or ad integration. Anyone with access to those tools can change what loads on your site without touching your repository. Remove anything you cannot justify.

Unsafe rendering. Search your front-end for patterns that insert raw HTML, such as dangerouslySetInnerHTML or equivalent features in your framework. Where content comes from the Content Lake, render rich text through a proper serializer rather than injecting strings. Sanity’s rich text format, Portable Text, is structured data, which makes safe rendering easier than raw HTML.

Custom schema types. If your Studio schema includes a field that holds raw HTML, code or a script URL, search your datasets for suspicious values in it. Use the Studio search or run a query with Sanity’s query language, GROQ, against that field.

Studio plugins. Review the Studio plugins listed in your Studio’s package.json. Remove any you no longer use, and check that each one is still the package you intended to install.

Remove the code and close the way in

Cleaning only the symptom invites reinfection. Work in this order.

  1. Put the site in a controlled state. If you can, redeploy from a clean, trusted commit rather than editing files on the host.
  2. Delete the malicious code from your repository, and remove any malicious or unknown dependency. Reinstall from a clean lockfile.
  3. Fix affected content. Edit or restore the document in Sanity Studio, or use the document history to return to a clean version where it is available for your plan.
  4. Remove access you cannot explain: unknown project members, unfamiliar deploy keys, extra Git collaborators and unneeded third-party integrations.
  5. Rebuild and redeploy, then repeat the visitor checks from earlier on the production URL.

Before changing content in bulk, consider exporting your dataset with the Sanity command line tool so you keep a record of the current state.

Rotate credentials and tighten access

Assume any secret that could have been exposed is no longer private. Treat the following as a rotation checklist.

  • Sanity API tokens: in your project’s management area, delete tokens you do not recognise or no longer need, and create new ones with the lowest permission that works. Read-only tokens are enough for most front-ends.
  • Project members and roles: remove people who no longer need access, and give editors only the roles they require. Ask every member to review their sign-in method and turn on any extra verification their login provider offers.
  • CORS origins: list only the exact origins that should talk to your project, and remove stale development or old domains. Avoid allowing credentials from origins you do not control.
  • Hosting and Git access: rotate deploy keys, Git access tokens, CI secrets and environment variables at your front-end host.
  • Webhooks: check configured webhooks and confirm each destination is still yours.

Then reduce the chance of a repeat. Add a Content Security Policy so browsers only run scripts from sources you approve. The MDN guide to Content Security Policy explains how. Keep dependencies updated, require review before merging code, and read the Sanity documentation for current guidance on tokens, roles and CORS. For general web risks such as injection and broken access control, the OWASP Top Ten is a useful checklist to review your front-end against.

Frequently asked questions

Can someone inject malware through Sanity itself?

The Content Lake stores and serves your content, but it does not run your front-end. Harmful code reaches visitors when your front-end renders unsafe content, loads a bad script, or ships a bad dependency. Control over your tokens, roles and repository is what matters most.

Do I need to take the site offline during cleanup?

Not always. Redeploying from a clean commit often replaces the infected build in one step. If visitors are being redirected or exposed to harmful scripts and you cannot find the source quickly, a temporary maintenance page is a reasonable choice while you investigate.

How do I know the problem is really gone?

Repeat the visitor checks on the production URL: view the raw source, review loaded domains, and test from a mobile connection. Then search your repository, dependencies and content for the strings you noted. Continue to monitor deploys and token usage for unexpected changes.

We use essential cookies to make this site work and remember your preferences. We do not use advertising or analytics cookies. Privacy Policy. Cookie Policy.