nextcloud / nextcloud/user_saml

[Bug]: SAML login fails behind reverse proxy when index.php is removed from the public URL

Open
#1,183 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

0. Needs triage bug
Dominant language
PHP
Stars
104
Forks
85
Avg merge
1d 15h
Merged PRs (30d)
16

Description

⚠️ This issue respects the following points: ⚠️
Bug description

SAML authentication fails when Nextcloud is configured behind a reverse proxy that removes index.php from the public URL.
I have used

sudo nextcloud.occ config:system:set overwrite.cli.url --value="https://mydomain.com"

sudo nextcloud.occ config:system:set htaccess.RewriteBase --value="/"

The SAML response received from the IdP contains the correct Destination:
https://mydomain.com/apps/user_saml/saml/acs

However, onelogin/php-saml reconstructs the current URL from the internal PHP request and generates an incorrect URL:
https://mydomain.com/apps/user_saml/saml/index.phpacs

As a result, SAML validation fails with:

The response was received at
https://mydomain.com/apps/user_saml/saml/index.phpacs
instead of
https://mydomain.com/apps/user_saml/saml/acs

Please note the extra index.php in the first URL.

As a temporary workaround I have modified
/var/snap/nextcloud/ current /nextcloud/extra-apps/user_saml/lib/Controller/SAMLController.php

and replaced:

                $this->callWithXmlEntityLoader(function () use ($auth, $AuthNRequestID): void {
                        $auth->processResponse($AuthNRequestID);
                });

with

$oldRequestUri = $_SERVER['REQUEST_URI'] ?? null;

$_SERVER['REQUEST_URI'] = parse_url(
    $this->urlGenerator->linkToRouteAbsolute(
        'user_saml.SAML.assertionConsumerService'
    ),
    PHP_URL_PATH
);

try {
    $this->callWithXmlEntityLoader(function () use ($auth, $AuthNRequestID): void {
        $auth->processResponse($AuthNRequestID);
    });
} finally {
    if ($oldRequestUri !== null) {
        $_SERVER['REQUEST_URI'] = $oldRequestUri;
    }
}
Steps to reproduce
  1. Enable SAML

  2. Execute

sudo nextcloud.occ config:system:set overwrite.cli.url --value="https://mydomain.com"
sudo nextcloud.occ config:system:set htaccess.RewriteBase --value="/"

  1. Set up nginx rules to proxy_pass the missing public url index.php
location / {
    try_files $uri @nextcloud_index;

    proxy_pass http://127.0.0.1:9988;

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;

    proxy_redirect off;
}

location @nextcloud_index {
    rewrite ^/(.*)$ /index.php/$1 break;

    proxy_pass http://127.0.0.1:9988;

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;

    proxy_redirect off;
}
  1. SAML login fails with the referred error
Expected behavior

Normal SAML login

Nextcloud Server version

34

Operating system

Debian/Ubuntu

PHP engine version

No response

Web server

No response

Database engine version

No response

Is this bug present after an update or on a fresh install?

No response

Are you using the Nextcloud Server Encryption module?

No response

What user-backends are you using?
  • Default user-backend (database)
  • LDAP/ Active Directory
  • SSO - SAML
  • Other
Configuration report

List of activated Apps

Nextcloud Signing status

Nextcloud Logs

Additional info

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in user_saml/lib/Controller/SAMLController.php at the assertionConsumerService flow and inspect how processResponse receives the request URL. Reproduce the reverse-proxy setup with index.php removed, then verify that SAML validation uses the public /apps/user_saml/saml/acs URL matching the IdP Destination without breaking request restoration.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
authentication
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
57/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.