File and page link in browser ...

File and page link in browser ...

by Julien Rey -
Number of replies: 22

Hi,

I have been using moddle for several months and I'm very happy of this great script.
However, last week, with my new students, I saw (with the log feature) a student who accessed a hidden ressource (a file) in a course.

I asked him how he did that. He answered me that he saw the link of the previous (and last shown) file in the browser, like : www.xxxxx.org/moodle/mod/resource/view.php?inpopup=true&id=10
So, he had the idea to check if there was not other files after this one and he tried :
www.xxxxx.org/moodle/mod/resource/view.php?inpopup=true&id=11

He had the great surprise to see and download the next file of my course which was the correction of an exercice for the next week... Good for him...not for me...

So, I just wonder if there is any possibility to change the format of the link. For security reasons, it would better if the IDs of the files were not in logical order.
The ID could be a random number indexed in the database ?

With this solution, students (skilful with computers) won't be able to download and see files before they are displayed on the course.

Thanks for your help and for all your suggestions in order to avoid a new file robbery wink before time...

Julien

Average of ratings: -
In reply to Julien Rey

Looks like a 1.4.1 (+?) bug.

by Michael Penney -

Hmm, this appears a major bug in 1.4.1 (Moodle 1.4.1+ (2004083101)). I just checked in 1.4.1 I get the same thing, but in 1.3.2, I get:

The correct "Sorry, this activity is currently hidden" behavior when trying to navigate to a hidden resource.

Are you using a standard install of Moodle and if so what version?

Average of ratings: Useful (1)
In reply to Michael Penney

Re: Looks like a 1.4.1 (+?) bug.

by Julien Rey -

Thank you for your answer.
I use the same version as you do : Moodle 1.4.1 (2004083101).
So as you said, It seems to be a serious bug of the new version.
I hope, the team will solve it as soon as possible.

Thank

Julien

In reply to Julien Rey

Re: File and page link in browser ...

by Herbert Keijers -
I think you are right. sad
I'm now on a testing machine, moodle 1.4.2 nearly (2004083121), and it behaves exactly as you discribe.
Michael Penney is right to say that the bug wasn't in 1.3

Hope it will be fixed soon ...

Has some already filed a bug report ?
In reply to Herbert Keijers

Re: File and page link in browser ...

by Petr Skoda -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
As a quick fix for version 1.4.1+ you can add this:


if (!$course = get_record("course", "id", $cm->course)) {
    error("Course is misconfigured");
}

if (!$cm->visible and !isteacher($cm->id)) {

    notice(get_string("activityiscurrentlyhidden"));
}


to the mod/resource/view.php right after line 15.

IMHO it should be fixed in resource_base constructor and also there should be printed some header (to set up correctly the encoding and theme).

skodak

Average of ratings: Useful (1)
In reply to Petr Skoda

Re: File and page link in browser ...

by Herbert Keijers -
Hi skodak,
digging up some older code like
if (!$cm->visible and !isteacher($course->id)) {
        print_header($pagetitle, "$course->fullname", "$navigation $resource->name", "", "", true,
                     update_module_button($cm->id, $course->id, $strresource), navmenu($course, $cm));
        notice(get_string("activityiscurrentlyhidden"));
    }
might solve the theme problem.

Thanks
In reply to Herbert Keijers

Re: File and page link in browser ...

by Petr Skoda -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hi!

Showing the $resource->name is not a good idea, the resource should be hidden completely (including its name). Update_module_button is not appropriate here too, because it is useful for the editing teachers only.

skodak
In reply to Petr Skoda

Re: File and page link in browser ...

by Julien Rey -

Hi Petr,

Could you tell me which files I have to modify, in order that the error message (your fix displays when trying to access an hidden resource) is displayed with my site theme.

I didn't understand exactly what you mean by "IMHO it should be fixed in resource_base constructor and also there should be printed some header (to set up correctly the encoding and theme)."

Thanks,
Julien

In reply to Julien Rey

Re: File and page link in browser ...

by Petr Skoda -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hi!

I do not recommend using the "quick fix" after the bug is fixed in CVS, the final patch in CVS is much more complex. The only problem is that it takes several hours before the update takes effect in the anonymous CVS (1.4.1+ == soon to be released 1.4.2) or nightly download.

The comments were only some hints for the developers wink

skodak
In reply to Julien Rey

Re: File and page link in browser ...

by Mad Alex -
We have experienced a similar problem here in York. I have given some thought to this issue and have produced a patch which (I hope) will comprehensively fix the issue.

