Files
Jonas b93d315dec fix(files): normalize Unicode in node paths
Filesystem::normalizePath() converts paths to Unicode NFC, so the
filecache and storage always use NFC names. The Node API uses
PathHelper::normalizePath() instead, which didn't normalize Unicode.
Nodes returned by Folder::newFile(), Folder::newFolder(), Node::copy()
and Node::move() therefore kept the name exactly as passed in.

Files uploaded from macOS in Chrome or Firefox have NFD names. Apps that
return $node->getName() after creating such a file sent back an NFD name
for a file that is stored as NFC. Other clients then couldn't match the
name against file listings, which broke attachments in Collectives and
Text.

Normalize to NFC in PathHelper::normalizePath(), like
Filesystem::normalizePath() does. Hooks and events emitted by the Node
API now get NFC paths as well.

Refs: nextcloud/collectives#2327
Refs: nextcloud/text#7873

Signed-off-by: Jonas <jonas@freesources.org>
Assisted-by: ClaudeCode:claude-opus-5-5
2026-10-15 16:40:08 +00:00

62 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Files\Utils;
class PathHelper {
/**
* Make a path relative to a root path, or return null if the path is outside the root
*
* @param string $root
* @param string $path
* @return ?string
*/
public static function getRelativePath(string $root, string $path) {
if ($root === '' || $root === '/') {
return self::normalizePath($path);
}
if ($path === $root) {
return '/';
} elseif (!str_starts_with($path, $root . '/')) {
return null;
} else {
$path = substr($path, strlen($root));
return self::normalizePath($path);
}
}
/**
* @param string $path
* @return string
*/
public static function normalizePath(string $path): string {
if ($path === '' || $path === '/') {
return '/';
}
// Use the same Unicode normalization as Filesystem::normalizePath(),
// so that node paths match the filecache
$path = \Normalizer::normalize($path) ?: $path;
// No null bytes
$path = str_replace(chr(0), '', $path);
//no windows style slashes
$path = str_replace('\\', '/', $path);
//add leading slash
if ($path[0] !== '/') {
$path = '/' . $path;
}
//remove duplicate slashes
while (str_contains($path, '//')) {
$path = str_replace('//', '/', $path);
}
//remove trailing slash
$path = rtrim($path, '/');
return $path;
}
}