Skip to content

fix: contain appconfig public_/remote_ handler includes to the app directory - #41856

Merged
oc-tmueller merged 3 commits into
masterfrom
fix/appconfig-service-handler-containment-master
Sep 23, 2026
Merged

oc-tmueller merged 3 commits into
masterfrom
fix/appconfig-service-handler-containment-master

Conversation

@oc-tmueller

@oc-tmueller oc-tmueller commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Front-port of #41803, which landed on 10.16 as b46c15d76. master carries the same defect.

The first commit is byte-identical to what merged: all 874 changed code lines match the merged 10.16 squash commit, verified by diffing the two net diffs, where only blob hashes and hunk offsets differ. The full discussion, including three rounds of review, is on #41803; this description summarises the result.

One commit on top of that is not in 10.16 — see "Delta against 10.16" at the end.

An authenticated admin could set the core appconfig key public_webdav (or any public_/remote_ key) to a path-traversal value that public.php then require_once'd on the next GET /public.php/<service>, achieving remote code execution.

1. Sink — the handler include is contained to the app directory

public.php and remote.php resolved the stored <appId>/<path> value by concatenating OC_App::getAppPath($app), which is not a containment check. getAppPath() returns false for an app id with no directory on disk, and false . '/' is '/', so the include path becomes absolute — and an absolute path contains no traversal sequence, which is why remote.php's pre-existing ../ substring test did not close this. isInstalled() does not help either: it only tests for an appconfig enabled row, not for a directory, and an admin can create such a row for any app id.

The stored value now resolves through a new OC_App::getServiceHandlerPath(), which requires the target to realpath() inside the app's own directory (with a trailing separator, so …/apps/files does not accept …/apps/files_sharing) and to be an existing .php file. That also covers a symlink out of the app tree. The is_file() half matters on its own: require_once on a directory named x.php is an E_COMPILE_ERROR the callers' catch (\Throwable) cannot handle. A NUL byte in either argument is refused before either is used as a path.

No legitimate value is affected — every programmatic writer stores <appId>/<path from info.xml>, and a test pins all six shipped handlers.

2. Guards on the way in — non-canonical ids and keys are refused, not enumerated

Enumerating mangled spellings of core cannot be complete: the only requirement for a bypass is that the written app id compare equal to core under the database collation, which is an open set. Blank padding into the varchar(32) appid column, an accent-insensitive collation, and a trailing invalid byte all survive normalisation. So any app id outside [a-zA-Z0-9_.-]{1,32} is refused outright on setValue and deleteApp, in both the controller and the legacy endpoint. The charset is anchored with \z rather than $, because $ also matches before a trailing newline, which would let a 33-byte id truncate onto an existing row.

The same reasoning applies to the key, which was compared case-sensitively: under a case-insensitive collation PUBLIC_webdav folds onto the stored public_webdav row. The prefix test is case-insensitive now, and a non-canonical key is refused outright on core only, so files_sharing keeps its legitimate public_share_* keys.

deleteKey deliberately does not demand a canonical app id — a row written under one before this guard existed (via occ or setAppValue()) would otherwise be undeletable through both admin paths. Such a row may only be removed key by key, and only for a canonical, non-handler key, because isCoreApp() compares a normalised string and cannot see a spelling only the collation folds. A deleted handler row is not self-healing: only an app version bump re-registers it.

3. Reads are refused too

Otherwise the endpoints hand back the handler path they refuse to let an admin set. The legacy endpoint had no read guard at all. Its guard checks the key in every source the dispatch might read it from rather than the first one it finds — the write actions take $_POST['key'] while getValue/hasKey take $_GET['key'], and both are attacker-controlled independently, so validating one source alone lets a harmless decoy in the other satisfy the guard while the action consumes the real key.

To stop the two endpoints drifting apart, isCoreApp(), isCanonicalAppId(), isCanonicalConfigKey(), isServiceHandlerKey() and isProtectedCoreServiceKey() are OC_App statics that both call, with their own tests.

4. Two pre-existing bugs in the reworked flow

getAppValue() returns '' for an unregistered service rather than null, so the $file === null test never fired and the request fell through to "App not installed" — a logged 500 on public.php and, on remote.php, a RemoteException with getCode() 0, which OC_Response::setStatus() turns into a bogus HTTP/1.1 0 that caches, monitoring and sync clients read as success. Both answer 404 now, and every RemoteException carries an explicit status. Refusals answer 503, because that is what handleException() already produced on a WebDAV request, and a sync client must not be told the root is gone.