The issue is potentially much more complicated than suggested here. Some modules may wish to release files only when a condition is met (eg. the exercise module does not display the link to the file when in phase 1 and in this case the URL to file.php should not work either). Also a module (including a resource) could be made visible and later hidden (eg. if it was made visible by mistake) and in this case the correct URL will be known, even if the id is randomised.

I hope you all agree that the best solution would be for file.php to check if access should be granted on each request.

Where a module stores a file within its own directory under the moddata directory a fix is fairly simple. The ownership of the file is clear and all that is needed is a mechanism for file.php to check if the module instance is visible and if so check that access is allowed at the present time.

I propose that a function be defined in the lib.php of each module for file.php to call with the course module object and the filename with a return value of true if access is allowed, false otherwise. If the function does not exist it should be assumed that access is always allowed (to maintain backward compatibility with existing modules). In my patch I have opted to call this function MODULENAME_allow_file_access.

Note that this test occurs after the visibility test; access is automatically denied when a module instance is hidden with no change required to the module code. Note also that the tests are not made if the user is a teacher since teachers can see hidden module instances and can access files directly through the file area anyway.

Where files are stored in the course directory things get more complicated, especially if we are to maintain backward compatibility with old modules. We need to establish ownership of the file (and there may be multiple owners). We need to indicate which module and which instance of that module the file belongs to in the URL so that file.php can check visibility and call MODULENAME_allow_file_access. However we need to verify that the file really is used by that module instance, otherwise access to a hidden resource could be made by simply passing the id for a different resource which is visible.

In order to maintain backward compatibility I propose that files in the course directory and (most) subdirectories are unprotected, i.e. the status quo. However a special directory should be created (which I propose to call .protected as directories starting with a period will be hidden automatically by directory resources) and all files and directories within this will be protected. Access will not be allowed directly to these files by file.php (under any circumstances); instead the URL gives a path within the moddata directory which will be collapsed to the real path. This is easiest to explain by example: 2/moddata/resource/1/.protected/file.ext --> 2/.protected/file.ext with the resource module instance with id 1 asked for permission to serve the file. Note that here the file 2/moddata/resource/1/file.ext does not need to exist and in fact will not be accessible if it does exist as the path will always be collapsed.

Note that this mechanism assumes that nobody uses directories by the name of .protected within their course directories or within a module directory or can easily change (another reason for the name to start with a period).

The protection in my patch is provided by yet another new function in the lib.php for the module. This is passed the course module object and the filename again and is expected to return true if the object belongs to the module instance, false otherwise. Again it is optional but this time defaults to false if not present; thus the student can not change the url to a module which has not been updated to use this mechanism to bypass the check. This is why we can not apply the mechanism to all files but need a special directory. In my patch I call the new function MODULENAME_is_module_file.

Note that old modules will not be able store files in the .protected directory as they will not be accessible and teachers will need to be told which modules do and do not support the security mechanism. Does anybody have any suggestions for what could be done about this? My only suggestion so far is that modules which have been patched should display a message saying "use the .protected dir if you want security" and a message in the file area saying "don't use the .protected directory unless the module tells you that you may". I have not implemented anything yet (we are mainly concerned with making things secure a.s.a.p. here at York).

I have patched the resource module (file and directory types) and exercise module to use my new system. While I was testing the resource module I found a flaw in the directory resource type which I have also fixed in the patch; the moddata directory and any directories starting with a period are removed from the view but the subdir argument is not checked so these directories can still be viewed and navigated once one has correctly guessed the name.

