[Bug]: TypeError in ThemingDefaults::getColorPrimary() — preg_match() under PHP 8.4 on unset user primary_color
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 apps/theming/lib/ThemingDefaults.php 第 196 行附近开始,这里 getColorPrimary() 读取用户的 primary_color 并将其传给 preg_match()。检查与 ThemingDefaults 相关的测试,以及 getColorBackground() 是否以相同方式处理输入。确认未设置颜色时会回退到全局默认值且不会报错,然后运行相关的 theming 测试。
由索引模型根据 Issue 内容生成。
描述
⚠️ This issue respects the following points: ⚠️
- This is a bug, not a question or a configuration/webserver/proxy issue.
- This issue is not already reported on Github OR Nextcloud Community Forum (I've searched it).
- Nextcloud Server is up to date. See Maintenance and Release Schedule for supported versions.
- I agree to follow Nextcloud's Code of Conduct.
Bug description
When accessing /apps/files/ (or any page initializing user capabilities/theming) under PHP 8.4+, Nextcloud crashes with a fatal TypeError (HTTP 500) if the logged-in user does not have a custom primary_color configured.
In apps/theming/lib/ThemingDefaults.php line 196-197:
$userPrimaryColor = $this->userConfig->getValueString($user->getUID(), Application::APP_ID, 'primary_color');
if (preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $userPrimaryColor)) {
return $userPrimaryColor;
}
Under PHP 8.4+, preg_match enforces strict types and no longer tolerates non-string types or certain uninitialized proxy representations from getValueString(), throwing:
TypeError: preg_match(): Argument #2 ($subject) must be of type string, string given at /var/www/html/apps/theming/lib/ThemingDefaults.php#197
Steps to reproduce
- Run Nextcloud Server 34.x on PHP 8.4 (e.g. official
nextcloud:34.0.5Apache Docker image). - Log in as an admin or normal user who has never set a custom theming primary color.
- Access
/apps/files/. - The page fails with HTTP 500 Internal Server Error.
Expected behavior
Nextcloud should fall back gracefully to the global system primary color ($defaultColor) without throwing a fatal TypeError.
Nextcloud Server version
34
Operating system
Debian/Ubuntu
PHP engine version
PHP 8.4
Web server
Apache (supported)
Database engine version
PostgreSQL
Is this bug present after an update or on a fresh install?
Updated from a MINOR version (ex. 32.0.1 to 32.0.2)
Are you using the Nextcloud Server Encryption module?
Encryption is Disabled
What user-backends are you using?
- Default user-backend (database)
Nextcloud Logs
{
"reqId": "n6FAHxGdzIB0H4OyzO55",
"level": 3,
"time": "2026-11-04T03:10:52+00:00",
"remoteAddr": "x.x.x.x",
"user": "admin",
"app": "index",
"method": "GET",
"url": "/apps/files/",
"message": "preg_match(): Argument #2 ($subject) must be of type string, string given",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/27.2 Safari/605.1.15",
"version": "34.0.5.1",
"exception": {
"Exception": "TypeError",
"Message": "preg_match(): Argument #2 ($subject) must be of type string, string given",
"Code": 0,
"Trace": [
{
"function": "preg_match",
"file": "/var/www/html/apps/theming/lib/ThemingDefaults.php",
"line": 197,
"args": ["/^\\#([0-9a-f]{3}|[0-9a-f]{6})$/i", ""]
},
{
"file": "/var/www/html/apps/theming/lib/Capabilities.php",
"line": 88,
"function": "getColorPrimary",
"class": "OCA\\Theming\\ThemingDefaults",
"type": "->"
},
{
"file": "/var/www/html/lib/private/CapabilitiesManager.php",
"line": 60,
"function": "getCapabilities",
"class": "OCA\\Theming\\Capabilities",
"type": "->"
},
{
"file": "/var/www/html/lib/private/Template/JSConfigHelper.php",
"line": 141,
"function": "getCapabilities",
"class": "OC\\CapabilitiesManager",
"type": "->"
},
{
"file": "/var/www/html/lib/private/TemplateLayout.php",
"line": 251,
"function": "getConfig",
"class": "OC\\Template\\JSConfigHelper",
"type": "->"
},
{
"file": "/var/www/html/apps/files/lib/Controller/ViewController.php",
"line": 202,
"function": "dispatchTyped",
"class": "OC\\EventDispatcher\\EventDispatcher",
"type": "->"
}
],
"File": "/var/www/html/apps/theming/lib/ThemingDefaults.php",
"Line": 197
}
}
Suggested Fix
Add defensive type checks and explicit casting in apps/theming/lib/ThemingDefaults.php:
--- a/apps/theming/lib/ThemingDefaults.php
+++ b/apps/theming/lib/ThemingDefaults.php
@@ -194,7 +194,7 @@ class ThemingDefaults extends \OC_Defaults {
// user-defined primary color
if (!empty($user)) {
$userPrimaryColor = $this->userConfig->getValueString($user->getUID(), Application::APP_ID, 'primary_color');
- if (preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $userPrimaryColor)) {
+ if (!empty($userPrimaryColor) && is_string($userPrimaryColor) && preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', (string)$userPrimaryColor)) {
return $userPrimaryColor;
}
}
Similarly for getColorBackground() if applicable.
- 主要语言
- PHP
- 星标
- 37.4k
- 派生
- 5.3k
- 平均合并
- 2 天 7 小时
- 30 天内合并 PR
- 744
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
nextcloud/server 的其他 Issue
-
[Bug]: DAV Forbidden/InvalidPath responses break on "&" in the message (DOMDocument::createElementNS(): unterminated entity reference)可能已有人在做 @th-joerger 于 3 天前认领。 未关闭0. Needs triage 35-feedback bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
[Bug]: Files folder-tree API eagerly lists the children of every subfolder at the last requested depth, then discards them (N extra directory listings per request)可能已有人在做 @lazerg 于 5 天前认领。 未关闭0. Needs triage 34-feedback bug
难度 2/5 1-3 小时 新手友好度 77/100
nextcloud/server#65904 · 1 条评论 ·
维护者通常 1 天内回复
-
[Bug]: Contradictory notifications when cancelling welcome email confirmation可能已有人在做 @Jashan32 于 7 天前认领。 未关闭0. Needs triage 36-feedback bug
难度 2/5 1-3 小时 新手友好度 67/100
维护者通常 1 天内回复
-
`OutOfOfficeEventDispatcherJob` is not cleaned up correctly可能已有人在做 @MishowHD 于 6 天前认领。 未关闭1. to develop bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
-
Author mapping step: "create new user" and "assign to existing user" fields have no spacing between them可能已有人在做 @umeshnevase 今天认领。 未关闭
难度 1/5 1-3 小时 新手友好度 78/100
WordPress/wordpress-importer#275 ·
-
security/openvpn-legacy: read-only user can stop a running VPN可能已有人在做 @besendorf 今天认领。 未关闭incomplete
难度 2/5 1-3 小时 新手友好度 72/100
opnsense/plugins#5887 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
-
jira-created
难度 2/5 1-3 小时 新手友好度 77/100