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

Send 404 instead of 500 when filename requested is too long on StaticFiles #2583

Merged
merged 4 commits into from
Jun 1, 2024

Conversation

Jaza
Copy link
Contributor

@Jaza Jaza commented Apr 30, 2024

Summary

If a static file with a really long name is requested, then it causes an OSError to be raised saying "Filename too long". This error currently gets propagated, resulting in a 500 response. In such a case, the name is too long for the underlying OS to handle, therefore a file with that name couldn't possibly exist, therefore it would be more appropriate to not propagate the error and to return a 404 response.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Note: I feel that the existing documentation is adequate, it already states:

Static files will respond with "404 Not found" or "405 Method not allowed" responses for requests which do not match.

@Kludex Kludex changed the title Make "Filename too long" return 404 not 500 Send 404 instead of 500 when filename requested is too long on StaticFiles Jun 1, 2024
Copy link
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Jaza !

Relying on regex is usually not a good idea for exceptions. I've used the errno instead.

I've also changed the test to use the max path on each OS that runs the test.

@Kludex Kludex enabled auto-merge (squash) June 1, 2024 13:05
@Kludex Kludex merged commit 4f16aed into encode:master Jun 1, 2024
5 checks passed
@Jaza Jaza deleted the filename-too-long branch June 2, 2024 00:33
@Jaza
Copy link
Contributor Author

Jaza commented Jun 2, 2024

Thanks @Kludex ! TIL you can be more specific about OSError s with errno . And I guess your google-fu (or your background knowledge of such things) is superior to mine, I did search for a way to programatically get the max path size, but I didn't end up finding PC_PATH_MAX , so TIL++.

nixroxursox pushed a commit to nixroxursox/starlette that referenced this pull request Jul 20, 2024
…cFiles` (encode#2583)

* Make "Filename too long" return 404 not 500

* Simplify code source

* Correct comment

---------

Co-authored-by: Jeremy Epstein <jeremy.epstein@seertechsolutions.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
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

Successfully merging this pull request may close these issues.

2 participants