diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake index 6a201896..3b76e614 100644 --- a/cmake/CompilerWarnings.cmake +++ b/cmake/CompilerWarnings.cmake @@ -31,7 +31,7 @@ function(set_project_warnings project_name) /wd4714 # '__forceinline' function not inlined: stdexec's coroutine helpers, which are not ours to change /analyze # Enable Code Analysis /analyze:external- # Disable Code Analysis for external headers - /analyze:stacksize 65536 # Raise C6262 stack warning threshold to 64KB (policy buffers can reach 32KB) + /analyze:stacksize65536 # Raise C6262 stack warning threshold to 64KB (policy buffers can reach 32KB); one argument, as sccache reads a separate "65536" as a second source file and refuses to cache the compilation /permissive- # standards conformance mode for MSVC compiler. ) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 1c1fb0ff..0cbfc669 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -9,6 +9,7 @@ ### Fixed +- The developer store reads and writes project paths beyond MAX_PATH on Windows (#349): `projectPath` takes the extended-length form from 200 characters, as does a project root longer than MAX_PATH (`canonicalDirectory`), containment is checked on the ordinary spelling, and staged files get the same treatment. The MSVC option is now `/analyze:stacksize65536`, one argument: with a space, sccache read `65536` as a second source file and refused to cache 48 compilations of the Windows CI build (about 9 minutes against 25 seconds on Linux). - The installer of the developer environment reads npm archives rooted at any single directory, as npm does (#316): the archives of `@types/*` are rooted at the package name and were refused as "Invalid npm tarball root". A mixed or escaping root is still refused. - The Life starter type-checks under TypeScript `strict` (`useState`), which `wf check` now proves. diff --git a/docs/PROJECT_STATUS.md b/docs/PROJECT_STATUS.md index c897573f..1c731ada 100644 --- a/docs/PROJECT_STATUS.md +++ b/docs/PROJECT_STATUS.md @@ -63,7 +63,7 @@ permissions (`JourneyPermissionDenied`), an emptied cache checked offline then r Green on CEF (GCC and Clang, public network cut), WebKitGTK, WKWebView and WebView2 in the [pull request run](https://github.com/ambroise-leclerc/WebFront/actions/runs/40081726319) and the [dispatch](https://github.com/ambroise-leclerc/WebFront/actions/runs/40081723204); the durations are in `docs/TEST_PERFORMANCE.md`. -ADR-013, ADR-014 and ADR-015 are accepted on these proofs. Known limit: file reads fail beyond 260 characters on Windows (#349). +ADR-013, ADR-014 and ADR-015 are accepted on these proofs. The developer store reads and writes project paths beyond 260 characters on Windows through the extended-length form (#349). The journey found two defects, now fixed with regression specs: the installer refused every archive whose root is not `package/` (all of `@types/*`, whose root is the package name; `npm` strips the first component whatever it is), and the Life starter diff --git a/docs/TEST_PERFORMANCE.md b/docs/TEST_PERFORMANCE.md index 4b571277..84d32b58 100644 --- a/docs/TEST_PERFORMANCE.md +++ b/docs/TEST_PERFORMANCE.md @@ -84,7 +84,7 @@ error cases) run one after the other on a single project. Their summed CTest dur The cases run under the same lock as the other browser tests, so the added time is about one minute per job, of which the compilation of the embedded application (`JourneyEmbeddedBuild`, serial like `LifeEmbeddedBuild`) is the largest part. The project of the -journey sits under `/jp`: on Windows the cache of `jasmine-core` holds a path beyond 260 characters in a longer root (#349). +journey sits under `/jp`, a short root kept from before the developer store handled paths beyond 260 characters on Windows (#349). ## Resource audit diff --git a/include/webfront/DeveloperApp.hpp b/include/webfront/DeveloperApp.hpp index aa92e66f..cd12889f 100644 --- a/include/webfront/DeveloperApp.hpp +++ b/include/webfront/DeveloperApp.hpp @@ -71,7 +71,7 @@ class BasicDeveloperApp : public BasicWF open(std::filesystem::path file) const { diff --git a/include/webfront/system/DeveloperJournal.hpp b/include/webfront/system/DeveloperJournal.hpp index e82e56a3..a4626886 100644 --- a/include/webfront/system/DeveloperJournal.hpp +++ b/include/webfront/system/DeveloperJournal.hpp @@ -83,14 +83,16 @@ inline void syncDirectory(const std::filesystem::path& directory) { /// of each one it creates: the entry of a new directory is only durable once its parent is. inline void createDirectoriesDurably(const std::filesystem::path& root, const std::filesystem::path& directory, const std::function& sync = syncDirectory) { - auto current = root; - for (const auto& part : directory.lexically_relative(root)) { + // The ordinary spellings are compared; each directory takes the extended form once its path is long. + auto current = plainPath(root); + for (const auto& part : plainPath(directory).lexically_relative(current)) { if (part == ".") continue; current /= part; - if (!std::filesystem::exists(current)) { - std::filesystem::create_directory(current); - sync(current.parent_path()); + const auto path = extendedPath(current); + if (!std::filesystem::exists(path)) { + std::filesystem::create_directory(path); + sync(path.parent_path()); } } } diff --git a/include/webfront/system/DeveloperPaths.hpp b/include/webfront/system/DeveloperPaths.hpp index 338620cf..6a15fa98 100644 --- a/include/webfront/system/DeveloperPaths.hpp +++ b/include/webfront/system/DeveloperPaths.hpp @@ -5,24 +5,73 @@ #include #include +#include #include namespace webfront::developer { +/// Paths from this length on take the extended-length form on Windows, below MAX_PATH (260) to leave room for appended names. +inline constexpr std::size_t extendedPathThreshold = 200; + +#ifdef _WIN32 +// Spelled with \x5c (a backslash): the prefixes are \?\ and \?\UNC\ for a network share, which is \ in its ordinary form. +inline constexpr std::wstring_view networkPrefix = L"\x5c" L"\x5c"; +inline constexpr std::wstring_view extendedPrefix = L"\x5c" L"\x5c" L"?" L"\x5c"; +inline constexpr std::wstring_view uncPrefix = L"\x5c" L"\x5c" L"?" L"\x5c" L"UNC" L"\x5c"; +#endif + +/// The form of an absolute path that the Windows file APIs accept beyond MAX_PATH; other platforms keep the path as is. +inline std::filesystem::path extendedPath(const std::filesystem::path& path) { +#ifdef _WIN32 + if (path.native().size() < extendedPathThreshold || path.native().starts_with(extendedPrefix) || !path.has_root_name()) + return path; + // The extended form turns off the normalization of the system: separators and dot segments are resolved here. + auto native = std::filesystem::absolute(path).lexically_normal().make_preferred().native(); + if (native.starts_with(networkPrefix)) + native = std::wstring(uncPrefix) + native.substr(2); + else + native = std::wstring(extendedPrefix) + native; + return native; +#else + return path; +#endif +} + +/// The ordinary spelling of a path, so that the extended and the ordinary forms of one location compare equal. +inline std::filesystem::path plainPath(const std::filesystem::path& path) { +#ifdef _WIN32 + const auto& native = path.native(); + if (native.starts_with(uncPrefix)) + return std::wstring(networkPrefix) + native.substr(uncPrefix.size()); + if (native.starts_with(extendedPrefix)) + return native.substr(extendedPrefix.size()); +#endif + return path; +} + +/// The canonical absolute path of an existing project directory, in the extended-length form when it is long: +/// canonical() of the system functions refuses the ordinary spelling beyond MAX_PATH. +inline std::filesystem::path canonicalDirectory(const std::filesystem::path& directory) { + return extendedPath(plainPath(std::filesystem::canonical(extendedPath(std::filesystem::absolute(directory))))); +} + // Resolve existing parents too: a symlink must never turn a relative project path into an escape. inline std::filesystem::path projectPath(const std::filesystem::path& root, std::string_view relative) { - const auto path = root / fs::safeRelativePath(relative); - const auto resolved = std::filesystem::weakly_canonical(path); - auto base = root.begin(); - auto part = resolved.begin(); - for (; base != root.end(); ++base, ++part) + const auto path = extendedPath(root / fs::safeRelativePath(relative)); + const auto resolved = plainPath(std::filesystem::weakly_canonical(path)); + const auto plainRoot = plainPath(root); + auto base = plainRoot.begin(); + auto part = resolved.begin(); + for (; base != plainRoot.end(); ++base, ++part) if (part == resolved.end() || *part != *base) throw std::invalid_argument("Path escapes the developer project"); return path; } -inline bool isParent(const std::filesystem::path& parent, const std::filesystem::path& child) { - auto part = child.begin(); +inline bool isParent(const std::filesystem::path& parentPath, const std::filesystem::path& childPath) { + const auto parent = plainPath(parentPath); + const auto child = plainPath(childPath); + auto part = child.begin(); for (const auto& component : parent) { if (part == child.end() || *part != component) return false; diff --git a/include/webfront/system/DeveloperStore.hpp b/include/webfront/system/DeveloperStore.hpp index be6629eb..16d28c15 100644 --- a/include/webfront/system/DeveloperStore.hpp +++ b/include/webfront/system/DeveloperStore.hpp @@ -35,7 +35,7 @@ class Store { /// and recoveryFailure() describes it meanwhile. The hook is for tests only. explicit Store(std::filesystem::path project, std::chrono::steady_clock::duration abandoned = defaultAbandonedAfter, StepHook stepHook = {}) - : root(std::filesystem::canonical(project)), abandonedAfter(abandoned), hook(std::move(stepHook)) { + : root(canonicalDirectory(project)), abandonedAfter(abandoned), hook(std::move(stepHook)) { if (!std::filesystem::is_directory(root)) throw std::invalid_argument("Developer project must be a directory"); removeStaleStaging(root); @@ -130,7 +130,7 @@ class Store { auto& size = tx.files[relative.generic_string()]; if (offset != size) throw std::invalid_argument("Non-sequential developer write"); - const auto target = tx.staging / relative; + const auto target = extendedPath(tx.staging / relative); std::filesystem::create_directories(target.parent_path()); std::ofstream file(target, std::ios::binary | std::ios::app); file.write(reinterpret_cast(bytes.data()), static_cast(bytes.size())); @@ -398,7 +398,7 @@ class Store { // The staging area and the destinations of unfinished transactions are off limits. void requireFreeDestination(const std::filesystem::path& resolved, const std::filesystem::path& staging) const { - if (resolved == root || related(resolved, staging)) + if (plainPath(resolved) == plainPath(root) || related(resolved, staging)) throw std::invalid_argument("Invalid publication destination"); requireNoOverlap(resolved); } diff --git a/test/unit/DeveloperTests.cpp b/test/unit/DeveloperTests.cpp index 4453b6fd..4a6e063a 100644 --- a/test/unit/DeveloperTests.cpp +++ b/test/unit/DeveloperTests.cpp @@ -796,3 +796,82 @@ SCENARIO("A project directory is listed without leaving the project") { } } } + +SCENARIO("A project path beyond the 260 characters of Windows is published and read like any other") { + TemporaryDirectory project; + developer::Store store{project.path()}; + GIVEN("a destination whose absolute path exceeds MAX_PATH") { + std::string directory = "deep"; + while (project.path().native().size() + directory.size() < 300) + directory += "/" + std::string(40, 'd'); + const auto destination = directory + "/file.txt"; + WHEN("a file is published there") { + const auto id = store.begin(destination, false); + store.write(id, "output", 0, bytes("long")); + store.commit(id); + THEN("it can be read back and replaced") { + REQUIRE(read(store, destination) == "long"); + const auto second = store.begin(destination, false); + store.write(second, "output", 0, bytes("longer")); + store.commit(second); + REQUIRE(read(store, destination) == "longer"); + } + THEN("a path escaping the project is still refused") { + REQUIRE_THROWS_AS(store.read(directory + "/../../../../../../../../../../../../../../../../../outside", 0), std::invalid_argument); + } + } + } +} + +SCENARIO("A group of files beyond the 260 characters of Windows is published together") { + TemporaryDirectory project; + developer::Store store{project.path()}; + GIVEN("two destinations in directories that do not exist yet, with absolute paths beyond MAX_PATH") { + std::string directory = "deep"; + while (project.path().native().size() + directory.size() < 300) + directory += "/" + std::string(40, 'd'); + const std::vector paths{directory + "/a.txt", directory + "/b.txt"}; + WHEN("they are published as one group") { + const auto first = store.begin(paths[0], false); + const auto second = store.begin(paths[1], false); + store.write(first, "output", 0, bytes("one")); + store.write(second, "output", 0, bytes("two")); + store.commitGroup({first, second}, paths, store.state(paths)); + THEN("both are readable and no journal remains") { + REQUIRE(read(store, paths[0]) == "one"); + REQUIRE(read(store, paths[1]) == "two"); + REQUIRE_FALSE(journalsRemain(project.path() / ".webfront/developer/journal")); + } + } + } +} + +SCENARIO("A project whose own directory exceeds the 260 characters of Windows can be opened") { + TemporaryDirectory outer; + auto root = outer.path(); + while (root.native().size() < 302) + root /= std::string(40, 'r'); + std::filesystem::create_directories(developer::extendedPath(root)); + GIVEN("a store on that directory") { + developer::Store store{root}; + WHEN("a file is published and a group is committed") { + publishFile(store, "file.txt", "root"); + const std::vector paths{"sub/a.txt", "sub/b.txt"}; + const auto first = store.begin(paths[0], false); + const auto second = store.begin(paths[1], false); + store.write(first, "output", 0, bytes("one")); + store.write(second, "output", 0, bytes("two")); + store.commitGroup({first, second}, paths, store.state(paths)); + THEN("everything is readable and the project stays confined") { + REQUIRE(read(store, "file.txt") == "root"); + REQUIRE(read(store, "sub/b.txt") == "two"); + REQUIRE_THROWS_AS(store.read("../outside", 0), std::invalid_argument); + } + } + } + GIVEN("a developer filesystem on that directory") { + THEN("it opens") { + REQUIRE_NOTHROW(fs::DeveloperFS{root}); + } + } +}