I’ve found a consistent method to cause the Ghost Admin API to fail to return posts, resulting in a 500 error whenever you try to access the drafts section of the admin interface.
I noticed this first when I was fleshing out a blog post and added an inline code example using the Markdown shorthand, like so:
I was talking about adding `<img>` elements
As soon as I typed that out the 500 error appeared and I was unable to access the drafts section (500 page with animated tumbleweed) and the API requests in the browser devtools were returning nothing for drafts (even though I had a lot).
I’ve since been able to replicate this reliably by simply copying the example in the replication steps below, effectively as soon as I type out the Markdown syntax for inline code, followed by the <img>
element. The “crash” doesn’t occur if you don’t put <img>
/ HTML elements inside.
Restarting Ghost doesn’t change anything. The only way I was able to resolve this and get my posts back was to manually delete the bad post via the Admin API, which was a journey in itself as I couldn’t get the ID of the bad post via the Admin API as it returns nothing.
For what it’s worth, I was unable to replicate this locally on an instance that uses SQLite. I’ve only seen this and been able to replicate it on my production instance that uses MySQL. I don’t know if that is relevant but it’s worth mentioning.
I’ve outlined as much as I can above and below but appreciate that you’ll likely need more to help debug this. Let me know what else I can provide to help as I can replicate this reliably.
What’s your URL?
https://robinhawkes.com
What version of Ghost are you using?
v4.11.0
How was Ghost installed and configured?
DigitalOcean one-click install
What Node version, database, OS & browser are you using?
- Node: v14.16.1
- MySQL
- Ubuntu Ubuntu 20.04.2 LTS (Ghost)
- Chrome 92.0.4515.131
What errors or information do you see in the console?
- No errors in the Ghost logs after saving a bad post.
- Error in browser after saving bad post:
What steps could someone else take to reproduce the issue you’re having?
- Open drafts and click “New Post”
- Give the post any title and move to the body
- Type the following
This will crash the Ghost Admin API for posts if I add an `<img>` element with the Markdown code syntax
- If 500 Error hasn’t already occurred then force a save
- You should be seeing a 500 Error, which remains if you refresh
- Browse and post API endpoints now return nothing
Temporary resolution
Given that this completely disables the ability to add new draft posts or edit existing ones, I had a pretty immediate need to get the blog functional again. I was able to do this via the following method:
- Manually log into the MySQL database
- Get the latest post ID
select id, title from posts order by updated_at desc limit 1;
- Use the Admin API client to delete the bad post
api.posts
.delete({
id: "the-bad-post-id",
});
- Refresh Ghost and all should be good
The only method I could get the bad post ID was via the database as the Admin API returns nothing when calling browse
/ posts
after the bad post has been added, so it’s impossible to get the ID from the API, nor edit the post via the API.
SQL data for the bad post
mobiledoc
:
{"version":"0.3.1","atoms":[],"cards":[],"markups":[["code"]],"sections":[[1,"p",[[0,[],0,"This will crash the Ghost Admin API for posts if I add an "],[0,[0],1,"<img>"],[0,[],0," element with the Markdown code syntax"]]]],"ghostVersion":"4.0"}
html
:
<p>This will crash the Ghost Admin API for posts if I add an <code><img></code> element with the Markdown code syntax</p>
plaintext
:
This will crash the Ghost Admin API for posts if I add an <img> element with the
Markdown code syntax