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
2 changes: 1 addition & 1 deletion cmake/CompilerWarnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.
)

Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array>`), which `wf check` now proves.
Expand Down
2 changes: 1 addition & 1 deletion docs/PROJECT_STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/TEST_PERFORMANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<build>/jp`: on Windows the cache of `jasmine-core` holds a path beyond 260 characters in a longer root (#349).
journey sits under `<build>/jp`, a short root kept from before the developer store handled paths beyond 260 characters on Windows (#349).

## Resource audit

Expand Down
2 changes: 1 addition & 1 deletion include/webfront/DeveloperApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class BasicDeveloperApp : public BasicWF<NetProvider, fs::Multi<fs::DeveloperFS,
/// An explicit project is required before enabling development file operations.
explicit BasicDeveloperApp(std::filesystem::path projectDirectory = {}, WebFrontConfig config = {})
: Base(developer::projectConfig(std::move(config), projectDirectory)),
project(projectDirectory.empty() ? std::filesystem::path{} : std::filesystem::canonical(projectDirectory)) { }
project(projectDirectory.empty() ? std::filesystem::path{} : developer::canonicalDirectory(projectDirectory)) { }

void bootstrap(std::string_view mode,
DeveloperBootstrapOptions options = {.environment = std::string(developer::release::environment),
Expand Down
2 changes: 1 addition & 1 deletion include/webfront/system/DeveloperFS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DeveloperFS {
public:
explicit DeveloperFS(std::filesystem::path project) {
if (!project.empty())
root = std::filesystem::canonical(project);
root = developer::canonicalDirectory(project);
}

std::optional<File> open(std::filesystem::path file) const {
Expand Down
12 changes: 7 additions & 5 deletions include/webfront/system/DeveloperJournal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(const std::filesystem::path&)>& 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());
}
}
}
Expand Down
63 changes: 56 additions & 7 deletions include/webfront/system/DeveloperPaths.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,73 @@

#include <filesystem>
#include <stdexcept>
#include <string>
#include <string_view>

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;
Expand Down
6 changes: 3 additions & 3 deletions include/webfront/system/DeveloperStore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<const char*>(bytes.data()), static_cast<std::streamsize>(bytes.size()));
Expand Down Expand Up @@ -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);
}
Expand Down
79 changes: 79 additions & 0 deletions test/unit/DeveloperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<std::string> 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});
}
}
}
Loading