MyNixOS website logo
Description

Secure HTTP proxy for authenticating users via OAuth2

Sproxy is secure by default. No requests makes it to the backend server if they haven't been explicitly whitelisted. Sproxy is independent. Any web application written in any language can use it.

Sproxy2

HTTP proxy for authenticating users via OAuth2.

Motivation

This is overhaul of original Sproxy. See ChangeLog.md for the differences.

Why use a proxy for doing OAuth2? Isn't that up to the application?

  • sproxy is secure by default. No requests make it to the web server if they haven't been explicitly whitelisted.
  • sproxy is independent. Any web application written in any language can use it.

Use cases

  • Existing web applications with concept of roles. For example, Mediawiki, Jenkins, Icinga Web 2. In this case you configure Sproxy to allow unrestricted access to the application for some groups defined by Sproxy. These groups are mapped to the application roles. There is a plugin for Jenkins which can be used for this. Mediawiki and Icinga Web 2 were also successfully deployed in this way, though it required changes to their source code.

  • New web applications designed to work specifically behind Sproxy. In this case you define Sproxy rules to control access to the application's API. It would likely be a single-page application. Examples are MyWatch and Juan de la Cosa.

  • Replace HTTP Basic authentication.

How it works

When an HTTP client makes a request, Sproxy checks for a session cookie. If it doesn't exist (or it's invalid, expired), it responses with HTTP status 511 with the page, where the user can choose an OAuth2 provider to authenticate with. Finally, we store the the email address in a session cookie: signed with a hash to prevent tampering, set for HTTP only (to prevent malicious JavaScript from reading it), and set it for secure (since we don't want it traveling over plaintext HTTP connections).

From that point on, when sproxy detects a valid session cookie it extracts the email, checks it against the access rules, and relays the request to the back-end server (if allowed).

Permissions system

Permissions are stored in internal SQLite3 database and imported from data sources, which can be a PostgreSQL database or a file. See sproxy.sql and datafile.example.yml for details.

Do note that Sproxy2 fetches only group_member, group_privilege and privilege_rule tables, because only these tables are used for authorization. The other tables in PostgreSQL schema serve for data integrity. Data integrity of the data file is not verfied, though import may fail due to primary key restrictions.

Only one data source can be used. The data in internal database, if any, is fully overwritten by the data from a data source. If no data source is specified, the data in internal database remains unchanged, even between restarts. Broken data source is not fatal. Sproxy will keep using existing internal database, or create a new empty one if missed. Broken data source means inability to connect to PostgreSQL database, missed datafile, etc.

The data from a PostgreSQL database are periodically fetched into the internal database, while the data file is read once at startup.

Here are the main concepts:

  • A group is identified by a name. Every group has
    • members (identified by email address, through group_member) and
    • associated privileges (through group_privilege).
  • A privilege is identified by a name and a domain. It has associated rules (through privilege_rule) that define what the privilege gives access to.
  • A rule is a combination of sql patterns for a domain, a path and an HTTP method. A rule matches an HTTP request, if all of these components match the respective attributes of the request. However of all the matching rules only the rule with the longest path pattern will be used to determine whether a user is allowed to perform a request. This is often a bit surprising, please see the following example:

Privileges example

Consider this group_privilege and privilege_rule relations:

groupprivilegedomain
readersbasicwiki.example.com
readersreadwiki.example.com
editorsbasicwiki.example.com
editorsreadwiki.example.com
editorseditwiki.example.com
administratorsbasicwiki.example.com
administratorsreadwiki.example.com
administratorseditwiki.example.com
administratorsadminwiki.example.com
privilegedomainpathmethod
basicwiki.example.com/%GET
readwiki.example.com/wiki/%GET
editwiki.example.com/wiki/edit/%GET
editwiki.example.com/wiki/edit/%POST
adminwiki.example.com/admin/%GET
adminwiki.example.com/admin/%POST
adminwiki.example.com/admin/%DELETE

With this setup, everybody (that is readers, editors and administratorss) will have access to e.g. /imgs/logo.png and /favicon.ico, but only administrators will have access to /admin/index.php, because the longest matching path pattern is /admin/% and only administrators have the admin privilege.

Likewise readers have no access to e.g. /wiki/edit/delete_everything.php.

Keep in mind that:

  • Domains are converted into lower case (coming from a data source or HTTP requests).
  • Emails are converted into lower case (coming from a data source or OAuth2 providers).
  • Groups are case-sensitive and treated as is.
  • HTTP methods are case-sensitive.
  • HTTP query parameters are ignored when matching a request against the rules.
  • Privileges are case-sensitive and treated as is.
  • SQL wildcards (_ and %) are supported for emails, paths (this will change in future versions).

Checking access in a bunch

There is an API end-point for checking access rights in a single POST query: /.sproxy/access. Users should be authenticated to use this end-point, otherwise the respond will be HTTP 511.

The request body shall be a JSON object like this:

{
  "tag1": {"path": "/foo", "method": "GET"},
  "tag2": {"path": "/bar", "method": "GET"}
}

And the respond will contain a JSON array with tag matching path and method pairs allowed to the user. For example:

$ curl -d '{"foo": {"path":"/get", "method":"GET"}, "bar": {"path":"/post", "method":"POST"}}' -XPOST -k 'https://example.ru:8443/.sproxy/access' ...
["foo","bar"]

$ curl -d '{"foo": {"path":"/get", "method":"POST"}, "bar": {"path":"/post", "method":"POST"}}' -XPOST -k 'https://example.ru:8443/.sproxy/access' ...
["bar"]

$ curl -d '{"foo": {"path":"/", "method":"POST"}, "bar": {"path":"/post", "method":"GET"}}' -XPOST -k 'https://example.ru:8443/.sproxy/access' ...
[]

Logout

Hitting the endpoint /.sproxy/logout will invalidate the session cookie. The user will be redirected to / after logout.

Robots

Since all sproxied resources are private, it doesn't make sense for web crawlers to try to index them. In fact, crawlers will index only the login page. To prevent this, sproxy returns the following for /robots.txt:

User-agent: *
Disallow: /

HTTP headers passed to the back-end server

All Sproxy headers are UTF8-encoded.

headervalue
From:visitor's email address, lower case
X-Groups:all groups that granted access to this resource, separated by commas (see the note below)
X-Given-Name:the visitor's given (first) name
X-Family-Name:the visitor's family (last) name
X-Forwarded-Proto:the visitor's protocol of an HTTP request, always https
X-Forwarded-Forthe visitor's IP address (added to the end of the list if header is already present in client request)

X-Groups denotes an intersection of the groups the visitor belongs to and the groups that granted access:

Visitor's groupsGranted groupsX-Groups
allall, devopsall
all, devopsallall
all, devopsall, devopsall,devops
all, devopsdevopsdevops
devopsall, devopsdevops
devopsallAccess denied

Requirements

Sproxy2 is written in Haskell with GHC. All required Haskell libraries are listed in sproxy2.cabal. Use cabal-install to fetch and build all pre-requisites automatically.

Configuration

By default sproxy2 will read its configuration from sproxy.yml. There is example file with documentation sproxy.example.yml. You can specify a custom path with:

sproxy2 --config /path/to/sproxy.yml
Metadata

Version

1.97.1

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows