Injected code on a Ghost site usually shows up as a script you did not add, links to unrelated pages that visitors never see, or a redirect that sends some readers elsewhere. Ghost keeps its moving parts fairly contained, so the places to look are short and predictable. This guide covers where to look, how to clean up, and how to make a repeat less likely.
What you can inspect depends on how the site runs. Self-hosted Ghost, installed with Ghost-CLI, gives you server access and the theme files on disk. Ghost(Pro) is managed by Ghost, so you work through the admin area and Ghost support instead.
Signs that code has been injected
Look for symptoms before you start searching files:
- Visitors report being sent to another site, often only on mobile or only from search results.
- Your pages load scripts from domains you do not recognise.
- View-source shows links to pharmacy, casino or other off-topic pages tucked into the footer, or hidden with CSS.
- A search engine flags the site, or shows a title and description you did not write.
- Your browser’s developer tools show unexpected requests on the Network tab.
Some redirects only trigger for particular visitors. Test in a private window, on a phone, and after arriving from a search result, not only by typing the address.
Check Ghost’s built-in code areas first
Ghost lets administrators add code without touching any theme file, which makes these areas the quickest to review and the easiest for an attacker with a stolen login to abuse.
- Code injection. In Ghost Admin, open the settings and find Code injection. Read both the site header and site footer boxes. Anything there runs on every page. Delete anything you cannot explain.
- Per-post and per-page code injection. Each post and page has its own header and footer injection fields in the post settings. A single compromised post can carry a script that the site-wide settings do not show.
- HTML and embed cards. Content can contain raw HTML cards. Search your content for iframes and script tags, especially in older posts.
- Navigation and announcement areas. Check navigation links, secondary navigation and any custom text fields your theme exposes for unfamiliar URLs.
- Integrations. Open the integrations settings and review custom integrations, webhooks and any third-party tools. Remove what you do not use.
Copy the contents of anything you remove into a note first. You may need it later to work out how the code got there.
Inspect the theme files
Ghost themes are built from Handlebars templates (.hbs files), CSS and JavaScript. Injected code tends to hide in the shared templates that appear on every page, such as default.hbs, the header and footer partials, and anything under the theme’s asset folders.
On a self-hosted site, the active theme sits in the content/themes folder of your Ghost installation. Look for:
- Script tags pointing to external domains you do not recognise.
- Long strings of encoded text, or JavaScript that decodes and runs a string at load time.
- Links wrapped in elements positioned off-screen or given zero size.
- Scripts that check the visitor’s referrer or device before redirecting.
- Files with odd names or recent modification dates that do not match your last theme update.
On Ghost(Pro), you cannot browse the server. Download your active theme as a zip from the theme settings in Ghost Admin, unzip it on your computer and review the files the same way.
Compare against a known clean copy
The most reliable method is a comparison, not a hunt by eye. If you use a theme from the Ghost marketplace or a public repository, download a fresh copy from the original source. Then run a file comparison between the clean copy and your live theme, using a diff tool or the comparison feature in your code editor.
Every difference is either a change you or your developer made on purpose, or a suspect. If your theme is custom, keep it in version control and compare the live files against the last commit you trust. A site without that history is much harder to clean with confidence.
Compare the whole theme, not only the files you expect to be affected. If a diff reveals changes in several unrelated files, treat the theme as untrustworthy and replace it entirely rather than patching it.
Remove the code and replace what you cannot trust
Work in this order so the problem does not come straight back:
- Take a backup of the current state, so you have evidence to examine. On self-hosted Ghost, Ghost-CLI provides a backup command for this.
- Remove malicious entries from code injection fields, posts and integrations.
- Replace the theme with a clean copy: upload a fresh zip from the original source, or deploy your last good version from version control. On self-hosted Ghost, also look for extra or unfamiliar theme folders and delete the ones you do not use.
- On self-hosted Ghost, check the rest of the installation and the server for files you did not put there, and confirm no unknown users or scheduled tasks exist on the server.
- If you are on Ghost(Pro) and the theme and settings look clean but symptoms persist, contact Ghost support and describe what you see.
- Clear any CDN or caching layer in front of the site, then retest in a private window.
Cleaning only the visible symptom is a common mistake. If you leave the way in open, the code returns.
Rotate credentials and close the way in
Injected code means someone could write to the site. Assume they could do it with a valid login or key until you can show otherwise.
- Change the password of every staff user, starting with owners and administrators, and remove staff accounts nobody recognises.
- Turn on any extra sign-in protection your Ghost version offers for staff accounts.
- Regenerate the Admin API and Content API keys for each custom integration, and update the services that use them.
- On self-hosted Ghost, change the server’s SSH keys and passwords, the database password and any deployment keys, and review who has server access.
- Sign out of all sessions after the password changes.
Rotate credentials only after you have removed the code and replaced the theme. Otherwise a still-active backdoor can simply capture the new details.
Prevent it from happening again
Prevention is mostly routine maintenance:
- Keep software current. On self-hosted Ghost, you are responsible for updating both Ghost and Node.js, and Ghost-CLI handles Ghost updates. Ghost(Pro) updates itself, so your job is the theme and the settings you control.
- Limit who can edit. Give staff the lowest role that lets them do their job. Few people need code injection access.
- Vet themes and integrations. Use sources you trust, and review any custom code before it goes live.
- Keep the theme in version control and deploy from there, so any unexpected change stands out in a comparison.
- Add a Content Security Policy that limits which domains can load scripts. MDN’s web security documentation explains how it works.
- Review on a schedule. Open code injection, integrations and the staff list regularly.
For wider background, the OWASP site covers common web application risks, and the Ghost documentation covers updating, backups, themes and staff roles for your setup.
Frequently asked questions
Can I just delete the suspicious script and move on?
Deleting it is a necessary step, but it is not sufficient. Something let the code in, whether a stolen login, an outdated install or a compromised integration. Replace the theme with a clean copy, rotate credentials and review user access as well.
Is Ghost(Pro) safer from injected code?
Ghost(Pro) removes the server and software update work from your side, which closes off some routes. Code can still be added through stolen staff logins, integrations, post HTML or a third-party theme. The checks in the admin area still apply.
How do I know a theme file is legitimate if I did not write it?
Compare it with a fresh download from the original theme source. If the files match, the file is clean. If the theme is custom and you have no clean copy, ask the developer for the original and consider replacing the theme with a known good one.