Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files names are not sanitized under Android #7814

Open
alervd opened this issue Jan 15, 2025 · 9 comments
Open

Files names are not sanitized under Android #7814

alervd opened this issue Jan 15, 2025 · 9 comments

Comments

@alervd
Copy link

alervd commented Jan 15, 2025

Please provide the following information

libtorrent version (or branch): 1.2.19

platform/architecture: Android

compiler and compiler version: clang

please describe what symptom you see, what you would expect to see instead and
how to reproduce it.

Try to download the following torrent under Android:

magnet:?xt=urn:btih:ED05708B7CFB452E6042C54EEDDED94943FD05AD&dn=Various+Artists+-+Top+Country+Bangers+%282024%29+Mp3+320kbps+%5BPMEDIA%5D+%E2%AD%90%EF%B8%8F&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A6969%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Faarsen.me%3A6969%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Fexplodie.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fuploads.gamecoast.net%3A6969%2Fannounce&tr=http%3A%2F%2Ftracker.files.fm%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.tiny-vps.com%3A6969%2Fannounce&tr=udp%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.dler.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=http%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fcoppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.zer0day.to%3A1337%2Fannounce

In meta data received signal you'll get a torrent with non-sanitized files names. This is not the case under Windows. For example, the second file of this torrent ("02. Orville Peck - How Far Will We Take It?.mp3"). Under Windows it's name in torrent object provided by Libtorrent is 02. Orville Peck - How Far Will We Take It_.mp3 ("?" replaced with "_"). However, under Android, this is not the case. And because of this, we get file creation failure.

@arvidn
Copy link
Owner

arvidn commented Jan 25, 2025

filenames (path elements) are sanitized in this function: https://github.com/arvidn/libtorrent/blob/RC_2_0/src/torrent_info.cpp#L158

As you can see, it differs between windows and other operating systems. For example, to determine whether a character is valid in a filename or not, this function is used:

https://github.com/arvidn/libtorrent/blob/RC_2_0/src/torrent_info.cpp#L88

On linux, filenames and directory names are allowed to contain "?". Is that not the case on Android?

@alervd
Copy link
Author

alervd commented Jan 25, 2025

Yes, it seems that '?' is not allowed under Android. At least on FAT file system which is used for SD cards.
https://stackoverflow.com/a/64021421/3765267
Samsung My Files app does not allow me to use '?' in file names on both internal storage and SD card.

@OvercookedBeef
Copy link

I've tried the same on LineageOS and using Material Files (from f-droid) and same thing here, so not specific to Samsung or SD card (I tried with internal storage)
Image

@arvidn
Copy link
Owner

arvidn commented Jan 26, 2025

On windows, I believe the operating system restricts some characters, but I suspect that on Android it's the file system that limits valid characters. Perhaps FAT32 is common there. What I'm doing in that PR is an approximation.

@arvidn
Copy link
Owner

arvidn commented Jan 26, 2025

how does this look? #7828

@alervd
Copy link
Author

alervd commented Jan 27, 2025

how does this look? #7828

Thanks, it became better than ever. :)
However, I would recommend to use raw string literals.

Would it be possible to merge the fix into 1.2.x?

@arvidn
Copy link
Owner

arvidn commented Jan 27, 2025

That is a string literal. I don't know what would make it "raw".

@alervd
Copy link
Author

alervd commented Jan 27, 2025

@arvidn I meant this: https://en.cppreference.com/w/cpp/language/string_literal.
E.g. static const char invalid_chars[] = R"("*:<>?|";`)".

@arvidn
Copy link
Owner

arvidn commented Jan 27, 2025

back-port: #7832

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants