Skip to content

[Feature] read cookie values in middleware #146

Description

@witsch

Hi! First off, thanks for Barbacane — we've been running it in production for a (still short) while now as a validating gateway in front of a PostgREST API, and it has been a pleasure to work with. Compiling the OpenAPI spec into the artefact and having the contract be the routing table turned out to be exactly the model we wanted.

As for this feature request, I've only seen your template after writing it down, but I'm happy to reorganize it if that helps.

The gap

Middleware currently has no way to read a single cookie. As far as we can tell:

  • request-transformer interpolates $header.<name>, $query.<name>, $path.<name>, $client_ip and context:<key>, but nothing for cookies — and $header.cookie gives the whole header, with no way to pick one value out of it.
  • cel can match on request.headers.cookie, but on_match.set_context only writes literal strings, so the matched value cannot be carried anywhere.

So anything of the shape "take a value out of Cookie and put it somewhere else" is currently out of reach.

Our use case, generically

Browser clients authenticate with an SSO cookie; the API behind the gateway expects a bearer token. Something has to translate:

Cookie: sso_token=<jwt>   →   Authorization: Bearer <jwt>

with the incoming Authorization header taking precedence when it is already set. That is a handful of lines in Nginx, and it is the single reason we still run an Nginx sidecar behind Barbacane rather than letting the gateway talk to the backend directly. We suspect cookie-to-header translation is common wherever a browser-facing API sits behind a gateway — session cookies, CSRF tokens, tenant hints.

Possible shapes

Either would solve it for us; the first looks smaller:

  1. A $cookie.<name> variable in request-transformer, alongside the existing ones:

    - name: request-transformer
      config:
        headers:
          set:                                  # `set` = only when absent
            Authorization: "Bearer $cookie.sso_token"

    set already gives us the precedence rule for free, and an unresolvable variable already yields an empty string.

  2. A small cookie-to-header middleware, if you would rather keep cookie parsing out of the transformer — mapping named cookies onto headers, with a flag for whether to overwrite.

A cookie map on the CEL request context would work too, though it only helps if set_context can pass a captured value along.

Happy to test a build against our setup if that is useful, and thanks either way! 😊

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions