Skip to content

proxy: credentials_by_user_agent matches the user agent the wrong way round #4091

Open

Description

@asaah262

The proxy setting auth_middleware.credentials_by_user_agent is meant to limit the WWW-Authenticate challenges of a 401 to one method for clients whose user agent contains a configured key, e.g. mirall: basic for the desktop client. The proxy compares the other way round, strings.Contains(key, userAgent), so the lock-in only applies when the user agent is part of the key:

  • A real desktop client (Mozilla/5.0 (…) mirall/<version> (…)) never matches mirall, and still gets all challenges.
  • A request without a user agent, or with a short one such as mir, matches every key, and gets only the configured method. With several keys, the method is picked at random (map order) and changes from request to request.

The default (empty map) isn't affected. The setting only changes the advertised challenges, not which credentials are accepted.

Steps to reproduce

Reproduced on main (d43947a209), otherwise with the default config.

  1. In proxy.yaml:
    auth_middleware:
      credentials_by_user_agent:
        mirall: basic
  2. curl -si https://<host>/remote.php/dav/files/admin -A 'Mozilla/5.0 (Macintosh) mirall/3.0.0 (OpenCloud)' | grep -i www-authenticate
  3. curl -si https://<host>/remote.php/dav/files/admin -H 'User-Agent:' | grep -i www-authenticate

Expected

  1. only Basic; 3. Bearer and Basic.

Actual

  1. Bearer and Basic; 3. only Basic.

Where

services/proxy/pkg/middleware/authentication.go, in Authentication (the loop over options.CredentialsByUserAgent) and in evalRequestURI (for /ocs/v[12].php/cloud/). Neither has a test.

Fix: match with strings.Contains(r.UserAgent(), key) and skip an empty user agent, as the frontend's reva auth middleware already does (getCredsForUserAgent); share one helper between both places, pick the longest matching key so that the result doesn't depend on map order, and add tests.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions