hexo icon indicating copy to clipboard operation
hexo copied to clipboard

`og:image` URL Construction Crashes on Invalid/Relative Base URL

Open codeCraft-Ritik opened this issue 2 months ago • 1 comments

Check List

  • [x] I have already read Docs page & Troubleshooting page.
  • [x] I have already searched existing issues and they are not help to me.
  • [x] I examined error or warning messages and it's difficult to solve.
  • [x] I am using the latest version of Hexo. (run hexo version to check)
  • [x] My Node.js is matched the required version.

Expected behavior

📌 Expected behavior

The open_graph() helper should safely resolve all og:image URLs — including relative image paths embedded inside post content — against the configured config.url. If a path cannot be resolved to a valid absolute URL, it should either be skipped gracefully with a warning log, or left unchanged. Site generation must never crash due to an image URL resolution failure.


Actual behavior

The helper maps every collected image path through new URL(path, url || config.url) on line 138 of lib/plugins/helper/open_graph.ts:

// lib/plugins/helper/open_graph.ts — Line 138
images = images.map(path => new URL(path, url || config.url).toString())
  .filter(url => !url.startsWith('data:'));

When any image path extracted from <img src="..."> inside post content is a relative path (e.g., ../images/photo.jpg) and the base URL is not valid or this.url is a relative string, new URL() throws an unhandled TypeError that bubbles all the way up, crashing the entire page generation pipeline.

Even when it does not crash outright, resolved URLs like http://example.com/../shared/photo.jpg are not normalized and will be rejected by social media crawlers (Facebook, Twitter, Slack) when parsing og:image.


How to reproduce?

  1. Set url: 'http://example.com' and root: /blog/ in _config.yml.
  2. Create a post with an image using a relative path:
    ![Photo](../shared/photo.jpg)
    
  3. In your theme's head.ejs or head.njk, call:
    <%- open_graph() %>
    
  4. Run hexo generate.
  5. Observe: The generated HTML contains <meta property="og:image" content="http://example.com/../shared/photo.jpg"> which is an un-normalized, broken URL — or in edge cases the build crashes entirely with TypeError [ERR_INVALID_URL].

Is the problem still there under Safe mode?

Yes. The bug is in the core open_graph helper (lib/plugins/helper/open_graph.ts) and is not caused by any plugin. Running with hexo generate --safe still triggers the broken URL output whenever a post contains a relative image path.

Your Node.js & npm version

node -v && npm -v

> *(Reproducible on Node.js ≥ 16.x — any version that supports the WHATWG `URL` API)*

Your Hexo and Plugin version

npm ls --depth 0

> Reproducible on Hexo `7.x` and `8.x` — the code on `master` is identically affected.

Your package.json

#### Your `package.json`

{
  "name": "my-blog",
  "version": "1.0.0",
  "dependencies": {
    "hexo": "^8.0.0",
    "hexo-generator-archive": "^2.0.0",
    "hexo-generator-category": "^2.0.0",
    "hexo-generator-index": "^3.0.0",
    "hexo-generator-tag": "^2.0.0",
    "hexo-renderer-ejs": "^2.0.0",
    "hexo-renderer-marked": "^6.0.0",
    "hexo-renderer-stylus": "^3.0.0",
    "hexo-server": "^3.0.0"
  }
}

Your site's _config.yml (Optional)

url: http://example.com
root: /blog/
theme: landscape

Others

Root cause (code-level):

The bug is at lib/plugins/helper/open_graph.ts#L138:

// BUGGY — no error handling for invalid URL combinations
images = images.map(path => new URL(path, url || config.url).toString())
  .filter(url => !url.startsWith('data:'));

Suggested fix:

images = images
  .map(path => {
    try {
      return new URL(path, url || config.url).toString();
    } catch {
      // log warning instead of crashing
      return null;
    }
  })
  .filter(Boolean)
  .filter(u => !u.startsWith('data:'));

codeCraft-Ritik avatar Oct 04 '26 04:10 codeCraft-Ritik

Hi! I'd like to work on this. I'll add a regression test for the crash and a fix in open_graph.ts. This is my first contribution to Hexo, so please let me know if you'd prefer a different approach.

amanrock1 avatar Oct 06 '26 09:10 amanrock1