From 6d60747a35017ff51d01907d525cb9c05dd6c172 Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 10 Nov 2026 18:08:19 +0100 Subject: [PATCH] fix(workspace): replace XML-invalid characters in rich workspace content The rich-workspace DAV properties embed the folder's Readme.md content in the multistatus response as is. Control characters, U+FFFE/U+FFFF or invalid UTF-8 (e.g. a Readme.md left behind encrypted after disabling server-side encryption) make the whole response malformed, so strict clients like the iOS app fail to show the folder listing. NUL bytes also silently truncate the property value. Decode the content to UTF-8 the same way the editor does and replace characters that cannot be represented in XML 1.0 with U+FFFD. Fixes: #9511 Signed-off-by: Jonas Assisted-by: ClaudeCode:claude-opus-5-5 --- lib/DAV/WorkspacePlugin.php | 4 +++- lib/Service/EncodingService.php | 20 ++++++++++++++++++++ tests/unit/DAV/WorkspacePluginTest.php | 25 ++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/DAV/WorkspacePlugin.php b/lib/DAV/WorkspacePlugin.php index 313e5184d76..b1d3ce1000a 100644 --- a/lib/DAV/WorkspacePlugin.php +++ b/lib/DAV/WorkspacePlugin.php @@ -12,6 +12,7 @@ use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Files\FilesHome; use OCA\Text\Service\ConfigService; +use OCA\Text\Service\EncodingService; use OCA\Text\Service\FileService; use OCA\Text\Service\WorkspaceService; use OCP\Files\File; @@ -38,6 +39,7 @@ public function __construct( private readonly FileService $fileService, private readonly ICacheFactory $cacheFactory, private readonly ConfigService $configService, + private readonly EncodingService $encodingService, private readonly LoggerInterface $logger, private readonly ?string $userId, ) { @@ -112,7 +114,7 @@ public function propFind(PropFind $propFind, INode $node) { } try { - $cachedContent = $file->getContent(); + $cachedContent = $this->encodingService->encodeToXmlSafeUtf8($file->getContent()); $cache->set($cacheKey, $cachedContent, 3600); } catch (GenericFileException|NotPermittedException|LockedException $e) { // Ignore but log when debugging diff --git a/lib/Service/EncodingService.php b/lib/Service/EncodingService.php index 1ed1cea5494..a4248f95a04 100644 --- a/lib/Service/EncodingService.php +++ b/lib/Service/EncodingService.php @@ -20,6 +20,14 @@ class EncodingService { 'UTF-8' => "\xef\xbb\xbf" ]; + /** + * Matches characters outside the XML 1.0 Char production, which cannot be + * part of an XML document, not even escaped. + * + * @see https://www.w3.org/TR/xml/#charsets + */ + private const string XML_INVALID_CHARS = '/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]/u'; + public function encodeToUtf8(string $string): ?string { $encoding = $this->detectEncoding($string); if ($encoding === null) { @@ -30,6 +38,18 @@ public function encodeToUtf8(string $string): ?string { return is_string($encoded) ? $encoded : null; } + /** + * Encode to UTF-8 and replace characters that are not allowed in XML with U+FFFD + */ + public function encodeToXmlSafeUtf8(string $string): ?string { + $encoded = $this->encodeToUtf8($string); + if ($encoded === null) { + return null; + } + + return preg_replace(self::XML_INVALID_CHARS, "\u{FFFD}", $encoded); + } + public function detectEncoding(string $string): ?string { $bomDetect = $this->detectUtfBom($string); if ($bomDetect !== null) { diff --git a/tests/unit/DAV/WorkspacePluginTest.php b/tests/unit/DAV/WorkspacePluginTest.php index 06f293d6c6f..ace7bcc512a 100644 --- a/tests/unit/DAV/WorkspacePluginTest.php +++ b/tests/unit/DAV/WorkspacePluginTest.php @@ -12,6 +12,7 @@ use OCA\DAV\Connector\Sabre\Directory; use OCA\Text\DAV\WorkspacePlugin; use OCA\Text\Service\ConfigService; +use OCA\Text\Service\EncodingService; use OCA\Text\Service\FileService; use OCA\Text\Service\WorkspaceService; use OCP\Files\File; @@ -28,12 +29,13 @@ class WorkspacePluginTest extends TestCase { private FileService&MockObject $fileService; private ICache&MockObject $cache; private WorkspacePlugin $plugin; + private string $content = '## Readme'; protected function setUp(): void { $file = $this->createMock(File::class); $file->method('getId')->willReturn(42); $file->method('getEtag')->willReturn('etag'); - $file->method('getContent')->willReturn('## Readme'); + $file->method('getContent')->willReturnCallback(fn () => $this->content); $workspaceService = $this->createMock(WorkspaceService::class); $workspaceService->method('getFile')->willReturn($file); @@ -53,6 +55,7 @@ protected function setUp(): void { $this->fileService, $cacheFactory, $configService, + new EncodingService(), $this->createMock(LoggerInterface::class), 'user', ); @@ -80,6 +83,26 @@ public function testWorkspaceContentHidden(): void { $this->assertSame(42, $propFind->get(WorkspacePlugin::WORKSPACE_FILE_PROPERTY)); } + public static function dataXmlInvalidContent(): array { + return [ + 'control characters' => ["a\x00b\x10c\x16d\x1ce\x0Bf", "a\u{FFFD}b\u{FFFD}c\u{FFFD}d\u{FFFD}e\u{FFFD}f"], + 'noncharacters' => ["a\u{FFFE}b\u{FFFF}c", "a\u{FFFD}b\u{FFFD}c"], + 'allowed whitespace and multibyte characters' => ["# Grüße 😀\t<&>\r\n", "# Grüße 😀\t<&>\r\n"], + 'UTF-16 with BOM' => ["\xff\xfe#\x00 \x00\xfc\x00", "\u{FEFF}# ü"], + ]; + } + + /** + * @dataProvider dataXmlInvalidContent + */ + public function testWorkspaceContentXmlInvalid(string $content, string $expected): void { + $this->fileService->method('canSeeContent')->willReturn(true); + $this->content = $content; + $this->cache->expects($this->once())->method('set')->with('42_etag', $expected); + + $this->assertSame($expected, $this->propFind()->get(WorkspacePlugin::WORKSPACE_PROPERTY)); + } + private function propFind(): PropFind { $propFind = new PropFind('files/user/folder', [ WorkspacePlugin::WORKSPACE_PROPERTY,