koreader / koreader

An ebook reader application supporting PDF, DjVu, EPUB, FB2 and many more formats, running on Cervantes, Kindle, Kobo, PocketBook and Android devices
http://koreader.rocks/
GNU Affero General Public License v3.0
16.78k stars 1.26k forks source link

Cannot download title with colon in name via OPDS #7837

Closed zephyros-dev closed 3 years ago

zephyros-dev commented 3 years ago

Issue

Same as issue #2433

Steps to reproduce

  1. Download epub from any OPDS source (Project Gutenberg, Feedbooks)
  2. Title with colon in the name failed with error "Could not save file to [local directory]"
  3. Other title without colon can be downloaded normally
crash.log (if applicable)

crash.log is a file that is automatically created when KOReader crashes. It can normally be found in the KOReader directory:

Android logs are kept in memory. Please go to Help → Bug Report to save these logs to a file.

Please try to include the relevant sections in your issue description. You can upload the whole crash.log file on GitHub by dragging and dropping it onto this textbox.

If you instead opt to inline it, please do so behind a spoiler tag:

crash.log ``` ```
NiLuJe commented 3 years ago

Right, we're probably missing fs sanitization there (as colons are a no-no on some FS).

Frenzie commented 3 years ago

We have a util function for that.

NiLuJe commented 3 years ago

Yep, which makes me wonder why it's not being used here ;o).

Frenzie commented 3 years ago

The issue claims it's the same as #2433 but having glanced at it now, that was fixed by its very introduction. ;-) https://github.com/koreader/koreader/pull/2464

NiLuJe commented 3 years ago

Possibly the filesystem check in util.getSafeFilename needs an update?

@phongngthanh: What's the target filesystem? (And is /proc/mounts actually readable by a plain app, ping @pazos?).

Galunid commented 3 years ago

https://github.com/koreader/koreader/blob/6b31b160a2964aacfb2fb8b44ffb75d7efa98666/frontend/util.lua#L823-L827 seems to miss ;

Frenzie commented 3 years ago

It's not in there because it's perfectly fine.

zephyros-dev commented 3 years ago

Possibly the filesystem check in util.getSafeFilename needs an update?

@phongngthanh: What's the target filesystem? (And is /proc/mounts actually readable by a plain app, ping @pazos?).

How do I check for that? I'm using Android 11 atm. I just tested on another device running Android 10 and the OPDS download normally even with colon. Maybe it had something to do with Android 11. I'll try to find some other device to test with if possible.

pazos commented 3 years ago

is /proc/mounts actually readable by a plain app?

No idea. Reading stuff from /proc isn't part of the api, so it can be implemented at vendors will. If we're relying on that maybe we should change it to another android API call :tm:

@phongngthanh: I would need a sample of a book that fails to download in Android11 (name, repo) to check if I can reproduce on my samsung tablet.

zephyros-dev commented 3 years ago

is /proc/mounts actually readable by a plain app?

No idea. Reading stuff from /proc isn't part of the api, so it can be implemented at vendors will. If we're relying on that maybe we should change it to another android API call ™️

@phongngthanh: I would need a sample of a book that fails to download in Android11 (name, repo) to check if I can reproduce on my samsung tablet.

You can try

pazos commented 3 years ago

I can reproduce the issue here too. Nothing weird in the logs. I'm building a debug APK just to be sure I'm not missing something.

@NiLuJe: found the requisites for the kernel interface: /proc/mounts is in there (a symbolic link to /proc/self/mounts). That just mean than the android system relies on it for some stuff, but access for unprivilegied apps can be restricted by selinux

pazos commented 3 years ago

It turns out that : is invalid in android: https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/os/FileUtils.java;l=972?q=isValidFatFilenameChar

nothing jumps out in logs, except for this line:

06-14 19:54:29.459  3144  3614 E MediaProvider: File name contains invalid characters
pazos commented 3 years ago

downgrading targetSdk doesn't work either.

Frenzie commented 3 years ago

But the function in question errs on the side of caution. If a colon's being inserted something's going wrong somewhere.

pazos commented 3 years ago

@Frenzie: no idea. Maybe util.getSafeFilename fails on some android versions and that triggers a different replaceFunc in https://github.com/koreader/koreader/blob/master/frontend/util.lua#L858-L863 ?

The infomessage shows the full path with a colon, but I'm not sure if it should display the safe filename there.

NiLuJe commented 3 years ago

Well, if colons are invalid on Android, period, then just stick an android specific branch that doesn't even check the fs and just enforce the full sanitization?

Frenzie commented 3 years ago

Yes, early exit here if isAndroid() or something like that.

https://github.com/koreader/koreader/blob/6b31b160a2964aacfb2fb8b44ffb75d7efa98666/frontend/util.lua#L849-L851