The attached diff is against version 2004083121 (MOODLE_14_STABLE from 8th Oct). Apply in the moodle directory with `patch -p1 < moodle-file-security.diff'. I'll try it against MOODLE_141 and the latest cvs code and report back. I was going to do this first and post later in the week but since this topic has come up...

I should point out that I made the changes against a copy from cvs so the patch contains some of the cvs changes but these are all minor in the files I changed and will do no harm.

Oh, and I couldn't get a less than to work on any formatting setting; everything after it was removed as if it formed a tag and the entity does not seem to be decoded even for html formatting. So please translate the entity manually!
In reply to Mad Alex

Re: File and page link in browser ...

by Mad Alex -
Here is the patch against cvs (created with cvs diff -u). Nothing had changed in the files I edited which was nice! smile

I'll create a patch for the stable release (1.4.1) and post that soon...
In reply to Mad Alex

Re: File and page link in browser ...

by Mad Alex -
...and here it is. This should patch 1.4.1 to provide file security with or without a fix for the issue raised here. We might still want to fix view.php to provide a nicer error message .
In reply to Mad Alex

Re: File and page link in browser ...

by Frederic Triquet -
Hi,
posting a followup to an old thread is maybe not a good idea but I did not find any more recent thread dealing with the problem I have.

It seems that any student can download a file from a course by guessing and typing the appropriate URL, for instance :
http://mymoodle/file.php?file=/12/b.pdf
this URL allows any user registered to the course (the one with id==12) to get the file "b.pdf", even if there is no related ressource pointing to this file or if such a ressource is hidden.

This has been a problem in my school :
A teacher provided the students with "solutions1.pdf", "solutions2.pdf", etc, he only displayed the first of them, and the students found how to download the next ones before he made them public.


My moodle version is 1.4.3 stable release without "slasharguments"

Fred
In reply to Frederic Triquet

Re: File and page link in browser ...

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
This will be (is) fixed in Moodle 1.5.
In reply to Martin Dougiamas

Re: File and page link in browser ...

by Frederic Triquet -
Great, thank you very much !
In reply to Frederic Triquet

Re: File and page link in browser ...

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Sorry, Frederic, I was under the wrong impression myself and misled you here about the fixes in 1.5 ... the fixes we currently have will prevent students from accessing other students assignments and uploaded files, but won't prevent arbitrary access to teacher-uploaded files by students who can guess the filename.

This is a more difficult problem which we are currently discussing on security.moodle.org. There is a good idea and patch from Mad Alex but ideally I would like to see this solved properly within the DMS using proper access-control, and this patch will further complicate that transition when it comes.

In the meantime, if you have really sensitive files and really inquisitive students then the best idea would be not to upload them.
In reply to Martin Dougiamas

Re: File and page link in browser ...

by Michael Penney -
One could also give sensitive files a long and complicated filename (the Blackboard solution).

Michael Penney
In reply to Mad Alex

Re: File and page link in browser ...

by Thomas Korner -
Picture of Plugin developers Picture of Testers
I know, this thread is very old...

but we observe the same behaviour in a recent version of moodle (1.9.5):

A student who knows the url (like"...file.php/123/abc.jpg") can download a file although it is set to hidden. He has to be member of the course with the ID 123.

Did we setup anything wrong? Due to the age of the thread I think this problem could be solved meanwhile.

Many thanks

Thomas
In reply to Thomas Korner

Re: File and page link in browser ...

by Oliver Rahs -
I guess there is a problem in the following condition in "file.php" (root directory of installation, Moodle 1.9.5):


// security: some protection of hidden resource files
// warning: it may break backwards compatibility
if ((!empty($CFG->preventaccesstohiddenfiles))
and (count($args) >= 2)
and (!(strtolower($args[1]) == 'moddata' and strtolower($args[2]) != 'resource'))
and (!has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $course->id)))) {..........}


The variable "$CFG->preventaccesstohiddenfiles" is always empty. I couldn't find a place to set a value to this variable.
-> The code in the curly brackets is not executed and the students can see the hidden files too.


I've changed the condition to:

if ((count($args) >= 2)
and (!(strtolower($args[1]) == 'moddata' and strtolower($args[2]) != 'resource'))
and (!has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $course->id)))) {..........}


and then it seems to work. The teachers can access and see the hidden files, students can't. Are there any side effects to delete "!empty($CFG->preventaccesstohiddenfiles)" in the condition? What is this variable used for?

Oliver
In reply to Oliver Rahs

Re: File and page link in browser ...

by Oliver Rahs -
Another possibility is to manually add a new entry "preventaccesstohiddenfiles" with the value "1" into the moodle-db (table "mdl_config", mdl is the prefix):

INSERT INTO mdl_config (id,name,value) VALUES (NULL,'preventaccesstohiddenfiles','1');

Then no changes in the file "file.php" are required.
Average of ratings: Useful (1)
In reply to Oliver Rahs

Re: File and page link in browser ...

by Patrick Pollet -

Hello,

   Raising this to note that this is not yet fixed in latest 1.9.15+ where preventaccesstohiddenfiles still does not

exists in mdl_config table, which is quite surprising (see http://moodle.org/mod/forum/discuss.php?d=192603#p838972

Our fix is to have this value set in config.php file. 

$CFG->preventaccesstohiddenfiles=1 ; 

I just opened a bug report of Moodle Tracker http://tracker.moodle.org/browse/MDL-30863

Cheers.