Closed Bug 1637973 Opened 5 years ago Closed 4 years ago

browser.downloads.download file name with more than one space and illegal characters error

Categories

(WebExtensions :: General, defect, P1)

76 Branch
defect
Points:
2

Tracking

(firefox-esr68 unaffected, firefox76 wontfix, firefox77 wontfix, firefox78 fixed, firefox79 fixed)

RESOLVED FIXED
mozilla79
Iteration:
79.1 - June 1 - June 14
Tracking Status
firefox-esr68 --- unaffected
firefox76 --- wontfix
firefox77 --- wontfix
firefox78 --- fixed
firefox79 --- fixed

People

(Reporter: hurzon, Assigned: mak)

References

(Regression)

Details

(Keywords: regression)

Attachments

(2 files)

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0

Steps to reproduce:

call browser.downloads.download() with filename with more than one space character (consecutive). For example { filename: "0 a/image.png" }: there are two spaces between "0" and "a" folder name.

I attached an simple addon package.

  1. Install it, you can see "SAVE IMG FILE" context menu, and "SAVE" sub-menu on a image.
  2. Click this sub-menu "SAVE", and it will try to download an image file (Google logo image) as "0 a/image.png", but it fails.

Actual results:

It says "Error: filename must not contain illegal characters" on javascript console.

Expected results:

An image file should be downloaded into the specified directory and file name.
At least, until ver. 75.0, it worked with two or more consecutive space characters in the file name.
By the way, if there is one space character (e.g. "0 a/image.png"), the image file is saved without any error.

Bugbug thinks this bug should belong to this component, but please revert this change in case of error.

Component: Untriaged → Downloads Panel
Component: Downloads Panel → General
Product: Firefox → WebExtensions

This seems to be a result of the code at https://searchfox.org/mozilla-central/rev/3ce874dc2703831af3e5ef3a1d216ffd08057fa5/toolkit/components/extensions/parent/ext-downloads.js#581-584 rejecting any sanitized paths. I'm not sure why it does that. Presumably the webextension folks woul dknow.

The double space sanitization got added in bug 1598216 so that if we get sent a path with foo :bar in it, and we sub a space for :, we normalize the two consecutive spaces back to 1 space...

