Skip to content

Added length validation for RequestBodyStream #205

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

Merged
merged 6 commits into from
Jul 4, 2016
Merged

Added length validation for RequestBodyStream #205

merged 6 commits into from
Jul 4, 2016

Conversation

sanketr
Copy link

@sanketr sanketr commented Jun 29, 2016

This is proposed fix for #204. Tested with my code to verify that the error doesn't happen any more in case of wrong length, and that when the length is correct, data upload goes through.

I repurposed writeStream machinery and added length check for when chunking is false - not elegant solution :(

Now, there is an additional branch - pseudo-code changes in bold below -
Before: if isChunked then return terminator else return ()
After: if isChunked then return terminator else (if length error return 400 status code else return ())

Not sure if Status code 400 is right one here. It seems to be appropriate for client-side error, to me.

If anything needs to be changed, please do let me know.

where
loop stream = do
loop n stream = do
Copy link
Owner

Choose a reason for hiding this comment

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

This should probably be !n to avoid a space leak.

@snoyberg
Copy link
Owner

I actually think it's a perfectly elegant solution. I put in some inline comments. My only other thought: this deserves its own exception constructor instead of using the StatusCodeException. How about IncorrectRequestBodyStreamSize or something like that?

@sanketr
Copy link
Author

sanketr commented Jun 30, 2016

Yep, agree it should be its own exception constructor. WrongRequestBodyStreamSize Word64 Word64 perhaps (same layout as ResponseBodyTooShort).

I have also applied all the changes that you suggested.

Also, tested all of the changes including exception message (verified it prints correct values - caveat int64 to word64 conversion in case of negative length argument but this is expected).

if isChunked then connectionWrite "0\r\n\r\n"
-- If not chunked, then length argument is present
-- and should be validated
else unless (fromJust mlen == n) $ throwIO $ WrongRequestBodyStreamSize (fromIntegral . fromJust $ mlen) (fromIntegral n)
Copy link
Owner

Choose a reason for hiding this comment

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

I don't feel comfortable including a partial function like fromJust here. Why not just pattern match on mlen instead of using isChunked?

Copy link
Author

Choose a reason for hiding this comment

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

Yep, agree. Removed partial function in latest commit.

@snoyberg snoyberg merged commit 81d7077 into snoyberg:master Jul 4, 2016
snoyberg added a commit that referenced this pull request Jul 4, 2016
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