Live data from Hacker News

If your code accepts URIs as input, filter out “file://”

blog.steve.fi

21–30 of 161 posts

Re: If your code accepts URIs as input, filter out “file://”

#21
post #8

Earlier quoted context omitted.

If thats the only validation on calls to file_get_contents, that could very easily be bypassed. Entering something like just "/etc/passwd" for example.

/etc != http :)

Ah yes, you're right. Totally mis-read the code.

It'd still leave access to any files in the same (or sub) directory starting with http, which realistically would probably be none but still something to bear in mind.

Re: If your code accepts URIs as input, filter out “file://”

#23
post #12

I tried Python requests and Common Lisp drakma, and neither of them can handle "file://" URL schema. Which HTTP client libraries are actually vulnerable to this?

urllib/urllib2's urlopen for example. I would assume it would be a bug for the requests library to allow file:///

Re: If your code accepts URIs as input, filter out “file://”

#24

Wrong way around: only allow http:// and https:// (and generally filtering out anything thats not letters, numbers, slash or dot is probably a good idea. Remove any sequences of more than one slash or dot.

>Wrong way around: only allow http:// and https:// For myself, it's a subtle change in developer thinking - "what should I allow" vs "what should I exclude" - that's paid off massively over the years.

https://en.m.wikipedia.org/wiki/Robustness_principle

Re: If your code accepts URIs as input, filter out “file://”

#25
"Despite reporting the problem to the author on Friday, and following up the report via Twitter this has not yet been fixed, but after four days I assume I'm not alone in spotting this."

Giving someone a weekend to fix something doesn't exactly sound like responsible disclosure. I understand if you get excited because you found a flaw but if you find something like this please be more responsible with publishing your findings.

Re: If your code accepts URIs as input, filter out “file://”

#27

Earlier quoted context omitted.

>Wrong way around: only allow http:// and https:// For myself, it's a subtle change in developer thinking - "what should I allow" vs "what should I exclude" - that's paid off massively over the years.

https://en.m.wikipedia.org/wiki/Robustness_principle

>https://en.m.wikipedia.org/wiki/Robustness_principle

You need to be careful of where you place the emphasis on that, though:

Be -liberal- in what you accept.

vs

Be liberal in what you -accept-.

Re: If your code accepts URIs as input, filter out “file://”

#28
Also be sure not to allow loopback connections (ie your own site or localhost) or you can cause a deadlock if the user requests the same URL to download from your site recursively. Choose an appropriate timeout too to prevent users tying up backend processes with a HTTP server that is slow to respond, and don't follow Location headers to avoid bypassing of your initial filters. A Range header should also be used to prevent users from telling your server to download multi-GB files and causing bandwidth waste / denial of service.

Re: If your code accepts URIs as input, filter out “file://”

#29

Wrong way around: only allow http:// and https:// (and generally filtering out anything thats not letters, numbers, slash or dot is probably a good idea. Remove any sequences of more than one slash or dot.

(Also be weary of imagetragick-type bugs too, where the URL starts innocuously and then contains some shellcode, because you pass the URL to something that'll paste it into system() call)

I certainly am weary of bug branding...

Re: If your code accepts URIs as input, filter out “file://”

#30

Earlier quoted context omitted.

https://en.m.wikipedia.org/wiki/Robustness_principle

> https://en.m.wikipedia.org/wiki/Robustness_principle You need to be careful of where you place the emphasis on that, though: Be - liberal - in what you accept. vs Be liberal in what you - accept -.

Right - you accept URIs. That's fairly liberal.

> Be conservative in what you do

However, you only handle specific schemes and ignore the rest.

Post reply on HN