Skip to content

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

Merged
Kludex merged 4 commits intoKludex:masterfrom
Jaza:filename-too-long
Jun 1, 2024
Merged

Send 404 instead of 500 when filename requested is too long on StaticFiles#2583
Kludex merged 4 commits intoKludex:masterfrom
Jaza:filename-too-long

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.

@Jaza Jaza force-pushed the filename-too-long branch from 2437fe4 to 6acdbe8 Compare April 30, 2024 07:11
@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
Owner

@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 Kludex:master Jun 1, 2024
@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++.

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