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

How to Find and Remove Injected Code on a Strapi Site

Strapi is a headless CMS. It stores content and serves it through an API, and it does not render your public pages. So injected code on a Strapi-powered site can sit in several places: the frontend that displays the content, the custom code in your Strapi project, the npm packages it depends on, or the content stored in the database. This guide shows you how to check each layer, remove what you find and stop it coming back.

Work out where the injection lives

Start with the symptom, because it points to the layer.

  • Visitors are redirected, or you see odd scripts in the page source of every page. The cause is usually in the frontend (your Next.js, Nuxt, Astro or other app), its templates, or a third-party tag.
  • Hidden links or scripts appear only on certain articles. The cause is probably in the content itself, such as a rich text field, or in the way the frontend renders it.
  • The admin panel behaves strangely, or the API returns unexpected data. Look at your Strapi project’s custom code, plugins and dependencies.

Check the symptom from a clean browser profile, then use view-source instead of the inspector. Injected markup that scripts add later will show up in the inspector but not in the source, and that difference tells you whether the code is server-rendered or added in the browser.

Take a safe copy before you change anything

Before you clean up, preserve evidence and a way back.

  1. Take a database backup and a copy of the project folder, including uploads if you store them locally.
  2. Note which hosting model you use. On Strapi Cloud, the platform runs the infrastructure and you manage the project code and settings. When you self-host, you also own the server, Node.js, the process manager and the file system.
  3. If the site is actively redirecting visitors, consider putting up a maintenance page while you work.

Compare your code against version control

The fastest check is a file comparison. If your Strapi project and your frontend live in Git, a clean checkout of your last known-good commit is your reference.

  • Run a diff between the deployed files and the repository. On a self-hosted server, any file that differs from the repository and that you did not change yourself deserves attention.
  • Look closely at config/, src/ (including src/api, src/plugins, src/extensions and any custom middlewares), and your frontend’s layout and head templates.
  • Review recent commits and who made them. A pushed change from an account nobody recognizes is as important as a changed file.
  • If the server holds files that are not in Git, such as a build folder edited by hand, rebuild from the repository rather than trusting them.

If you have no repository, compare against a fresh project created with the same Strapi setup and read the custom code line by line.

Search for the usual signs of injected code

Search your project, excluding node_modules at first, for patterns that rarely belong in legitimate code:

  • eval(, new Function(, atob( and long encoded strings next to them
  • document.write( and innerHTML assignments built from external data
  • script tags with an unfamiliar external src, or iframes with zero size
  • window.location changes and meta refresh tags that you did not add
  • unexpected outbound requests in server-side code, such as calls to unknown domains from a middleware or lifecycle hook
  • links with hidden styling, such as off-screen positioning or tiny text, inserted into templates

Treat each hit as a question, not a verdict. Many legitimate libraries use these functions. What matters is whether you can explain the code and trace it to a commit you trust.

Review dependencies, embeds and content

Strapi and its plugins are npm packages, so your dependency tree is part of your attack surface.

  • Run npm audit and read the output, then reinstall from your lockfile with npm ci to get a known set of packages.
  • Compare package.json and the lockfile against Git. A package you do not recognize, or an install script you did not expect, is a reason to investigate.
  • Remove plugins you no longer use, and keep Strapi, plugins and Node.js current. On Strapi Cloud, you still update the project’s own packages.

Then look at the content. Open entries in the Content Manager and check rich text fields for pasted HTML, script tags or unfamiliar links, especially in older or high-traffic entries. Check the Media Library for files you did not upload. If your frontend renders rich text as raw HTML, a malicious entry can become a script on the public page, so sanitize that output in the frontend as well.

Also review third-party tags, such as analytics, chat widgets and ad code, in your tag manager or frontend. A compromised external script can inject content without any file on your own server changing.

Clean up, rotate credentials and lock things down

Remove the injected code from the repository first, commit the fix, then redeploy from clean source. Deleting a file on the server alone does not help if the code returns on the next deploy.

Then assume that any secret the attacker could have read is compromised, and rotate it:

  1. Reset passwords for every admin user, and remove admin accounts you do not recognize. Check the roles assigned to the remaining ones.
  2. Delete and recreate API tokens in the admin panel settings, and review what each token can access.
  3. Replace the secrets in your environment file, such as APP_KEYS, API_TOKEN_SALT, ADMIN_JWT_SECRET, TRANSFER_TOKEN_SALT and the JWT secret used by the Users and Permissions plugin. Changing them invalidates existing sessions and tokens.
  4. Rotate database credentials, server SSH keys, and the deploy keys and tokens used by your Git host or CI.
  5. Review the public role’s permissions under Users and Permissions so that unauthenticated visitors can reach only what they need.

For lasting protection, add a Content Security Policy. Strapi ships a security middleware in its middleware configuration, and your frontend can send its own policy headers. A strict policy limits which script sources can run, which blunts many injected scripts. The MDN documentation on Content Security Policy explains the directives. Also protect the Git branch you deploy from with required reviews and two-factor authentication on every account that can push.

For the Strapi-specific settings mentioned above, see the official Strapi documentation. For a general framework on web application risks, the OWASP Top 10 is a useful checklist to review your code against.

Frequently asked questions

Can the Strapi admin panel itself be where the injected code is?

It can be, if someone changed custom admin code in your project or added a malicious plugin. Most visible injections, though, appear on the public frontend or come from content and third-party scripts. Compare your src/ folder and plugin list against Git to rule out the admin side.

Does Strapi Cloud protect me from this?

Strapi Cloud takes over hosting and infrastructure, but your project code, dependencies, content, users and API tokens remain yours to manage. Your separate frontend host is also your responsibility. Check what your plan and host cover.

How do I know the cleanup worked?

Redeploy from a clean commit, then check the page source in a fresh browser profile and test the affected URLs. Watch your server logs and admin user list for new activity. If the code returns, a credential or a dependency you have not rotated or reviewed is still open.

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.