Files not downloaded in IIS with special characters +++

Files not downloaded in IIS with special characters +++

Олексій Буренко -
回帖数:29

Hello.

I installed moodle on the Windows server 2019 server.

If you upload a file with a name such as 1111+++.pptx , it loads successfully, but when you try to open it, you get the error "File not found"

If you rename it to 1111.pptx or 1111---.pptx, then everything works fine.

Tell me how to add support in IIS for Moodle files like 1111+++.pptx (with pluses, there may be bugs for other characters and I would like to support all valid characters.)

Initially, there was a problem in general with file names with Russian and Ukrainian characters, the solution on the website https://docs.moodle.org/400/en/Internet_Information_Services#URL_rewriting helped

URL rewriting rule has been added

Matches the Pattern - Regular Expressions - ^([^\?]+?\.php)(\/.+)$

Action - Rewrite - {R:1}\?file={R:2}

Append query string - enabled

Stop processing of subsequent rules - enabled

Maybe I need to correct it somehow to solve the problem, but I don’t understand regular expressions


Ask the community for help

回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -

I realized that the problem is in the plus sign (+)

I found on the Internet that you need to enable Allow double escaping (Request filtering - allow double conversion)

via the link https://helpx.adobe.com/experience-manager/kb/CannotOpenAFileHavingSpecialCharactersInTheFilenameOnIIS.html

If you just download the file from the server, for example https://ippo.dn.ua/1111+++.pptx, then now the file is downloaded, even from the link https://ippo.dn.ua/1111%2B%2B%2B. pptx

But the problem with the moodle still remains, the file is uploaded, but not downloaded...

回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Visvanath Ratnaweera -
Particularly helpful Moodlers的头像 Translators的头像
This is a philosophical problem, not a problem of IIS or Moodle per se.
wink

In an ideal world the file names may contain _any_ UTF-8 character. In the real world there are very many combination that one platform or the other does not like. Name a file --foo or .bar and upload it to Moodle. Ask a Linux user to download those files, list the downloaded directory and and delete the files in the command shell!

Every OS has its blind spot. You are discovering one in the Windows world. The thing difficult to fathom is that the world is inherently diverse. Your desktop is not the Web!

回复Visvanath Ratnaweera

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -
This issue is open on https://tracker.moodle.org/browse/MDL-23567
In this case, you can only vote for it in order to speed up and motivate developers to find a solution to this issue.
回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Howard Miller -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 Plugin developers的头像
Yep - you can upload a file from (say) a Mac which has a valid filename on that system that will, however, not open on Windows. Possibly vice versa - although, it's usually Windows that is the problem.
回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Matteo Scaramuccia -

Hi Алексей Буренко,
you should stop using the configuration path "URL Rewriting" and adopting the slash arguments path even in IIS: you should just keep care of https://docs.moodle.org/400/en/Internet_Information_Services#Optional_UTF-8_file_name_fix.

Please keep care of allowDoubleEscaping as discussed here.

I'll properly update the Wiki if you'll confirm that moving to pure "slash arguments" will fix your issue - the URL Rewriting path was for old IIS versions almost ten years ago and it's no more suitable for most of the installations and some Moodle modules force the slash arguments regardless your configuration.

HTH,
Matteo

回复Matteo Scaramuccia

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -
Thanks, I didn't finish watching.
I deleted "URL Rewriting"
and add (although it has already been added) 
reg add HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\w3svc\Parameters /v FastCGIUtf8ServerVariables /t REG_MULTI_SZ /d REQUEST_URI\0PATH_INFO
Now UTF-8 filenames work correctly, download without errors
But files with a plus sign are still not downloading
I have such errors
if <requestFiltering allowDoubleEscaping="false" />
allowDoubleEscaping=false
if <requestFiltering allowDoubleEscaping="true" />
allowDoubleEscaping=true
回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -
My moodle course https://test.ippo.dn.ua/moodle/course/view.php?id=2
username testplus
password   Testplus+1
回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Matteo Scaramuccia -

Hi Алексей,
we should try to debug on your installation since we need to find what kind of "path" the Moodle storage will receive and how your file has been described in the Moodle database.

We could start with https://test.ippo.dn.ua/moodle/pluginfile.php/32/mod_resource/content/0/2+.pptx by editing the pluginfile.php file - make a backup first - and print out all the variables given to the function call at this line: https://github.com/moodle/moodle/blob/c1910fc822182bedca9c7768fe61e9c90b27ac70/pluginfile.php#L44 ; $relativepath is the sensitive one.