Tests

tests/lib/legacy/AppTest.php covers the containment helper (file inside the app, traversal that stays inside, traversal out, an app id with no directory on disk, the sibling-directory boundary, a directory named handler.php, a non-.php target, a missing file, NUL bytes, empty arguments) and all five new statics, and pins the six shipped handler values. tests/Settings/Controller/AppConfigControllerTest.php covers the mangled core spellings, the non-canonical ids and keys, the read guards, the narrowed deleteKey gate, and the allowed near-misses including files_sharing/public_share_*.

core/ajax/appconfig.php cannot be covered by an API test — it calls OCP\JSON::callCheck() → passesCSRFCheck(), which needs a browser session, and there is no harness for core/ajax/*. That is exactly why the logic lives in unit-testable OC_App statics; the wiring was verified by hand.

Verified locally

In owncloudci/php:8.3 on sqlite, matching this branch's CI matrix:

  • tests/lib/legacy/AppTest.php — OK (89 tests, 107 assertions)
  • tests/Settings/Controller/AppConfigControllerTest.php — OK (83 tests, 235 assertions)
  • php-cs-fixer --dry-run over the seven changed PHP files — 0 of 7 need changes
  • calens — exit 0, entry renders as expected

The end-to-end live-instance checks (the three attack values refused; public.php/webdav, public.php/files, remote.php/webdav, remote.php/dav all still serving) were done on the 10.16 change this is identical to — see #41803.

Delta against 10.16

1f37060 is an extra fix found while reviewing this front-port. It is deliberately master-only — see "Why this one is not backported" below.

getValue() checked is_string() on the app id but not on the key, unlike setValue() and deleteKey(), while its comment claimed the three entry points share one predicate. Both core predicates short-circuit on a non-core app, so for any other app an array key fell through to OC\AppConfig as a subscript — an "Illegal offset type" warning on PHP 7, a TypeError on PHP 8, i.e. a logged 500 where the two write paths answer 400.

The route's {key} is not a guarantee: Request::decodeContent() decodes an application/json body and merges it over items['parameters'] for a GET as well — i.e. after setUrlParameters() merged the url params — so a body of {"key":["x"]} overrides the {key} taken from the path. Admin-only and no security consequence (a read cannot fold onto a stored row), but it is the exact failure mode the rest of the diff sets out to close. The new test was confirmed red before the fix.

Why this one is not backported

The 500 is unreachable on 10.16, because the runtime that produces it cannot run 10.16: lib/base.php there exits with "This version of ownCloud is not compatible with PHP 8.0" for PHP_VERSION_ID >= 80000, and composer.json pins platform.php: 7.4. Probed on both images rather than inferred — isset($cache['app'][['k']]) raises an Illegal offset type in isset or empty warning and returns false on owncloudci/php:7.4, but a TypeError on owncloudci/php:8.3. Since OC\AppConfig::getValue() → hasKey() → isset() applies no string cast, 10.16 answers 200 with the default and logs one warning where this branch answers 400.

So on 10.16 the difference is cosmetic, with no security consequence — the guards that protect the handler path each is_string()-check and fail closed on their own. Not worth a backport PR and a second full CI matrix; the cost is a small context difference for any future cherry-pick touching getValue().

Deliberately left for follow-ups

  • Enforcing the charset inside OC\AppConfig::setValue(). That would cover occ and every app, but it changes IConfig::setAppValue() for all writers, so it belongs in a major release.
  • public.php still answers a logged 500 where remote.php now answers 503.
  • getKeys($app) is the one appconfig entry point still without an is_string() guard, so {"app":["core"]} in a JSON body gives the same admin-triggerable 500 as above. Left out deliberately: it is pre-existing, untouched by this diff, and reading key names cannot disclose a handler path, since the path is the value. Note core/ajax/appconfig.php already refuses a non-string app id for every action, so the two endpoints disagree on this one point.
  • getServiceHandlerPath() returns a bare false for six distinct reasons and logs none of them. Two are environmental rather than malicious — a handler file that is a symlink out of the app directory, and an app directory outside open_basedir, where realpath() returns false — and both turn a working service into a generic 500/503 with only "Path not allowed" to go on. A debug-level line naming the reason would make that diagnosable without weakening the check.

oc-tmueller and others added 3 commits September 23, 2026 15:37
…rectory

An authenticated admin could set the core appconfig key `public_webdav`
(or any `public_`/`remote_` key) to a path-traversal value and have it
`require_once`'d by public.php on the next `GET /public.php/webdav`,
achieving remote code execution.

## The sink

public.php and remote.php resolved the stored `<appId>/<path>` value by
concatenating `OC_App::getAppPath($app)`, which is not a containment
check. getAppPath() returns false for an app id that has no directory on
disk, and `false . '/'` is `'/'`, so the include path becomes absolute -
and an absolute path contains no traversal sequence, which is why a `../`
substring test does not close this. `isInstalled()` does not stop it
either, because it only tests for an appconfig `enabled` row, not for a
directory, and an admin can create such a row for any app id. Verified on
a live instance: a stored value of `<dirless app>//etc/passwd` returned
the contents of /etc/passwd.

The stored value now resolves through `OC_App::getServiceHandlerPath()`,
which requires the target to realpath() inside the app's own directory
(with a trailing separator, so `…/apps/files` does not accept
`…/apps/files_sharing`) and to be an existing `.php` file. That also
covers a symlink out of the app tree, and the `is_file()` test matters
because `require_once` on a directory named `x.php` is an
E_COMPILE_ERROR the callers' `catch (\Throwable)` cannot handle. A NUL
byte in either argument is refused before either is used as a path:
realpath(), and is_dir() inside getAppPath(), warn on PHP 7 and throw on
PHP 8 rather than returning the documented false, and cleanAppId() does
not help because its '\0' is a literal backslash-zero.

remote.php builds its include path the same way in the default branch of
its service switch, so it gets the same treatment. Its `core` branch is
the one include site left without containment and is unreachable by
contradiction - getting there needs a stored `remote_<service>` value
starting `core/`, which means `$service` was not hardcoded, which the
`in_array()` above then rejects - so that reasoning is recorded next to
it rather than the branch being changed. Every programmatic writer stores
`<appId>/<path from info.xml>`, which always resolves inside the app
directory, and a test pins all six shipped handlers.

## The guards on the way in

Enumerating mangled spellings of "core" cannot be complete: the only
requirement for a bypass is that the written app id compares equal to
"core" under the database collation, which is an open set. Blank padding
into the varchar(32) appid column, an accent-insensitive collation and a
trailing invalid byte all survive normalization. Any app id outside the
canonical charset is therefore refused outright on setValue and
deleteApp, in the controller and in the legacy endpoint. The charset is
anchored with `\z` rather than `$`, because `$` also matches before a
trailing newline, which would let a 33-byte id truncate onto an existing
row. The legacy endpoint additionally requires cleanAppId() to be a
no-op, so that it agrees with the controller, which validates and writes
the raw value: the charset permits `.`, so `co..re` would otherwise pass
validation and be written as `core`, and `..` as the empty app id.

deleteKey deliberately does not demand a canonical app id outright, or a
row written under one before this guard existed - through this endpoint,
occ, or an app calling setAppValue() - would be undeletable through both
admin paths. Such a row may only be removed key by key and only for a
canonical, non-handler key, because isCoreApp() compares a normalized
string and cannot see a spelling that only the collation folds onto
"core": on utf8mb4_general_ci or utf8mb4_0900_ai_ci - the MySQL default
whenever the database is pre-created rather than created by ownCloud -
deleting "córe"/"public_webdav" would otherwise remove core's registered
handler, which is only re-registered on an app version bump. Blank
padding plus varchar(32) truncation gets there with no exotic collation
at all. deleteApp keeps the blanket check, since dropping the whole core
appconfig is not recoverable.

The same reasoning applies to the key, which was compared case
sensitively: under a case-insensitive collation `PUBLIC_webdav` folds
onto the stored `public_webdav` row, so an admin could overwrite or
delete a registered handler. The prefix test is case-insensitive now, and
a key that is not canonical is refused outright on core, because an
accent-insensitive collation folds `públic_webdav` onto it as well. Only
the core rows are read as handlers, so both key checks are gated on the
app: files_sharing keeps its legitimate `public_share_*` keys.

Reads are refused too, or the endpoints would hand back the handler path
they refuse to let an admin set. The legacy endpoint had no read guard at
all; the controller refused the exact lowercase prefix but not a mangled
spelling of it, and was missing the key charset check. The endpoint's
guard checks the key in every source the dispatch might read it from
rather than the first one it finds - the write actions take
`$_POST['key']` while getValue/hasKey take `$_GET['key']`, and both are
attacker-controlled independently, so validating one source alone lets a
harmless decoy in the other satisfy the guard while the action consumes
the real key. (A read cannot fold onto a stored row: OC\AppConfig's
getValue()/hasKey() are exact lookups in the preloaded cache, so the
collation never sees the key. It is the prefix match that mattered.)

`app[]=core` stringified to "Array", passed the charset test and landed a
row under the app id `Array`, because the endpoint cast before
validating. A non-string app id is refused now, and so is a non-string
key on a write: it reaches OC\AppConfig as an array subscript, an
"Illegal offset type" warning on PHP 7 and a TypeError on PHP 8.

To stop the two endpoints drifting apart - which is what let the legacy
one ship without the deleteApp guard in an earlier revision - isCoreApp(),
isCanonicalAppId(), isCanonicalConfigKey(), isServiceHandlerKey() and
isProtectedCoreServiceKey() are OC_App statics that both call, with their
own tests. All five compare `preg_match()` against the failing value, so
a PCRE error refuses the request rather than passing it.

## Two pre-existing bugs in the reworked flow

getAppValue() returns '' for an unregistered service rather than null, so
the `$file === null` test never fired and the request fell through to
"App not installed: " - a logged 500 on public.php and, on remote.php, a
RemoteException with getCode() 0, which OC_Response::setStatus() turns
into a bogus `HTTP/1.1 0` that caches, monitoring and sync clients read
as a success. Both answer 404 now, and every RemoteException carries an
explicit status. The refusals get 503 rather than 404, because that is
what handleException() already answered on a WebDAV request (code 0
matches no case in its switch and falls through to ServiceUnavailable),
and a sync client must not be told the root is gone.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The entry was written before the pull request number was known.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
getValue() checked is_string() on the app id but not on the key, unlike
setValue() and deleteKey(), while its comment claimed the three entry
points share one predicate. Both core predicates short-circuit on a
non-core app, so for any other app an array key fell through to
OC\AppConfig as a subscript: an "Illegal offset type" warning on PHP 7
and a TypeError on PHP 8, i.e. a logged 500 where the two write paths
answer 400.

The route parameter is not a guarantee. Request::decodeContent() decodes
an application/json body and merges it over items["parameters"] for a GET
as well, i.e. after setUrlParameters() merged the url params, so a body
of {"key":["x"]} overrides the {key} taken from the path.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@oc-tmueller
oc-tmueller marked this pull request as ready for review September 23, 2026 16:03
@oc-tmueller
oc-tmueller requested a review from a team as a code owner September 23, 2026 16:03
@oc-tmueller
oc-tmueller merged commit 17a9ce2 into master Sep 23, 2026
33 checks passed
@oc-tmueller
oc-tmueller deleted the fix/appconfig-service-handler-containment-master branch September 23, 2026 16:49
oc-tmueller added a commit that referenced this pull request Sep 25, 2026
Bump version.php to 11.0.1 and materialize the changelog fragments for
the first patch release of the 11.0 line.

$OC_Version becomes [11, 0, 1, 0]: the 4th digit is the internal
DB-upgrade patch level, not the public patch number, and nothing in this
release needs it moved.

The 15 fragments in changelog/unreleased/ move into
changelog/11.0.1_2026-09-25/ (git mv, so the renames stay tracked) and
CHANGELOG.md is regenerated with calens. unreleased/ keeps its .gitkeep
and is now empty, ready for the next cycle.

Security:

- #41827 reject SVG/script content before it reaches ImageMagick bitmap
  previews
- #41834 pin the Imagick coder for each preview provider
- #41856 prevent path traversal via appconfig public_/remote_ keys

Bugfixes:

- #41676 reduce priority of checkPropFind event
- #41779 do not echo secrets when setting config values via occ
- #41782 restore index usage for filecache writes on Oracle
- #41807 show federated users in the share dialog when local users also
  match
- #41808 avoid a deprecation notice when hashing the file cache path on
  Oracle
- #41824 ship only the app payload in the release tarballs
- #41835 release the file handle when a bitmap preview cannot be decoded
- #41855 show a media type icon when a preview file cannot be opened
- #41869 restrict federated address book sync to the trusted server

Changes:

- #41775 update PHP dependencies
- #41785 require rhukster/dom-sanitizer as a tagged release
- #41808 restore Oracle database support in the command line installer

The #41824 fragment is carried over from #41825, which prepared this
release on a release/v11.0.1 branch that the org maintenance ruleset
freezes after its first push; that branch cannot take the changelog
commit, so this supersedes it.

Once merged, v11.0.1 gets tagged on the merged commit and the release
bundles are built and published from owncloud/server-release.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Thomas Müller <[email protected]>
oc-tmueller added a commit that referenced this pull request Sep 25, 2026
* feat: release 11.0.1

Bump version.php to 11.0.1 and materialize the changelog fragments for
the first patch release of the 11.0 line.

$OC_Version becomes [11, 0, 1, 0]: the 4th digit is the internal
DB-upgrade patch level, not the public patch number, and nothing in this
release needs it moved.

The 15 fragments in changelog/unreleased/ move into
changelog/11.0.1_2026-09-25/ (git mv, so the renames stay tracked) and
CHANGELOG.md is regenerated with calens. unreleased/ keeps its .gitkeep
and is now empty, ready for the next cycle.

Security:

- #41827 reject SVG/script content before it reaches ImageMagick bitmap
  previews
- #41834 pin the Imagick coder for each preview provider
- #41856 prevent path traversal via appconfig public_/remote_ keys

Bugfixes:

- #41676 reduce priority of checkPropFind event
- #41779 do not echo secrets when setting config values via occ
- #41782 restore index usage for filecache writes on Oracle
- #41807 show federated users in the share dialog when local users also
  match
- #41808 avoid a deprecation notice when hashing the file cache path on
  Oracle
- #41824 ship only the app payload in the release tarballs
- #41835 release the file handle when a bitmap preview cannot be decoded
- #41855 show a media type icon when a preview file cannot be opened
- #41869 restrict federated address book sync to the trusted server

Changes:

- #41775 update PHP dependencies
- #41785 require rhukster/dom-sanitizer as a tagged release
- #41808 restore Oracle database support in the command line installer

The #41824 fragment is carried over from #41825, which prepared this
release on a release/v11.0.1 branch that the org maintenance ruleset
freezes after its first push; that branch cannot take the changelog
commit, so this supersedes it.

Once merged, v11.0.1 gets tagged on the merged commit and the release
bundles are built and published from owncloud/server-release.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Thomas Müller <[email protected]>

* fix: name every updated PHP dependency in the 11.0.1 changelog

composer/semver (3.4.4 to 3.5.0) and nikic/php-parser (v5.8.0 to v5.9.0)
both moved since v11.0.0, both are direct entries in composer.json's
require, and neither was named in the fragment. Diffing composer.lock at
v11.0.0 against this branch gives 26 changed production packages; the
fragment listed 24.

Each was missed by the pass that bumped it: #41864 edited this fragment in
the same commit that raised composer/semver, and #41829 did the same for
nikic/php-parser. The list is not direct-only either — it already names
transitive dependencies such as guzzlehttp/psr7 and symfony/mime — so both
are omissions, not a scoping decision. An administrator reconciling the
11.0.1 notes against advisory ranges would read both as unchanged.

The four remaining differences are require-dev only (myclabs/deep-copy,
phpunit/phpunit, sebastian/exporter, sebastian/recursion-context) and stay
out, matching the fragment's existing production-only scope; the tarball
installs --no-dev.

Also restores CHANGELOG.md's trailing newline. `ocrelease changelog` writes
calens' stdout as captured by execa, which strips it; the last six
"chore: update changelog" revisions of the file all end in one, and so does
calens' own output, so the next regeneration would have put it back as a
one-line no-op diff.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Thomas Müller <[email protected]>

---------

Signed-off-by: Thomas Müller <[email protected]>
Co-authored-by: Thomas Müller <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants