[PR #2120] [MERGED] [server] x-accel: encode all paths to URIs #3654

Closed
opened 2026-04-25 00:16:31 +02:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/advplyr/audiobookshelf/pull/2120
Author: @itzexor
Created: 9/19/2023
Status: Merged
Merged: 9/19/2023
Merged by: @advplyr

Base: masterHead: x-accel-encode


📝 Commits (1)

  • 207ba7e x-accel: encode all paths to URIs

📊 Changes

4 files changed (+26 additions, -16 deletions)

View changed files

📝 server/controllers/BackupController.js (+4 -2)
📝 server/controllers/LibraryItemController.js (+13 -9)
📝 server/managers/CacheManager.js (+7 -4)
📝 server/utils/fileUtils.js (+2 -1)

📄 Description

updates util function encodeUriPath to use node:url with a file:// path prefix, and updates all instances x-accel redirection to use this helper util instead of sending unencoded paths into the header.

Using a local dev container with an external nginx reverse proxy for x-accel: I tested covers, audio files, and ebooks which all played correctly, even with unicode chars. I was unable to get download working, but I think that was just because of my test setup. I don't see any reason it should function differently in a proper setup...

fixes errors such as:

[2023-09-16 22:46:05] DEBUG: Use X-Accel to serve static file /audiobooks/Craig Alanson/Ascendant/1. Ascendantː Book 1/Ascendantː Book 1.webm (LibraryItemController.js:578)
node:internal/errors:478
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["X-Accel-Redirect"]
    at ServerResponse.setHeader (node:_http_outgoing:647:3)
    at ServerResponse.header (/node_modules/express/lib/response.js:794:10)
    at ServerResponse.header (/node_modules/express/lib/response.js:797:12)
    at ApiRouter.getLibraryFile (/server/controllers/LibraryItemController.js:579:30)
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at next (/node_modules/express/lib/router/route.js:144:13)
    at ApiRouter.middleware (/server/controllers/LibraryItemController.js:742:5) {
  code: 'ERR_INVALID_CHAR'

[the error in this specific file is the colon which is modifier letter triangular colon (U+02D0)]

this also changes the log to output the full encoded x-accel path (which means it now includes the prefix):

[2023-09-17 22:36:24] DEBUG: Use X-Accel to serve static file /protected/audiobooks/Craig%20Alanson/Ascendant/1.%20Ascendant%CB%90%20Book%201/Ascendant%CB%90%20Book%201.webm (LibraryItemController.js:579)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/advplyr/audiobookshelf/pull/2120 **Author:** [@itzexor](https://github.com/itzexor) **Created:** 9/19/2023 **Status:** ✅ Merged **Merged:** 9/19/2023 **Merged by:** [@advplyr](https://github.com/advplyr) **Base:** `master` ← **Head:** `x-accel-encode` --- ### 📝 Commits (1) - [`207ba7e`](https://github.com/advplyr/audiobookshelf/commit/207ba7ec8e6b665cc318aa20cd50ebabbc1024af) x-accel: encode all paths to URIs ### 📊 Changes **4 files changed** (+26 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `server/controllers/BackupController.js` (+4 -2) 📝 `server/controllers/LibraryItemController.js` (+13 -9) 📝 `server/managers/CacheManager.js` (+7 -4) 📝 `server/utils/fileUtils.js` (+2 -1) </details> ### 📄 Description updates util function encodeUriPath to use node:url with a file:// path prefix, and updates all instances x-accel redirection to use this helper util instead of sending unencoded paths into the header. Using a local dev container with an external nginx reverse proxy for x-accel: I tested covers, audio files, and ebooks which all played correctly, even with unicode chars. I was unable to get download working, but I think that was just because of my test setup. I don't see any reason it should function differently in a proper setup... fixes errors such as: ``` [2023-09-16 22:46:05] DEBUG: Use X-Accel to serve static file /audiobooks/Craig Alanson/Ascendant/1. Ascendantː Book 1/Ascendantː Book 1.webm (LibraryItemController.js:578) node:internal/errors:478 ErrorCaptureStackTrace(err); ^ TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["X-Accel-Redirect"] at ServerResponse.setHeader (node:_http_outgoing:647:3) at ServerResponse.header (/node_modules/express/lib/response.js:794:10) at ServerResponse.header (/node_modules/express/lib/response.js:797:12) at ApiRouter.getLibraryFile (/server/controllers/LibraryItemController.js:579:30) at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5) at next (/node_modules/express/lib/router/route.js:144:13) at ApiRouter.middleware (/server/controllers/LibraryItemController.js:742:5) { code: 'ERR_INVALID_CHAR' ``` [the error in this specific file is the colon which is `modifier letter triangular colon (U+02D0)`] this also changes the log to output the full encoded x-accel path (which means it now includes the prefix): ``` [2023-09-17 22:36:24] DEBUG: Use X-Accel to serve static file /protected/audiobooks/Craig%20Alanson/Ascendant/1.%20Ascendant%CB%90%20Book%201/Ascendant%CB%90%20Book%201.webm (LibraryItemController.js:579) ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2026-04-25 00:16:31 +02:00
adam closed this issue 2026-04-25 00:16:31 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/audiobookshelf#3654