Are you a bit familiar with PHP coding?

Matteo

回复Matteo Scaramuccia

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -
Unfortunately, I'm not strong in PHP, I did not change the moodle files. Here is what is written in my file pluginfile.php

<?php
if (!defined('NO_DEBUG_DISPLAY')) {
    define('NO_DEBUG_DISPLAY', true);
}
require_once('config.php');
require_once('lib/filelib.php');
if (empty($relativepath)) {
    $relativepath = get_file_argument();
}
$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
$preview = optional_param('preview', null, PARAM_ALPHANUM);
// Offline means download the file from the repository and serve it, even if it was an external link.
// The repository may have to export the file to an offline format.
$offline = optional_param('offline', 0, PARAM_BOOL);
$embed = optional_param('embed', 0, PARAM_BOOL);
file_pluginfile($relativepath, $forcedownload, $preview, $offline, $embed);
回复Matteo Scaramuccia

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -
Familiar haze with PHP
i change pluginfile.php
$offline = optional_param('offline', 0, PARAM_BOOL);
$embed = optional_param('embed', 0, PARAM_BOOL);
var_dump($relativepath, $forcedownload, $preview, $offline, $embed);
die;

file_pluginfile($relativepath, $forcedownload, $preview, $offline, $embed);

Result is
string(34) "/32/mod_resource/content/0/2 .pptx" int(0) NULL int(0) int(0)

Here is no sign plus in 2 .pptx
回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -

I noticed that if you change the plus symbol in the filename field to %2B in the database, then the file is downloaded, but this is not quite the correct way, errors appear when editing the file and for some reason it does not work with jpg images

https://moodle.org/pluginfile.php/133/mod_forum/post/1756197/db.JPG

回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Matteo Scaramuccia -

Hi Алексей,

if you change the plus symbol in the filename field to %2B in the database, then the file is downloaded

Yep, that is not the proper way to work, "+" should be stay there in the database.
That being said, I'm a little confused on your tests:

  1. You said that the plus character is no more there in $relativepath which is the candidate issue
  2. You said that changing the "+" in the files table into "%2B" fix the issue, which is contrast with (1)

Could you clarify the results of your tests?

HTH,
Matteo

回复Matteo Scaramuccia

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -
Hi Matteo
1. If you display the $relativepath variable, then there is no plus sign, something tells me that it should be there and probably this is due to the PHP code and the Windows server
2. Changing the plus symbol in the database solves the problem of downloading the file. The file is then downloading. But it doesn't work quite right. I would like not to use queries for manual editing of the database

If you apply the second solution then the variable $relativepath looks like 
string(37) "/35/mod_resource/content/0/2+2+2.pptx" int(0) NULL int(0) int(0) 
for the file 2+2+2.docx 
and file downloading

He is screen database of both variants

File 2+.pptx not downloading, getting error
File 2+2+2.pptx downloaded, after editing in the database

回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Matteo Scaramuccia -

Hi Алексей,

If you apply the second solution then the variable $relativepath looks like
string(37) "/35/mod_resource/content/0/2+2+2.pptx" int(0) NULL int(0) int(0)
for the file 2+2+2.docx
and file downloading

what is the second solution? I.e. removing URL Rewriting in IIS and enabling slash arguments in Moodle?
That was my first suggestion, just in case 眨眼.

If the $relativepath looks like /35/mod_resource/content/0/2+2+2.pptx, Moodle will be able to match that info with the file record in the files table without the need to alter + with %2B. But... you need to apply "my" second solution above.

HTH,
Matteo

回复Matteo Scaramuccia

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -
The second solution is to edit the entry in the Moodle database via PHPmyadmin.
as i already wrote
2. Changing the plus symbol in the database solves the problem of downloading the file. The file is then downloading. But it doesn't work quite right. I would like not to use queries for manual editing of the database
And it's not a solution, just an observation
回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Matteo Scaramuccia -

Hi Алексей,

Here is no sign plus in 2 .pptx

OK we've found what could be wrong in the Moodle stack; I do not have a running Moodle on Windows, I'll try to setup something on spare time - best effort i.e. no guarantee.

HTH,
Matteo

回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Ken Task -
Particularly helpful Moodlers的头像

Pardon interruption ...

RFC 2396 https://www.ietf.org/rfc/rfc2396.txt
is the governing document for "Uniform Resource Identifiers (URI): Generic Syntax." This RFC defines what can and can't be used in a URI, as well as what shouldn't be used.

