[Bug]: Sub-directory checks in watcher.js are incorrect #1480

Closed
opened 2026-04-24 23:47:07 +02:00 by adam · 2 comments
Owner

Originally created by @mikiher on GitHub (Oct 23, 2023).

Describe the issue

In server/watcher.js, there are two places in which the code checks if a path B is the same or a sub-path of another path A:

  // Get file folder
  const folder = libwatcher.folders.find(fold =>  path.startsWith(filePathToPOSIX(fold.fullPath)))
  if (!folder) {
    Logger.error(`[Watcher] New file folder not found in library "${libwatcher.name}" with path "${path}"`)
    return
  }

and:

checkShouldIgnorePath(path) {
  return !!this.ignoreDirs.find(dirpath => {
    return filePathToPOSIX(path).startsWith(dirpath)
  })
}

B.startsWith(A) is an incorrect check for sub-paths. For example, we might have A='/audiobooks' and B='/audiobooks2/book.m4b', so B.startsWith(A), but B is not a sub-path of A. This can of course lead to issues.

This Stackoverflow question has a solution, which is still not watertight, but I think it's acceptable for this case.

Steps to reproduce the issue

  1. In the same library, put two folders: '/audiobooks' and '/audiobooks2'.
  2. Add a file to /audiobooks2.
  3. The wrong folder will be selected in the first code snippet above, and consequently a wrong relPath will be calculated, likely leading to errors.

Audiobookshelf version

2.4.4

How are you running audiobookshelf?

Docker

Originally created by @mikiher on GitHub (Oct 23, 2023). ### Describe the issue In server/watcher.js, there are two places in which the code checks if a path B is the same or a sub-path of another path A: ``` // Get file folder const folder = libwatcher.folders.find(fold => path.startsWith(filePathToPOSIX(fold.fullPath))) if (!folder) { Logger.error(`[Watcher] New file folder not found in library "${libwatcher.name}" with path "${path}"`) return } ``` and: ``` checkShouldIgnorePath(path) { return !!this.ignoreDirs.find(dirpath => { return filePathToPOSIX(path).startsWith(dirpath) }) } ``` B.startsWith(A) is an incorrect check for sub-paths. For example, we might have A='/audiobooks' and B='/audiobooks2/book.m4b', so B.startsWith(A), but B is not a sub-path of A. This can of course lead to issues. This [Stackoverflow question](https://stackoverflow.com/questions/37521893/determine-if-a-path-is-subdirectory-of-another-in-node-js) has a solution, which is still not watertight, but I think it's acceptable for this case. ### Steps to reproduce the issue 1. In the same library, put two folders: '/audiobooks' and '/audiobooks2'. 2. Add a file to /audiobooks2. 3. The wrong folder will be selected in the first code snippet above, and consequently a wrong relPath will be calculated, likely leading to errors. ### Audiobookshelf version 2.4.4 ### How are you running audiobookshelf? Docker
adam added the bug label 2026-04-24 23:47:08 +02:00
adam closed this issue 2026-04-24 23:47:09 +02:00
Author
Owner

@advplyr commented on GitHub (Oct 23, 2023):

Thanks, I just added a subdir check recently actually using the same method https://github.com/advplyr/audiobookshelf/blob/c4c12836a4c512b82c560a3fdfe8fa427edf3a2b/server/routers/HlsRouter.js#L39

@advplyr commented on GitHub (Oct 23, 2023): Thanks, I just added a subdir check recently actually using the same method https://github.com/advplyr/audiobookshelf/blob/c4c12836a4c512b82c560a3fdfe8fa427edf3a2b/server/routers/HlsRouter.js#L39
Author
Owner

@advplyr commented on GitHub (Oct 29, 2023):

Fixed in v2.5.0

@advplyr commented on GitHub (Oct 29, 2023): Fixed in [v2.5.0](https://github.com/advplyr/audiobookshelf/releases/tag/v2.5.0)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/audiobookshelf#1480