Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/DAV/WorkspacePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions lib/Service/EncodingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
25 changes: 24 additions & 1 deletion tests/unit/DAV/WorkspacePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -53,6 +55,7 @@ protected function setUp(): void {
$this->fileService,
$cacheFactory,
$configService,
new EncodingService(),
$this->createMock(LoggerInterface::class),
'user',
);
Expand Down Expand Up @@ -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,
Expand Down
Loading