First, section "2.2. Reserved Characters" contains the following list of reserved characters:

reserved = ";" | "/" | "?" | ":" | "@" |
           "&" | "=" | "+" | "$" | ","

'SoS', Ken



回复Ken Task

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -
Yes, of course you are right. But the plus symbol on the Apache server works and there are no problems with the files
Now there is a need to transfer the Moodle site to a server with Windows, where many files have already been uploaded in the names of which there is a plus.
回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Matteo Scaramuccia -

Hi Алексей,

Now there is a need to transfer the Moodle site to a server with Windows, where many files have already been uploaded in the names of which there is a plus.

Ahhh... now I understand why you're in that need about plus characters and why you've been able to save those files!

Would you share how did you perform the "stack transfer"? E.g.:

  1. Moodle running under Apache on Linux
  2. Same Moodle code (wwwroot) as when running under Apache
  3. Same Database - backup&restore or same server - as when running under Apache
  4. Same Moodle dataroot - backup&restore - as when running under Apache

TIA,
Matteo

 

回复Matteo Scaramuccia

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -
When I discovered the problem after migrating the old moodle to the new server, I did a clean install of the latest version of the moodle. It showed the same problem as after the transfer.
Therefore, it is not a problem of moving the moodle from one server to another.
I gave all links to a purely installed moodle.
回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Visvanath Ratnaweera -
Particularly helpful Moodlers的头像 Translators的头像
> Now there is a need to transfer the Moodle site to a server with Windows,

So basically a downgrade. There is a price you have to pay. Hopefully, these + signs are the only one.

Sorry for the not so helpful philosophical note. You know technology is based on physics. Physics in turn is based on philosophy.
眨眼
回复Ken Task

Re: Files not downloaded in IIS with special characters +++

Matteo Scaramuccia -

Hi Ken,

RFC 2396 https://www.ietf.org/rfc/rfc2396.txt
is the governing document for "Uniform Resource Identifiers (URI): Generic Syntax." This RFC defines what can and can't be used in a URI, as well as what shouldn't be used.

First, section "2.2. Reserved Characters" contains the following list of reserved characters:

you're right! Unfortunately Moodle does not enforce such a policy.

See also what I wrote here.

HTH,
Matteo

回复Matteo Scaramuccia

Re: Files not downloaded in IIS with special characters +++

Visvanath Ratnaweera -
Particularly helpful Moodlers的头像 Translators的头像
Matteo wrote:
> Ken wrote:
>> RFC 2396 https://www.ietf.org/rfc/rfc2396.txt is the governing document for "Uniform Resource Identifiers (URI): Generic Syntax."
> you're right! Unfortunately Moodle does not enforce such a policy.

What is Moodle's policy (on file names)?

Is the lack of thereof the reason i was called a troll when mentioned Posix file names https://moodle.org/mod/forum/discuss.php?d=190958#p831105 ?
回复Matteo Scaramuccia

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -
But on the Linux server everything works the same ...
Then it would not be bad to add a plug-in to the moodle that will check file names on upload
回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Ken Task -
Particularly helpful Moodlers的头像

What's the purpose in naming a file with whatever+2+2+2.whatever extension?

How many files are there like that?

If they are Microsoft docs/files, the filenames are not contained in the file are they?   Files can be renamed.

The goal: those files be downloaded.  IF such naming is preventing without having to do re-coding, tweaking, or whatever on server, why not just change the filenames in DB and be done with it?   Path of least resistance?

Guess I don't understand ...

'SoS', Ken



回复Ken Task

Re: Files not downloaded in IIS with special characters +++

Олексій Буренко -

Where else can I find a working query to edit the database.

I tried two queries

UPDATE mdl_files SET filename = REPLACE(filename, '+', '');

UPDATE mdl_files SET source = REPLACE(source, '+', '');

After executing the requests, the file is still not downloaded, if you go to edit the file and click the save button, then everything works.

I understand that something else needs to be done in order for everything to work. But I don't understand what...

回复Олексій Буренко

Re: Files not downloaded in IIS with special characters +++

Ken Task -
Particularly helpful Moodlers的头像

In the mdl_files table there is a 'timemodified' column and others that might also need 'bumping'.

Example:

mysql> select filearea,filename,status,timecreated,timemodified from mdl_files where filename like '%.docx';

Example of one row:

| submission_files          | unoconvtest.docx       |      0 |  1601075532 |   1601075536 |

timeblah columns are epoch time stampts.

Assuming file was uploaded to moodle and not in a remote repository of some sort.

'SoS', Ken