Git Product home page Git Product logo

Comments (12)

justinas avatar justinas commented on September 21, 2024 1

@bryanjeal Now that manual token verification is available, do you think it serves as a decent workaround to use when parsing multipart forms manually?

from nosurf.

justinas avatar justinas commented on September 21, 2024

I'm kind of stuck on how to get the both sides happy here. :(

from nosurf.

bryanjeal avatar bryanjeal commented on September 21, 2024

I wondered if using MultipartReader() would cause problems because it is trying to stream in the data.

Would it be possible to expose verifyToken()? Then I can mark my route as exempt, and process the CSRF token outside of your middleware.

from nosurf.

dmitshur avatar dmitshur commented on September 21, 2024

I'm kind of stuck on how to get the both sides happy here. :(

What's the other side?

from nosurf.

justinas avatar justinas commented on September 21, 2024

@shurcooL if nosurf parses multipart forms, they cannot be parsed in alternative ways by the end-user. If nosurf doesn't, it will not be able to verify multipart requests.

from nosurf.

dmitshur avatar dmitshur commented on September 21, 2024

What about this. Make a copy of the Request Body, then parse it. But restore the Body before calling the success http.Handler. Similar to how httputil.DumpRequest does it (see its source code).

from nosurf.

elithrar avatar elithrar commented on September 21, 2024

Copying the entire request body could/can consume
a non-negligible amount of memory (and therefore garbage).
On Tue, 4 Aug 2015 at 5:20 pm Dmitri Shuralyov [email protected]
wrote:

What about this. Make a copy of the Request Body, then parse it. But
restore the Body before calling the success handler. Similar to how
httputil.DumpRequest https://godoc.org/net/http/httputil#DumpRequest
does it (see its source code
http://gotools.org/net/http/httputil#dump-go-L177-L193).


Reply to this email directly or view it on GitHub
#27 (comment).

from nosurf.

dmitshur avatar dmitshur commented on September 21, 2024

@elithrar That is a good point, making a copies of the request body can be detrimental to performance.

I wonder if it can be optimized by copying a part of the body until the CSRF token is found (and suggesting making it the first POST parameter), for example. Or some other means.

On another note, unless something here changes, I think the current documentation can be improved. It says:

... acts like a middleware and therefore is compatible with basically any Go HTTP application.

  • Supports any http.Handler (frameworks, your own handlers, etc.) and acts like one itself.

It should be noted that this middleware may call ParseMultipartForm and ParseForm, therefore populating request's Form, PostForm, and MultipartForm fields, but consuming the body in the process.

Therefore, it will not work with http.Handlers that expect the original request body to be there unmodified.

from nosurf.

elithrar avatar elithrar commented on September 21, 2024

That might work, but it's a pretty sharp edge: if the token is after the other form values (e.g. a large file upload) that's a ton of implicit copying, especially for existing users.

Like @justinas said, I'm not quite sure how we can address this (my own CSRF lib has the same problem) and make both sides happy. Perhaps an option (not a huge fan of providing more options) to just do the copy operation (copy -> restore body) when parsing the multipart form? At least that way package users have some control over it.

from nosurf.

dmitshur avatar dmitshur commented on September 21, 2024

I'm not quite sure how we can address this

What do you think about my suggestion in #27 (comment) to improve documentation?

from nosurf.

elithrar avatar elithrar commented on September 21, 2024

Agree with improving the docs. I also think providing an example "how to" for package users (i.e. how to do the copy themselves) is worthwhile though: it can be a real pain.

from nosurf.

bryanjeal avatar bryanjeal commented on September 21, 2024

@justinas That is exactly what I was hoping for.

Thank you very much.

Closing this issue.

from nosurf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.