(In reply to :Gijs (he/him) from comment #4)

This seems to be a result of the code at https://searchfox.org/mozilla-central/rev/3ce874dc2703831af3e5ef3a1d216ffd08057fa5/toolkit/components/extensions/parent/ext-downloads.js#581-584 rejecting any sanitized paths. I'm not sure why it does that. Presumably the webextension folks woul dknow.

The idea is that if extension used a filename with invalid characters, we would throw. Since this is an actual api with explicitly passed filename, we prefer throwing an error instead of silently failing or normalizing it. We detect errors by checking if sanitized filename differs from the passed argument. This has mostly worked as described until bug 1598216.

The double space sanitization got added in bug 1598216 so that if we get sent a path with foo :bar in it, and we sub a space for :, we normalize the two consecutive spaces back to 1 space...

That sounds like good reasoning for the case when the original filename is invalid, but it doesn't looks like consecutive spaces normalization was intentional for already valid filenames. This is definitely not what we want for an explicit api like extensions, but I would also argue Firefox shouldn't be dropping consecutive spaces from otherwise valid filenames, they might be meaningful in some cases (just an anecdote, but my partner used to regularly download reports from a local government website which used leading whitespace for dates and case numbers, for proper sorting purposes).

Marco, what do you think?

Flags: needinfo?(mak)

I'm not sure how we'd distinguish a space generated from replacing an invalid char, from a normal space (without ugly double replacements). Even if we'd first check if there are invalid chars, there could still be multiple spaces too. So the question is pretty much whether we should compress whitespaces at all.
I think the most common case is making the file name more readable and usable to the user, rather than supporting exotic use-cases, like the example you pointed out, and compressing whitespaces goes that direction. For example posting a filename with multiple spaces on the Web compresses them into a single one (note how comment 0 itself had to specify the name contains 2 spaces); or typing into a shell, it's hard to distinguish the number of consecutive spaces to type.

I must note that to fix this specific case we could add an options argument to sanitize, like { compressWhitespaces: false } and WebExt could use that, while the rest of the code will keep compressing.
I don't have such strong feelings anyway, this is my opinion but if others think differently I'm fine to revert.

Flags: needinfo?(mak)

Maybe I should not disturb the discussion here now, as I am not familiar with the API development policy on FF. But I would like to share my feeling as a user and reporter of this issue.

I understand the necessity of sanitizing the file name, and agree that it helps to generate human readable one for users. Besides, I also think that having multiple space characters in file name is a little bit strange.
What I originally want to do with this WebExt is to save a file in a directory which is in a few layers down of the directory hierarchy from the download root. Actually, the file name itself doesn't matter if the multiple spaces are compressed or not. But I care the intermediate directory names. I rarely see those directory names in console window. I usually see them through file manger software (e.g. Explorer), and adding extra spaces makes it easier for me to view / organize them on the window.

By the way, when I use file download dialog, there is no problem to save files in those directory with multiple space characters in their name. That is why I had a feeling that the WebExt might have additional file name policy, and it narrows the valid file name definition which is permitted on a OS. And if that is the case, I thought it might be good to report this situation to know how current developers think.

Anyway, if the option to suppress space compression becomes available, it will be great. But if it generate any discrepancy / difficulty for your future development, I will revise my directory names in order to use my extension on FF.

(In reply to hurzon from comment #7)

Anyway, if the option to suppress space compression becomes available, it will be great.

Just to clarify, my suggestion would be an internal option for our code, for your extension everything would return to work as before, you'd not need to provide any additional options.

Thank you very much for your clarification. I imagined by myself that the option might be available through "about:config" tab.

(In reply to Marco Bonardo [:mak] from comment #6)

I'm not sure how we'd distinguish a space generated from replacing an invalid char, from a normal space (without ugly double replacements). Even if we'd first check if there are invalid chars, there could still be multiple spaces too.

I was thinking this was in JS, so something like replacing \s? \X ([\s \X]* \X)? \s? with a single space. It would be harder in C++ without regular expressions, but could be done.

It's a lot more complex, for a tiny benefit.

The severity field is not set for this bug.
:mixedpuppy, could you have a look please?

For more information, please visit auto_nag documentation.

Flags: needinfo?(mixedpuppy)

(In reply to Marco Bonardo [:mak] from comment #11)

It's a lot more complex, for a tiny benefit.

Nod, that's a question of cost/benefit. I'll leave the more general decision about user downloads to you.

For the concrete case of extension compatibility, are you able to take this bug Marco?

Severity: -- → S2
Flags: needinfo?(mixedpuppy)
Flags: needinfo?(mak)
Priority: -- → P1

S1 or S2 bugs need an assignee - could you find someone for this bug?

Flags: needinfo?(mixedpuppy)

I can try.

Assignee: nobody → mak
Status: NEW → ASSIGNED
Flags: needinfo?(mixedpuppy)
Flags: needinfo?(mak)
Pushed by mak77@bonardo.net: https://hg.mozilla.org/integration/autoland/rev/5c88e66a73e6 browser.downloads.download throws error with filenames having multiple spaces. r=zombie
Iteration: --- → 79.1 - June 1 - June 14
Points: --- → 2
Status: ASSIGNED → RESOLVED
Closed: 4 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla79

Is this something we should consider uplifting to Beta78?

Flags: needinfo?(mak)
Flags: in-testsuite+

Comment on attachment 9154680 [details]
Bug 1637973 - browser.downloads.download throws error with filenames having multiple spaces. r=zombie

Beta/Release Uplift Approval Request

  • User impact if declined: WebExtension downloads api doesn't act properly, some valid downloads may fail
  • Is this code covered by automated tests?: Yes
  • Has the fix been verified in Nightly?: No
  • Needs manual test from QE?: No
  • If yes, steps to reproduce: The automated test is enough.
  • List of other uplifts needed: None
  • Risk to taking this patch: Low
  • Why is the change risky/not risky? (and alternatives if risky): Very simple patch with a test
  • String changes made/needed:
Flags: needinfo?(mak)
Attachment #9154680 - Flags: approval-mozilla-beta?

Comment on attachment 9154680 [details]
Bug 1637973 - browser.downloads.download throws error with filenames having multiple spaces. r=zombie

approved for 78.0b6

Attachment #9154680 - Flags: approval-mozilla-beta? → approval-mozilla-beta+
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: