nextcloud / server

☁️ Nextcloud server, a safe home for all your data
https://nextcloud.com
GNU Affero General Public License v3.0
26.93k stars 4.02k forks source link

TypeError null returned if list of trusted proxies is not empty but there is no element at position one #18848

Closed eibex closed 4 years ago

eibex commented 4 years ago

Steps to reproduce

  1. Set trusted_domains in config.php, but do not put anything at position 0 in the array. (this usually happens if the IP is set at position 0, then a domain is added, and then the IP removed - even if done via occ)

Expected behaviour

The next useful trusted proxy is chosen.

Actual behaviour

NC looks for position 0 only and throws errors every few hours:

[PHP] Error: TypeError: Return value of OC\AppFramework\Http\Request::getServerHost() must be of the type string, null returned at /var/www/nextcloud/lib/private/AppFramework/Http/Request.php#903
[PHP] Error: Undefined offset: 0 at /var/www/nextcloud/lib/private/AppFramework/Http/Request.php#903
[index] Error: TypeError: Return value of OC\AppFramework\Http\Request::getServerHost() must be of the type string, null returned at <<closure>>

 0. /var/www/nextcloud/lib/private/URLGenerator.php line 266
    OC\AppFramework\Http\Request->getServerHost()
 1. /var/www/nextcloud/lib/private/Template/JSCombiner.php line 72
    OC\URLGenerator->getBaseUrl()
 2. /var/www/nextcloud/lib/private/Server.php line 986
    OC\Template\JSCombiner->__construct(OC\Files\AppData\AppData {}, OC\URLGenerator {}, OC\Memcache\Factory {}, OC\SystemConfig {}, OC\Log {})
 3. /var/www/nextcloud/3rdparty/pimple/pimple/src/Pimple/Container.php line 118
    OC\Server->OC\{closure}("*** sensitive parameters replaced ***")
 4. /var/www/nextcloud/lib/private/ServerContainer.php line 120
    Pimple\Container->offsetGet("OC\\Template\\JSCombiner")
 5. /var/www/nextcloud/lib/private/TemplateLayout.php line 339
    OC\ServerContainer->query("OC\\Template\\JSCombiner")
 6. /var/www/nextcloud/lib/private/TemplateLayout.php line 169
    OC\TemplateLayout::findJavascriptFiles(["core/js/dist/m ... "])
 7. /var/www/nextcloud/lib/private/legacy/template.php line 183
    OC\TemplateLayout->__construct("guest", "core")
 8. /var/www/nextcloud/lib/private/Template/Base.php line 131
    OC_Template->fetchPage()
 9. /var/www/nextcloud/lib/base.php line 782
    OC\Template\Base->printPage()
10. /var/www/nextcloud/lib/base.php line 1078
    OC::init()
11. /var/www/nextcloud/index.php line 40
    undefinedundefinedrequire_once("/var/www/nextcloud/lib/base.php")
[PHP] Error: Undefined offset: 0 at /var/www/nextcloud/lib/private/AppFramework/Http/Request.php#903

I am not completely sure the issue was scanning app data, but the error started coming up 1 hour after I ran the command, nothing else was done to the instance.

Nextcloud still runs fine even though these errors are being logged. I cannot understand what they are relating to though.

Server configuration

Operating system: Ubuntu LTS 18.04

Web server: Apache 2.4.41

Database: PostgreSQL 10.10

PHP version: 7.2

Nextcloud version: (see Nextcloud admin page) 17.0.2

Updated from an older Nextcloud/ownCloud or fresh install: Went through all the stable channel updates since NC 16

Where did you install Nextcloud from: GitHub months ago, ran flawlessly until these errors came up. Always updated via web updater.

Signing status:

Signing status ``` No errors have been found. ```

List of activated apps:

App list ``` - accessibility: 1.3.0 - activity: 2.10.1 - bruteforcesettings: 1.4.0 - calendar: 1.7.2 - cloud_federation_api: 1.0.0 - comments: 1.7.0 - contacts: 3.1.6 - dav: 1.13.0 - federatedfilesharing: 1.7.0 - federation: 1.7.0 - files: 1.12.0 - files_pdfviewer: 1.6.0 - files_rightclick: 0.15.1 - files_sharing: 1.9.0 - files_trashbin: 1.7.0 - files_versions: 1.10.0 - files_videoplayer: 1.6.0 - firstrunwizard: 2.6.0 - gallery: 18.4.0 - logreader: 2.2.0 - lookup_server_connector: 1.5.0 - maps: 0.1.2 - nextcloud_announcements: 1.6.0 - notifications: 2.5.0 - oauth2: 1.5.0 - password_policy: 1.7.0 - previewgenerator: 2.2.0 - privacy: 1.1.0 - provisioning_api: 1.7.0 - recommendations: 0.5.0 - richdocuments: 3.5.1 - serverinfo: 1.7.0 - sharebymail: 1.7.0 - support: 1.0.1 - systemtags: 1.7.0 - tasks: 0.11.3 - text: 1.1.1 - theming: 1.8.0 - twofactor_backupcodes: 1.6.0 - twofactor_totp: 4.1.2 - updatenotification: 1.7.0 - viewer: 1.2.0 - workflowengine: 1.7.0 Disabled: - admin_audit - encryption - files_external - survey_client - user_ldap ```

Nextcloud configuration:

Config report ``` { "system": { "instanceid": "***REMOVED SENSITIVE VALUE***", "passwordsalt": "***REMOVED SENSITIVE VALUE***", "secret": "***REMOVED SENSITIVE VALUE***", "trusted_domains": { "2": "cloud.example.com" }, "datadirectory": "***REMOVED SENSITIVE VALUE***", "dbtype": "pgsql", "version": "17.0.2.1", "overwrite.cli.url": "https:\/\/cloud.example.com\/", "htaccess.RewriteBase": "\/", "dbname": "***REMOVED SENSITIVE VALUE***", "dbhost": "***REMOVED SENSITIVE VALUE***", "dbport": "", "dbtableprefix": "oc_", "dbuser": "***REMOVED SENSITIVE VALUE***", "dbpassword": "***REMOVED SENSITIVE VALUE***", "installed": true, "memcache.distributed": "\\OC\\Memcache\\Redis", "memcache.local": "\\OC\\Memcache\\Redis", "redis": { "host": "***REMOVED SENSITIVE VALUE***", "port": 6379, "timeout": 3 }, "mail_smtpmode": "smtp", "mail_smtpauth": 1, "mail_sendmailmode": "smtp", "mail_smtphost": "***REMOVED SENSITIVE VALUE***", "mail_smtpport": "587", "mail_smtpauthtype": "PLAIN", "mail_from_address": "***REMOVED SENSITIVE VALUE***", "mail_domain": "***REMOVED SENSITIVE VALUE***", "mail_smtpsecure": "tls", "mail_smtpname": "***REMOVED SENSITIVE VALUE***", "mail_smtppassword": "***REMOVED SENSITIVE VALUE***", "maintenance": false, "theme": "", "loglevel": 2, "updater.release.channel": "stable", "preview_max_x": "2048", "preview_max_y": "2048", "jpeg_quality": "60", "lost_password_link": "disabled" } } ```

Are you using external storage, if yes which one: No

Are you using encryption: No

Are you using an external user-backend, if yes which one: No

kesselb commented 4 years ago

Yep. I see the issue ;)

https://github.com/nextcloud/server/blob/d393b1612bab004359621500ff5e771d10165a95/lib/private/AppFramework/Http/Request.php#L909-L911

The list of trusted proxies is not empty but there is no element at position one.

Index: lib/private/AppFramework/Http/Request.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- lib/private/AppFramework/Http/Request.php   (revision 4b28da1dd9a6acf4f04b69474f18ce883323caa3)
+++ lib/private/AppFramework/Http/Request.php   (date 1578865440164)
@@ -906,8 +906,8 @@
            return $host;
        } else {
            $trustedList = $this->config->getSystemValue('trusted_domains', []);
-           if(!empty($trustedList)) {
-               return $trustedList[0];
+           if (count($trustedList) > 0) {
+               return reset($trustedList);
            } else {
                return '';
            }

Something like above should fix it. Some tests would be good. Your list of trusted_proxies with config.php should look like below.

The good news: You can fix that issue without touching the code.

    'trusted_domains' => [
        'cloud.example.com'
    ],