ryanwinchester
Verifying requests in accordance with the IETF HTTP Signatures specification Draft specification.
Lots of places have signature verification for their webhooks (for example GitHub, Stripe, Dropbox, Samsung SmartThings, and many many more), so it seems like it would be something Phoenix (or Plug) should be concerned with being able to do so without overriding core modules and functionality.
The problem is we need to verify the payload (the request body), and that is read only once in Plug.Parsers and discarded.
This solution could work but it’s weird if I want to receive and verify webhooks from different services as well as other issues (like not being able to use controllers).
Is there no way to get a better solution for this, maybe as a hex package (I’d write one if I knew a good way to solve this) or in Phoenix or Plug core?
Examples
Example 1: Securing GitHub webhooks
Example 2: Dropbox Webhooks
Example 3: Stripe Webhook verification
Example 4: Samsung SmartThings (IoT Cloud) HTTP signature verification:
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ryanwinchester
Possible Solutions?
I’m not sure what would be the best way to go about solving this, but maybe a way to hook Plugs into the
Plug.Parsersmodule (for certain routes, or route pipelines, or …?) where we can use the body before it is parsed?Or maybe we could have another plug before
Plug.ParsersandPlug.Parserscould check%Conn{}for (and use) a temporary “already fetched body attribute” in the struct, if the body has been read already?I don’t know if those ideas are possible or feasible, so I’m going to do some digging around, I guess.
Any other thoughts, ideas, possible solutions, concerns?
Does anybody agree that this is a problem that should be solved in core libraries, or at least without having to override core modules or functionality?
Qqwy
I have not encountered systems requiring this kind of signature verification yet myself, which gives me the (subjective) opinion that it might nog be that general of a problem (right now). But there could definitely be a Plug that will do this.
ryanwinchester
Do you often integrate with other services, and use webhooks?
Although, it would be applicable to any use case where you’d want to verify a request body with a signature, the most common situation where you’d encounter this is in verifying 3rd-party webhook requests. (like the ones from GitHub, Stripe, etc that I mentioned earlier)
outlog
I have implemented this - and it was my first (and only!) “bad” experience in elixir world.
Something you assume is gonna be fast to do - but gets you into quite the rabbit hole - and you end up with code you are not really happy about.
dev happiness rock bottom - and worst case is if people implement these webhooks without verifying, because it’s quite steep to understand what’s going on.
My brain have suppressed the experience/specifics - so can’t talk of potential solutions - but if there is no easy solution(which I think is the case) a PR with documentation for plug and even (especially?) for Phoenix and a best/recommended way of doing it is probably the fastest way forward..
eg titled “Verifying webhooks”, and then it tells the story of why it’s troublesome, and gives a suggested implementation..
well I think a good pattern is /webhooks/provider - so you add multiple Plugs (which of course is a small performance hit) - and you can decode the json and pass it to the controller.. How to read request body multiple times during request handling? - #7 by outlog - but pretty it ain’t.
so totally share your frustration..
ryanwinchester
Yeah, I’m looking through right now and
Plug.Conn.read_body/2is not used until inside each specific parser’sparse/5function, so I think that makes a solution more difficult.I wonder if we used
Plug.Conn.read_body/2inPlug.Parsersand passed the body intoparser.parse/5instead, if that would be okay.Too much breaking change, probably
mbuhot
How about a Plug that replaces the Conn.adapter with one that verifies the body when read?
read_bodycan return an error tuple already, so it shouldn’t break Plug.Parsers.Crowdhailer
We had this problem, and after talking about it at ElixirDaze Four other people approached me with the same. I think it’s common enough that it should be handled by something that is mean to be as general purpose as plug.
@ryanwinchester we copied and pasted the plug parses into our codebase and added a line the put the raw body in
conn.assigns[:raw_body]. Here is the appropriate slide in that ElixirDaze talk about it DazeHowever we don’t use this solution any more. The punchline is we don’t use plug on any service that validates signatures. We have one service on the front end still using phoenix because it serves assets and html and has to sign requests but not validate.
Crowdhailer
There is also this issue raised by someone else i know is doing signing
https://github.com/elixir-plug/plug/issues/691
ryh
Isn’t reading the body done with an adapter?
Maybe a toplevel plug could:
put_private(conn, :raw_body, raw_body)read_bodyuse:raw_bodyinstead.This way when the parsers parse, they will use the adapter’s
read_req_bodyinstead of the one that can only be called once.The custom adapter could otherwise use the original adapter’s implementation for anything else.
ryanwinchester
Where can I “plug” in my own
Plug.Conn.adapterin the framework?