ryanwinchester

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:

https://smartthings.developer.samsung.com/develop/guides/smartapps/webhook-apps.html#HTTP-signature-verification

Showing Posts 1 to 10

ryanwinchester

ryanwinchester OP

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.Parsers module (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.Parsers and Plug.Parsers could 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

Qqwy

TypeCheck Core Team

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

ryanwinchester OP

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

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

ryanwinchester OP

Yeah, I’m looking through right now and Plug.Conn.read_body/2 is not used until inside each specific parser’s parse/5 function, so I think that makes a solution more difficult.

I wonder if we used Plug.Conn.read_body/2 in Plug.Parsers and passed the body into parser.parse/5 instead, if that would be okay. :sweat_smile:

Too much breaking change, probably :frowning:

mbuhot

mbuhot

How about a Plug that replaces the Conn.adapter with one that verifies the body when read? read_body can return an error tuple already, so it shouldn’t break Plug.Parsers.

Crowdhailer

Crowdhailer

Creator of Raxx

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 Daze

However 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

Crowdhailer

Creator of Raxx

There is also this issue raised by someone else i know is doing signing
https://github.com/elixir-plug/plug/issues/691

ryh

ryh

Isn’t reading the body done with an adapter?

Maybe a toplevel plug could:

  1. Read the body and then put_private(conn, :raw_body, raw_body)
  2. Put a custom adapter that makes read_body use:raw_body instead.

This way when the parsers parse, they will use the adapter’s read_req_body instead of the one that can only be called once.

The custom adapter could otherwise use the original adapter’s implementation for anything else.

ryanwinchester

ryanwinchester OP

Where can I “plug” in my own Plug.Conn.adapter in the framework?

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews