From 6e2bea8117430ed887fab0ee85c5a9048c543b0e Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:52:50 +0100 Subject: [PATCH 1/4] Launch the developer environment from its verified cache without rewriting it (#570) Every launch read the cached archive back through the bridge, verified and unpacked it, then published its 14 MB again (about 90 bridge round trips and 18 MB of writes), even when nothing had changed: 1.5 s of the 2.5 s that a wf command of the tests spends starting. The store gains digests(directory): the SHA-256 of every file below a project directory, computed in C++ and returned in one call, walking what list() names and skipping links. DeveloperApp exposes it as webfront.developer.digests, for the bootstrap shipped with the same library. A launch from the verified cache compares those digests with the files of the archive and publishes the environment again only when one differs, is missing or is extra; the archive itself, already verified, is no longer rewritten. The files served remain those of the verified archive, and a C++ without the callback keeps publishing at every launch. The SHA-256 of the page authenticator moves to utils/Sha256.hpp and becomes incremental, so that a file is digested block by block. A wf command of the tests now starts in 1.64 s on average instead of 2.52 s (Windows, WebView2). --- docs/CHANGELOG.md | 1 + docs/DEVELOPER_ENVIRONMENT.md | 5 +- include/webfront/DeveloperApp.hpp | 2 + .../automation/PageAuthentication.hpp | 51 +---- .../system/DeveloperBootstrapData.hpp | 175 +++++++++--------- include/webfront/system/DeveloperStore.hpp | 40 ++++ include/webfront/utils/Sha256.hpp | 116 ++++++++++++ js/core/bootstrap/bootstrap.bundle.mjs | 2 +- js/developer/environment/bootstrap.mjs | 30 ++- js/developer/environment/bundles.json | 4 +- test/developer/specs/environment-specs.mjs | 50 ++++- test/unit/DeveloperTests.cpp | 59 ++++++ 12 files changed, 393 insertions(+), 142 deletions(-) create mode 100644 include/webfront/utils/Sha256.hpp diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 10fa1973..d5fd056c 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -24,6 +24,7 @@ these entries stays in the GitHub releases and the Git history. ### Changed +- Launch the developer environment from its verified cache without rewriting it: the bootstrap compares the SHA-256 of the published files, which the new `webfront.developer.digests` callback computes in C++, with the verified archive, and publishes it again only when a file differs. A `wf` command of the tests starts in 1.6 s instead of 2.5 s (#570, `docs/DEVELOPER_ENVIRONMENT.md`). - Replace Catch2 with `test/unit/Bdd.hpp`, a single-header BDD framework: scenarios are `Scenario` objects whose `given`/`when`/`then` sections are lambdas, checked with `require`/`check`. The tests no longer download or compile Catch2; `tests` takes `--exact`, `--list`, `--junit` and the former filters, order and shard options (ADR-012, `docs/TEST_TIERS.md`). - Wait for asynchronous states in unit tests with `requireEventually`, `checkEventually`, `requireReady` and `checkReady` of `Bdd.hpp`, which report the delay and the time waited and replace nine local helpers; `WEBFRONT_TEST_TIMEOUT_FACTOR` lengthens the delays of a slow job (#582, `docs/TEST_TIERS.md`, ADR-012). - Keep automated browser windows visible (#544, #556, `docs/TEST_PERFORMANCE.md`). diff --git a/docs/DEVELOPER_ENVIRONMENT.md b/docs/DEVELOPER_ENVIRONMENT.md index 85f04b53..c9b71f25 100644 --- a/docs/DEVELOPER_ENVIRONMENT.md +++ b/docs/DEVELOPER_ENVIRONMENT.md @@ -97,7 +97,10 @@ See [publication instructions](../js/developer/tools/PUBLISHING.md) for deployme The bootstrap verifies SHA-512 before unpacking or executing the archive, validates its manifest and paths, then publishes it under the application's local origin. The cached **archive** is -verified again before reuse, allowing subsequent launches without network access. Failure leaves +verified again before reuse, allowing subsequent launches without network access. Such a launch +asks the C++ for the SHA-256 of every published file (`webfront.developer.digests`, which the bootstrap +shipped with the same library uses) and publishes the environment again only when one differs from the +verified archive, or is missing or extra: the files served are always those of the archive. Failure leaves a readable status and Retry button. A different pinned hash requires a matching archive; the old environment is not silently substituted. diff --git a/include/webfront/DeveloperApp.hpp b/include/webfront/DeveloperApp.hpp index 26ccffcc..0c4b58de 100644 --- a/include/webfront/DeveloperApp.hpp +++ b/include/webfront/DeveloperApp.hpp @@ -115,6 +115,8 @@ class BasicDeveloperApp : public BasicWF("webfront.developer.list"); // Added after API 1 was published: an environment looks for it and does without (ls -l) when it is missing. expose<&Store::entries, Bytes, std::string>("webfront.developer.entries"); + // For the bootstrap shipped with this C++: it republishes the environment only when a published file differs. + expose<&Store::digests, Bytes, std::string>("webfront.developer.digests"); expose<&Store::beginReplacing, std::string, std::string, bool>("webfront.developer.begin"); expose<&Store::beginNew, std::string, std::string, bool>("webfront.developer.beginNew"); expose<&Store::write, void, std::string, std::string, std::uint64_t, Bytes>("webfront.developer.write"); diff --git a/include/webfront/automation/PageAuthentication.hpp b/include/webfront/automation/PageAuthentication.hpp index 41699fd3..19f3c1ae 100644 --- a/include/webfront/automation/PageAuthentication.hpp +++ b/include/webfront/automation/PageAuthentication.hpp @@ -1,60 +1,13 @@ /// @brief Authenticate page callbacks without putting a reusable secret on their wire messages. #pragma once #include "Channel.hpp" +#include "../utils/Sha256.hpp" #include #include namespace webfront::automation::authentication { -// SHA-256 (FIPS 180-4), used only by the optional page authenticator. Returns 32 raw bytes. -inline std::string sha256(std::string_view input) { - static constexpr std::array constants{ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, - 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, - 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, - 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, - 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, - 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; - std::array state{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; - std::string padded{input}; - padded += static_cast(0x80); - while (padded.size() % 64 != 56) - padded += '\0'; - const auto bits = static_cast(input.size()) * 8; - for (int shift = 56; shift >= 0; shift -= 8) - padded += static_cast((bits >> shift) & 255); - for (std::size_t offset = 0; offset < padded.size(); offset += 64) { - std::array words{}; - for (std::size_t i = 0; i < 16; ++i) - for (std::size_t j = 0; j < 4; ++j) - words[i] = (words[i] << 8) | static_cast(static_cast(padded[offset + i * 4 + j])); - for (std::size_t i = 16; i < 64; ++i) { - const auto x = words[i - 15], y = words[i - 2]; - words[i] = words[i - 16] + (std::rotr(x, 7) ^ std::rotr(x, 18) ^ (x >> 3)) + words[i - 7] + (std::rotr(y, 17) ^ std::rotr(y, 19) ^ (y >> 10)); - } - auto [a, b, c, d, e, f, g, h] = state; - for (std::size_t i = 0; i < 64; ++i) { - const auto first = h + (std::rotr(e, 6) ^ std::rotr(e, 11) ^ std::rotr(e, 25)) + ((e & f) ^ (~e & g)) + constants[i] + words[i]; - const auto second = (std::rotr(a, 2) ^ std::rotr(a, 13) ^ std::rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c)); - h = g; - g = f; - f = e; - e = d + first; - d = c; - c = b; - b = a; - a = first + second; - } - const std::array values{a, b, c, d, e, f, g, h}; - for (std::size_t i = 0; i < state.size(); ++i) - state[i] += values[i]; - } - std::string digest; - for (const auto word : state) - for (int shift = 24; shift >= 0; shift -= 8) - digest += static_cast((word >> shift) & 255); - return digest; -} +using utils::sha256; // 32 raw bytes, FIPS 180-4 // RFC 2104 construction; RFC 4231 vectors cover padding, binary keys and long keys/messages. inline std::string hmac(std::string_view key, std::string_view message) { const std::string normalized = key.size() > 64 ? sha256(key) : std::string(key); diff --git a/include/webfront/system/DeveloperBootstrapData.hpp b/include/webfront/system/DeveloperBootstrapData.hpp index 560a3396..bc72af36 100644 --- a/include/webfront/system/DeveloperBootstrapData.hpp +++ b/include/webfront/system/DeveloperBootstrapData.hpp @@ -9,9 +9,9 @@ namespace webfront::fs::generated { struct DeveloperBootstrapData { static constexpr std::string_view encoding{}; - static constexpr std::size_t dataSize{6619}; - static constexpr std::array data{ - 0x766172206c3d6e65, 0x7720546578744465, 0x636f6465723b6173, 0x796e632066756e63, 0x74696f6e20772865, + static constexpr std::size_t dataSize{6966}; + static constexpr std::array data{ + 0x76617220643d6e65, 0x7720546578744465, 0x636f6465723b6173, 0x796e632066756e63, 0x74696f6e20772865, 0x2c74297b6c657420, 0x723d537472696e67, 0x28743f3f2222292e, 0x73706c6974282f5c, 0x732b2f292e66696c, 0x74657228613d3e61, 0x2e73746172747357, 0x6974682822736861, 0x3531322d2229292e, 0x6d617028613d3e61, 0x2e73706c69742822, 0x3f22295b305d293b, 0x696628722e6c656e, 0x6774683d3d3d3029, 0x7468726f77206e65, @@ -24,7 +24,7 @@ struct DeveloperBootstrapData { 0x792e66726f6d286e, 0x2c613d3e53747269, 0x6e672e66726f6d43, 0x686172436f646528, 0x6129292e6a6f696e, 0x28222229293b6966, 0x2821722e696e636c, 0x75646573286f2929, 0x7468726f77206e65, 0x77204572726f7228, 0x60496e7465677269, 0x747920636865636b, 0x206661696c65643a, 0x2065787065637465, 0x6420247b725b305d, - 0x7d2c20676f742024, 0x7b6f7d60293b7265, 0x7475726e206f7d61, 0x73796e632066756e, 0x6374696f6e206828, + 0x7d2c20676f742024, 0x7b6f7d60293b7265, 0x7475726e206f7d61, 0x73796e632066756e, 0x6374696f6e207928, 0x652c743d32363834, 0x3335343536297b6c, 0x657420723d6e6577, 0x20426c6f62285b65, 0x5d292e7374726561, 0x6d28292e70697065, 0x5468726f75676828, 0x6e6577204465636f, 0x6d7072657373696f, 0x6e53747265616d28, 0x22677a6970222929, 0x2e67657452656164, 0x657228292c6e3d5b, 0x5d2c6f3d303b666f, 0x72286c657420613d, @@ -35,148 +35,157 @@ struct DeveloperBootstrapData { 0x297d72657475726e, 0x206e65772055696e, 0x7438417272617928, 0x6177616974206e65, 0x7720426c6f62286e, 0x292e617272617942, 0x7566666572282929, 0x7d66756e6374696f, 0x6e207528652c742c, 0x72297b6c6574206e, 0x3d652e7375626172, 0x72617928742c742b, 0x72292c6f3d6e2e69, 0x6e6465784f662830, 0x293b72657475726e, - 0x206c2e6465636f64, 0x65286f3c303f6e3a, 0x6e2e737562617272, 0x617928302c6f2929, 0x7d66756e6374696f, - 0x6e207928652c742c, 0x72297b696628655b, 0x745d263132382972, 0x657475726e20652e, 0x7375626172726179, + 0x20642e6465636f64, 0x65286f3c303f6e3a, 0x6e2e737562617272, 0x617928302c6f2929, 0x7d66756e6374696f, + 0x6e206d28652c742c, 0x72297b696628655b, 0x745d263132382972, 0x657475726e20652e, 0x7375626172726179, 0x28742b312c742b72, 0x292e726564756365, 0x28286f2c61293d3e, 0x6f2a3235362b612c, 0x655b745d26313237, 0x293b6c6574206e3d, 0x7528652c742c7229, 0x2e7472696d28293b, 0x696628212f5e5b30, 0x2d375d2a242f2e74, 0x657374286e292974, 0x68726f77206e6577, 0x204572726f722822, 0x436f727275707420, 0x7461722068656164, 0x657222293b726574, 0x75726e206e3d3d3d, 0x22223f303a706172, 0x7365496e74286e2c, 0x38297d66756e6374, - 0x696f6e207a286529, 0x7b6c657420743d30, 0x3b666f72286c6574, 0x20723d303b723c35, 0x31323b2b2b722974, - 0x2b3d723e3d313438, 0x2626723c3135363f, 0x33323a655b725d3b, 0x6966287928652c31, 0x34382c3829213d3d, + 0x696f6e2042286529, 0x7b6c657420743d30, 0x3b666f72286c6574, 0x20723d303b723c35, 0x31323b2b2b722974, + 0x2b3d723e3d313438, 0x2626723c3135363f, 0x33323a655b725d3b, 0x6966286d28652c31, 0x34382c3829213d3d, 0x74297468726f7720, 0x6e6577204572726f, 0x722822436f727275, 0x7074207461722068, 0x6561646572206368, - 0x65636b73756d2229, 0x7d66756e6374696f, 0x6e20492865297b6c, 0x657420743d7b7d2c, 0x723d303b666f7228, + 0x65636b73756d2229, 0x7d66756e6374696f, 0x6e20432865297b6c, 0x657420743d7b7d2c, 0x723d303b666f7228, 0x3b723c652e6c656e, 0x6774683b297b6c65, 0x74206e3d652e696e, 0x6465784f66283332, 0x2c72292c6f3d6e3c, - 0x303f4e614e3a4e75, 0x6d626572286c2e64, 0x65636f646528652e, 0x7375626172726179, 0x28722c6e2929293b, + 0x303f4e614e3a4e75, 0x6d62657228642e64, 0x65636f646528652e, 0x7375626172726179, 0x28722c6e2929293b, 0x69662821286f3e30, 0x297c7c722b6f3e65, 0x2e6c656e67746829, 0x7468726f77206e65, 0x77204572726f7228, - 0x22436f7272757074, 0x2070617820686561, 0x64657222293b6c65, 0x7420613d6c2e6465, 0x636f646528652e73, + 0x22436f7272757074, 0x2070617820686561, 0x64657222293b6c65, 0x7420613d642e6465, 0x636f646528652e73, 0x7562617272617928, 0x6e2b312c722b6f2d, 0x3129292c693d612e, 0x696e6465784f6628, 0x223d22293b745b61, 0x2e736c6963652830, 0x2c69295d3d612e73, 0x6c69636528692b31, 0x292c722b3d6f7d72, 0x657475726e20747d, - 0x66756e6374696f6e, 0x204228652c74297b, 0x6c657420723d652e, 0x7375626172726179, 0x28742c742b353132, - 0x293b7a2872293b6c, 0x6574206e3d792872, 0x2c3132342c313229, 0x2c6f3d742b353132, 0x3b6966286f2b6e3e, + 0x66756e6374696f6e, 0x205528652c74297b, 0x6c657420723d652e, 0x7375626172726179, 0x28742c742b353132, + 0x293b422872293b6c, 0x6574206e3d6d2872, 0x2c3132342c313229, 0x2c6f3d742b353132, 0x3b6966286f2b6e3e, 0x652e6c656e677468, 0x297468726f77206e, 0x6577204572726f72, 0x28225472756e6361, 0x7465642074617220, 0x6172636869766522, 0x293b72657475726e, 0x7b6865616465723a, 0x722c747970653a72, 0x5b3135365d3d3d3d, 0x303f2230223a5374, 0x72696e672e66726f, 0x6d43686172436f64, 0x6528725b3135365d, 0x292c646174613a65, 0x2e73756261727261, 0x79286f2c6f2b6e29, 0x2c6e6578743a6f2b, 0x4d6174682e636569, 0x6c286e2f35313229, - 0x2a3531327d7d6675, 0x6e6374696f6e2043, 0x28652c74297b7265, 0x7475726e20742b35, 0x31323e652e6c656e, + 0x2a3531327d7d6675, 0x6e6374696f6e2041, 0x28652c74297b7265, 0x7475726e20742b35, 0x31323e652e6c656e, 0x6774687c7c652e73, 0x7562617272617928, 0x742c742b35313229, 0x2e65766572792872, 0x3d3e723d3d3d3029, - 0x7d66756e6374696f, 0x6e20552865297b6c, 0x657420743d752865, 0x2c302c313030292c, 0x723d7528652c3235, + 0x7d66756e6374696f, 0x6e204f2865297b6c, 0x657420743d752865, 0x2c302c313030292c, 0x723d7528652c3235, 0x372c35293d3d3d22, 0x7573746172223f75, 0x28652c3334352c31, 0x3535293a22223b72, 0x657475726e20723f, - 0x60247b727d2f247b, 0x747d603a747d7661, 0x7220703d7b783a28, 0x652c74293d3e287b, 0x2e2e2e652c706178, - 0x3a492874292e7061, 0x74687d292c4c3a28, 0x652c74293d3e287b, 0x2e2e2e652c6c6f6e, 0x673a7528742c302c, - 0x742e6c656e677468, 0x297d292c673a653d, 0x3e657d3b66756e63, 0x74696f6e206d2865, 0x297b6c657420743d, - 0x5b5d2c723d7b7d3b, 0x666f72286c657420, 0x6e3d303b21432865, 0x2c6e293b297b6c65, 0x747b686561646572, - 0x3a6f2c747970653a, 0x612c646174613a69, 0x2c6e6578743a737d, 0x3d4228652c6e293b, 0x6966286e3d732c4f, - 0x626a6563742e6861, 0x734f776e28702c61, 0x29297b723d705b61, 0x5d28722c69293b63, 0x6f6e74696e75657d, + 0x60247b727d2f247b, 0x747d603a747d7661, 0x7220683d7b783a28, 0x652c74293d3e287b, 0x2e2e2e652c706178, + 0x3a432874292e7061, 0x74687d292c4c3a28, 0x652c74293d3e287b, 0x2e2e2e652c6c6f6e, 0x673a7528742c302c, + 0x742e6c656e677468, 0x297d292c673a653d, 0x3e657d3b66756e63, 0x74696f6e20622865, 0x297b6c657420743d, + 0x5b5d2c723d7b7d3b, 0x666f72286c657420, 0x6e3d303b21412865, 0x2c6e293b297b6c65, 0x747b686561646572, + 0x3a6f2c747970653a, 0x612c646174613a69, 0x2c6e6578743a737d, 0x3d5528652c6e293b, 0x6966286e3d732c4f, + 0x626a6563742e6861, 0x734f776e28682c61, 0x29297b723d685b61, 0x5d28722c69293b63, 0x6f6e74696e75657d, 0x28613d3d3d223022, 0x7c7c613d3d3d2237, 0x22292626742e7075, 0x7368287b70617468, 0x3a722e7061783f3f, - 0x722e6c6f6e673f3f, 0x55286f292c627974, 0x65733a697d292c72, 0x3d7b7d7d72657475, 0x726e20747d766172, + 0x722e6c6f6e673f3f, 0x4f286f292c627974, 0x65733a697d292c72, 0x3d7b7d7d72657475, 0x726e20747d766172, 0x20633d28652c742c, 0x2e2e2e72293d3e65, 0x2e63707046756e63, 0x74696f6e28607765, 0x6266726f6e742e64, - 0x6576656c6f706572, 0x2e247b747d602928, 0x2e2e2e72293b6173, 0x796e632066756e63, 0x74696f6e20622865, + 0x6576656c6f706572, 0x2e247b747d602928, 0x2e2e2e72293b6173, 0x796e632066756e63, 0x74696f6e20672865, 0x2c74297b6c657420, 0x723d5b5d2c6e3d30, 0x3b666f72283b3b29, 0x7b6c6574206f3d61, 0x7761697420632865, 0x2c2272656164222c, 0x742c6e293b696628, 0x722e70757368286f, 0x292c6e2b3d6f2e6c, 0x656e6774682c6f2e, 0x6c656e6774683c32, 0x3632313434296272, 0x65616b3b6966286e, 0x3e3235362a313032, 0x342a313032342974, 0x68726f77206e6577, 0x204572726f722822, 0x50726f6a65637420, 0x66696c6520697320, 0x746f6f206c617267, 0x6522297d72657475, 0x726e206e65772055, 0x696e743841727261, 0x7928617761697420, 0x6e657720426c6f62, - 0x2872292e61727261, 0x7942756666657228, 0x29297d6173796e63, 0x2066756e6374696f, 0x6e202428652c7b69, + 0x2872292e61727261, 0x7942756666657228, 0x29297d6173796e63, 0x2066756e6374696f, 0x6e205028652c7b69, 0x643a742c70617468, 0x3a722c6279746573, 0x3a6e7d2c6f297b66, 0x6f72286c65742061, 0x3d303b613c6e2e6c, 0x656e6774687c7c61, 0x3d3d3d303b612b3d, 0x323632313434296f, 0x3f2e7468726f7749, 0x6641626f72746564, 0x28292c6177616974, 0x206328652c227772, 0x697465222c742c72, 0x2c612c6e2e737562, 0x617272617928612c, - 0x612b323632313434, 0x29297d6173796e63, 0x2066756e6374696f, 0x6e206428652c742c, 0x722c7b6469726563, + 0x612b323632313434, 0x29297d6173796e63, 0x2066756e6374696f, 0x6e206628652c742c, 0x722c7b6469726563, 0x746f72793a6e3d21, 0x302c7369676e616c, 0x3a6f2c7265706c61, 0x63653a613d21307d, 0x3d7b7d297b6c6574, 0x20693d6177616974, 0x206328652c613f22, 0x626567696e223a22, 0x626567696e4e6577, 0x222c742c6e293b74, - 0x72797b666f72286c, 0x65745b732c545d6f, 0x6620722961776169, 0x74202428652c7b69, 0x643a692c70617468, - 0x3a732c6279746573, 0x3a547d2c6f293b6f, 0x3f2e7468726f7749, 0x6641626f72746564, 0x28292c6177616974, + 0x72797b666f72286c, 0x65745b732c6c5d6f, 0x6620722961776169, 0x74205028652c7b69, 0x643a692c70617468, + 0x3a732c6279746573, 0x3a6c7d2c6f293b6f, 0x3f2e7468726f7749, 0x6641626f72746564, 0x28292c6177616974, 0x206328652c22636f, 0x6d6d6974222c6929, 0x7d63617463682873, 0x297b7468726f7720, 0x6177616974206328, 0x652c2261626f7274, 0x222c69292e636174, 0x63682828293d3e7b, 0x7d292c737d7d6173, 0x796e632066756e63, - 0x74696f6e20502865, 0x2c74297b6c657420, 0x723d5b5d2c6e3d30, 0x3b666f72283b3b29, 0x7b6c65747b646f6e, + 0x74696f6e20242865, 0x2c74297b6c657420, 0x723d5b5d2c6e3d30, 0x3b666f72283b3b29, 0x7b6c65747b646f6e, 0x653a6f2c76616c75, 0x653a617d3d617761, 0x697420652e726561, 0x6428293b6966286f, 0x2972657475726e20, 0x723b6966286e2b3d, 0x612e6c656e677468, 0x2c6e3e7429746872, 0x6f77206e65772045, 0x72726f722822436f, 0x6d70726573736564, 0x20646f776e6c6f61, 0x6420657863656564, 0x7320697473206279, 0x7465206c696d6974, - 0x22293b722e707573, 0x682861297d7d6173, 0x796e632066756e63, 0x74696f6e204f2865, 0x2c74297b7472797b, - 0x72657475726e2061, 0x7761697420502865, 0x2c74297d63617463, 0x682872297b746872, 0x6f77206177616974, + 0x22293b722e707573, 0x682861297d7d6173, 0x796e632066756e63, 0x74696f6e206a2865, 0x2c74297b7472797b, + 0x72657475726e2061, 0x7761697420242865, 0x2c74297d63617463, 0x682872297b746872, 0x6f77206177616974, 0x20652e63616e6365, 0x6c28292e63617463, 0x682828293d3e7b7d, 0x292c727d66696e61, 0x6c6c797b652e7265, - 0x6c656173654c6f63, 0x6b28297d7d617379, 0x6e632066756e6374, 0x696f6e206728652c, 0x743d36342a313032, + 0x6c656173654c6f63, 0x6b28297d7d617379, 0x6e632066756e6374, 0x696f6e207628652c, 0x743d36342a313032, 0x342a31303234297b, 0x69662821652e6f6b, 0x297468726f77206e, 0x6577204572726f72, 0x2860446f776e6c6f, 0x61643a2048545450, 0x20247b652e737461, 0x7475737d60293b69, 0x66284e756d626572, 0x28652e6865616465, 0x72732e6765742822, 0x636f6e74656e742d, 0x6c656e6774682229, 0x293e74297468726f, 0x77206e6577204572, 0x726f722822436f6d, 0x7072657373656420, 0x646f776e6c6f6164, 0x2065786365656473, 0x2069747320627974, 0x65206c696d697422, 0x293b72657475726e, 0x206e65772055696e, 0x7438417272617928, 0x6177616974206e65, - 0x7720426c6f622861, 0x77616974204f2865, 0x2e626f64792e6765, 0x7452656164657228, 0x292c7429292e6172, - 0x7261794275666665, 0x722829297d766172, 0x206a3d2f5b5c5c5c, 0x303a2a3f223c3e7c, 0x5d2f2c413d653d3e, + 0x7720426c6f622861, 0x77616974206a2865, 0x2e626f64792e6765, 0x7452656164657228, 0x292c7429292e6172, + 0x7261794275666665, 0x722829297d766172, 0x204e3d2f5b5c5c5c, 0x303a2a3f223c3e7c, 0x5d2f2c463d653d3e, 0x652e73706c697428, 0x222f22292e736f6d, 0x6528743d3e21747c, 0x7c743d3d3d222e22, 0x7c7c743d3d3d222e, - 0x2e22293b66756e63, 0x74696f6e20762865, 0x297b72657475726e, 0x20747970656f6620, 0x65213d2273747269, - 0x6e67227c7c21657c, 0x7c6a2e7465737428, 0x65293f21303a4128, 0x65297d7661722066, 0x3d653d3e21762865, - 0x292c4e3d6e657720, 0x546578744465636f, 0x6465723b66756e63, 0x74696f6e20462865, 0x2c74297b69662821, + 0x2e22293b66756e63, 0x74696f6e20782865, 0x297b72657475726e, 0x20747970656f6620, 0x65213d2273747269, + 0x6e67227c7c21657c, 0x7c4e2e7465737428, 0x65293f21303a4628, 0x65297d7661722070, 0x3d653d3e21782865, + 0x292c453d6e657720, 0x546578744465636f, 0x6465723b66756e63, 0x74696f6e204c2865, 0x2c74297b69662821, 0x742e706174682e73, 0x7461727473576974, 0x6828227061636b61, 0x67652f2229297468, 0x726f77206e657720, 0x4572726f72282249, 0x6e76616c69642065, 0x6e7669726f6e6d65, 0x6e74206172636869, 0x766520726f6f7422, - 0x293b6c657420723d, 0x742e706174682e73, 0x6c6963652838293b, 0x6966282166287229, 0x7c7c652e68617328, + 0x293b6c657420723d, 0x742e706174682e73, 0x6c6963652838293b, 0x6966282170287229, 0x7c7c652e68617328, 0x7229297468726f77, 0x206e657720457272, 0x6f722822496e7661, 0x6c6964206f722064, 0x75706c6963617465, 0x20656e7669726f6e, 0x6d656e7420706174, 0x6822293b652e7365, 0x7428722c742e6279, 0x746573297d66756e, - 0x6374696f6e204c28, 0x65297b6c65742074, 0x3d6e6577204d6170, 0x3b666f72286c6574, 0x2072206f66206d28, - 0x6529294628742c72, 0x293b72657475726e, 0x20747d7661722078, 0x3d7b70726f746f63, 0x6f6c3a5b315d2c61, - 0x70693a5b315d7d3b, 0x66756e6374696f6e, 0x204428652c74297b, 0x6c657420723d7b70, 0x726f746f636f6c3a, + 0x6374696f6e204428, 0x65297b6c65742074, 0x3d6e6577204d6170, 0x3b666f72286c6574, 0x2072206f66206228, + 0x6529294c28742c72, 0x293b72657475726e, 0x20747d766172206b, 0x3d7b70726f746f63, 0x6f6c3a5b315d2c61, + 0x70693a5b315d7d3b, 0x66756e6374696f6e, 0x204828652c74297b, 0x6c657420723d7b70, 0x726f746f636f6c3a, 0x652e70726f746f63, 0x6f6c2c6170693a65, 0x2e6170693f3f317d, 0x3b666f72286c6574, 0x5b6e2c6f5d6f6620, - 0x4f626a6563742e65, 0x6e74726965732872, 0x29297b6c65742061, 0x3d743f2e5b6e5d3f, 0x3f785b6e5d3b6966, + 0x4f626a6563742e65, 0x6e74726965732872, 0x29297b6c65742061, 0x3d743f2e5b6e5d3f, 0x3f6b5b6e5d3b6966, 0x2821612e696e636c, 0x75646573286f2929, 0x7468726f77206e65, 0x77204572726f7228, 0x60496e636f6d7061, 0x7469626c65205765, 0x6246726f6e742064, 0x6576656c6f706572, 0x20656e7669726f6e, 0x6d656e743a20247b, 0x6e7d207665727369, 0x6f6e20247b6f7d2c, 0x2074686973206170, 0x706c69636174696f, 0x6e20737570706f72, 0x747320247b612e6a, 0x6f696e28222c2022, 0x297d60297d7d6675, 0x6e6374696f6e2052, 0x28652c74297b6966, - 0x28747970656f6620, 0x652e656e74727921, 0x3d22737472696e67, 0x227c7c216628652e, 0x656e747279297c7c, + 0x28747970656f6620, 0x652e656e74727921, 0x3d22737472696e67, 0x227c7c217028652e, 0x656e747279297c7c, 0x21742e6861732865, 0x2e656e7472792929, 0x7468726f77206e65, 0x77204572726f7228, 0x22556e737570706f, 0x7274656420576562, 0x46726f6e74206465, 0x76656c6f70657220, 0x656e7669726f6e6d, 0x656e743a20696e76, - 0x616c696420656e74, 0x727922297d617379, 0x6e632066756e6374, 0x696f6e204528652c, 0x742c723d78297b61, - 0x7761697420772865, 0x2c74293b6c657420, 0x6e3d4c2861776169, 0x742068286529293b, 0x696628216e2e6861, + 0x616c696420656e74, 0x727922297d617379, 0x6e632066756e6374, 0x696f6e205328652c, 0x742c723d6b297b61, + 0x7761697420772865, 0x2c74293b6c657420, 0x6e3d442861776169, 0x742079286529293b, 0x696628216e2e6861, 0x732822656e766972, 0x6f6e6d656e742e6a, 0x736f6e2229297468, 0x726f77206e657720, 0x4572726f72282245, 0x6e7669726f6e6d65, 0x6e74206172636869, 0x766520686173206e, 0x6f20656e7669726f, 0x6e6d656e742e6a73, - 0x6f6e22293b6c6574, 0x206f3d4a534f4e2e, 0x7061727365284e2e, 0x6465636f6465286e, 0x2e6765742822656e, - 0x7669726f6e6d656e, 0x742e6a736f6e2229, 0x29293b7265747572, 0x6e2044286f2c7229, 0x2c52286f2c6e292c, - 0x7b66696c65733a6e, 0x2c6d616e69666573, 0x743a6f7d7d766172, 0x206b3d222e776562, 0x66726f6e742f6465, + 0x6f6e22293b6c6574, 0x206f3d4a534f4e2e, 0x706172736528452e, 0x6465636f6465286e, 0x2e6765742822656e, + 0x7669726f6e6d656e, 0x742e6a736f6e2229, 0x29293b7265747572, 0x6e2048286f2c7229, 0x2c52286f2c6e292c, + 0x7b66696c65733a6e, 0x2c6d616e69666573, 0x743a6f7d7d766172, 0x207a3d222e776562, 0x66726f6e742f6465, 0x76656c6f7065722f, 0x656e7669726f6e6d, 0x656e742e74677a22, 0x3b6173796e632066, 0x756e6374696f6e20, - 0x5728652c742c7229, 0x7b7472797b6c6574, 0x206e3d6177616974, 0x206228652c6b293b, 0x72657475726e2061, - 0x776169742045286e, 0x2c742c72292c6e7d, 0x63617463687b7265, 0x7475726e206e756c, 0x6c7d7d6173796e63, - 0x2066756e6374696f, 0x6e204828652c7429, 0x7b6c657420723d6e, 0x65772055524c2865, 0x2e656e7669726f6e, + 0x5728652c742c7229, 0x7b7472797b6c6574, 0x206e3d6177616974, 0x206728652c7a293b, 0x72657475726e2061, + 0x776169742053286e, 0x2c742c72292c6e7d, 0x63617463687b7265, 0x7475726e206e756c, 0x6c7d7d6173796e63, + 0x2066756e6374696f, 0x6e204d28652c7429, 0x7b6c657420723d6e, 0x65772055524c2865, 0x2e656e7669726f6e, 0x6d656e742c6c6f63, 0x6174696f6e2e6872, 0x6566293b69662872, 0x2e70726f746f636f, 0x6c213d3d22687474, 0x70733a222626722e, 0x6f726967696e213d, 0x3d6c6f636174696f, 0x6e2e6f726967696e, 0x297468726f77206e, 0x6577204572726f72, 0x282254686520656e, 0x7669726f6e6d656e, 0x74206d7573742075, 0x7365204854545053, 0x206f722074686520, 0x6170706c69636174, 0x696f6e206f726967, 0x696e22293b6c6574, 0x206e3d6177616974, 0x20742872293b6966, 0x28216e2e6f6b2974, 0x68726f77206e6577, 0x204572726f722860, 0x456e7669726f6e6d, 0x656e7420646f776e, 0x6c6f61643a204854, 0x545020247b6e2e73, 0x74617475737d6029, 0x3b72657475726e20, - 0x67286e297d66756e, 0x6374696f6e204d28, 0x65297b7265747572, 0x6e21657c7c216628, 0x652e617263686976, + 0x76286e297d66756e, 0x6374696f6e204a28, 0x65297b7265747572, 0x6e21657c7c217028, 0x652e617263686976, 0x65293f21313a5374, 0x72696e6728652e69, 0x6e74656772697479, 0x292e737461727473, 0x5769746828227368, - 0x613531322d22297d, 0x66756e6374696f6e, 0x204a28652c74297b, 0x72657475726e204f, 0x626a6563742e6672, + 0x613531322d22297d, 0x66756e6374696f6e, 0x204728652c74297b, 0x72657475726e204f, 0x626a6563742e6672, 0x6f6d456e74726965, 0x73284f626a656374, 0x2e656e7472696573, 0x28652e7374617274, 0x6572733f3f7b7d29, - 0x2e6d617028285b72, 0x2c6e5d293d3e7b69, 0x6628214d286e2929, 0x7468726f77206e65, 0x77204572726f7228, + 0x2e6d617028285b72, 0x2c6e5d293d3e7b69, 0x6628214a286e2929, 0x7468726f77206e65, 0x77204572726f7228, 0x22496e76616c6964, 0x2070696e6e656420, 0x7374617274657220, 0x6d65746164617461, 0x22293b7265747572, 0x6e5b722c7b75726c, 0x3a6e65772055524c, 0x286e2e6172636869, 0x76652c74292e6872, 0x65662c696e746567, - 0x726974793a6e2e69, 0x6e74656772697479, 0x7d5d7d29297d6173, 0x796e632066756e63, 0x74696f6e20532865, - 0x2c742c723d666574, 0x63682c6e3d28293d, 0x3e7b7d297b6c6574, 0x206f3d6177616974, 0x205728652c742e69, - 0x6e74656772697479, 0x2c742e737570706f, 0x72746564293b6f3f, 0x6e28225573696e67, 0x2076657269666965, - 0x6420636163686564, 0x20646576656c6f70, 0x657220656e766972, 0x6f6e6d656e742229, 0x3a286e2822446f77, - 0x6e6c6f6164696e67, 0x20646576656c6f70, 0x657220656e766972, 0x6f6e6d656e742229, 0x2c6f3d6177616974, - 0x204828742c722929, 0x3b6c65747b66696c, 0x65733a612c6d616e, 0x69666573743a697d, 0x3d61776169742045, - 0x286f2c742e696e74, 0x6567726974792c74, 0x2e737570706f7274, 0x6564292c733d4a28, 0x692c6e6577205552, - 0x4c28742e656e7669, 0x726f6e6d656e742c, 0x6c6f636174696f6e, 0x2e6872656629293b, 0x72657475726e2061, - 0x7761697420642865, 0x2c222e7765626672, 0x6f6e742f64657665, 0x6c6f7065722f656e, 0x7669726f6e6d656e, - 0x74222c61292c6177, 0x616974206428652c, 0x6b2c6e6577204d61, 0x70285b5b226f7574, 0x707574222c6f5d5d, - 0x292c7b6469726563, 0x746f72793a21317d, 0x292c7b75726c3a60, 0x2f77656266726f6e, 0x742f646576656c6f, - 0x7065722f656e7669, 0x726f6e6d656e742f, 0x247b692e656e7472, 0x797d602c636f6e66, 0x69673a7b2e2e2e74, - 0x2c73746172746572, 0x733a737d7d7d6966, 0x28747970656f6620, 0x646f63756d656e74, 0x3c2275222626646f, - 0x63756d656e742e67, 0x6574456c656d656e, 0x7442794964282272, 0x657472792229297b, 0x6c657420653d646f, - 0x63756d656e742e67, 0x6574456c656d656e, 0x7442794964282273, 0x746174757322292c, 0x743d646f63756d65, - 0x6e742e676574456c, 0x656d656e74427949, 0x6428227265747279, 0x22292c723d617379, 0x6e6328293d3e7b74, - 0x2e68696464656e3d, 0x21303b7472797b61, 0x7761697420776562, 0x46726f6e742e7265, 0x6164793b6c657420, - 0x6e3d4a534f4e2e70, 0x6172736528617761, 0x6974206328776562, 0x46726f6e742c2263, 0x6f6e666967222929, - 0x2c7b75726c3a6f2c, 0x636f6e6669673a61, 0x7d3d617761697420, 0x532877656246726f, 0x6e742c6e2c666574, - 0x63682c733d3e7b65, 0x2e74657874436f6e, 0x74656e743d737d29, 0x3b61776169742861, 0x7761697420696d70, - 0x6f7274286f29292e, 0x6d6f756e74287b62, 0x72696467653a7765, 0x6246726f6e742c63, 0x6f6e6669673a612c, - 0x7465726d696e616c, 0x3a646f63756d656e, 0x742e676574456c65, 0x6d656e7442794964, 0x28227465726d696e, - 0x616c22292c707265, 0x766965773a646f63, 0x756d656e742e6765, 0x74456c656d656e74, 0x4279496428227072, - 0x657669657722297d, 0x292c652e74657874, 0x436f6e74656e743d, 0x22446576656c6f70, 0x657220656e766972, - 0x6f6e6d656e742072, 0x656164792e205479, 0x7065207766206865, 0x6c702e204167656e, 0x74733a20646f6373, - 0x2f444556454c4f50, 0x494e475f57495448, 0x5f4147454e54532e, 0x6d64202877662072, 0x756e7320696e204a, - 0x534f4e206f72204d, 0x4350207769746820, 0x77656266726f6e74, 0x2d6167656e74292e, 0x227d636174636828, - 0x6e297b652e746578, 0x74436f6e74656e74, 0x3d60426f6f747374, 0x726170206661696c, 0x65643a20247b6e2e, - 0x6d6573736167657d, 0x602c742e68696464, 0x656e3d21317d7d3b, 0x742e616464457665, 0x6e744c697374656e, - 0x65722822636c6963, 0x6b222c72292c7228, 0x297d0a0000000000 + 0x726974793a6e2e69, 0x6e74656772697479, 0x7d5d7d29297d7661, 0x7220543d222e7765, 0x6266726f6e742f64, + 0x6576656c6f706572, 0x2f656e7669726f6e, 0x6d656e74222c5f3d, 0x653d3e4172726179, 0x2e66726f6d286e65, + 0x772055696e743841, 0x727261792865292c, 0x743d3e742e746f53, 0x7472696e67283136, 0x292e706164537461, + 0x727428322c223022, 0x29292e6a6f696e28, 0x2222293b6173796e, 0x632066756e637469, 0x6f6e207128652c74, + 0x297b6c657420723b, 0x7472797b723d4a53, 0x4f4e2e7061727365, 0x28452e6465636f64, 0x6528617761697420, + 0x6328652c22646967, 0x65737473222c5429, 0x29297d6361746368, 0x7b72657475726e21, 0x317d6966284f626a, + 0x6563742e6b657973, 0x2872292e6c656e67, 0x7468213d3d742e73, 0x697a652972657475, 0x726e21313b666f72, + 0x286c65745b6e2c6f, 0x5d6f662074296966, 0x28725b6e5d213d3d, 0x5f28617761697420, 0x63727970746f2e73, + 0x7562746c652e6469, 0x6765737428225348, 0x412d323536222c6f, 0x2929297265747572, 0x6e21313b72657475, + 0x726e21307d617379, 0x6e632066756e6374, 0x696f6e204928652c, 0x742c723d66657463, 0x682c6e3d28293d3e, + 0x7b7d297b6c657420, 0x6f3d617761697420, 0x5728652c742e696e, 0x746567726974792c, 0x742e737570706f72, + 0x746564292c613d6f, 0x213d3d6e756c6c3b, 0x613f6e2822557369, 0x6e67207665726966, 0x6965642063616368, + 0x656420646576656c, 0x6f70657220656e76, 0x69726f6e6d656e74, 0x22293a286e282244, 0x6f776e6c6f616469, + 0x6e6720646576656c, 0x6f70657220656e76, 0x69726f6e6d656e74, 0x22292c6f3d617761, 0x6974204d28742c72, + 0x29293b6c65747b66, 0x696c65733a692c6d, 0x616e69666573743a, 0x737d3d6177616974, 0x2053286f2c742e69, + 0x6e74656772697479, 0x2c742e737570706f, 0x72746564292c6c3d, 0x4728732c6e657720, 0x55524c28742e656e, + 0x7669726f6e6d656e, 0x742c6c6f63617469, 0x6f6e2e6872656629, 0x293b72657475726e, 0x2821617c7c216177, + 0x616974207128652c, 0x6929292626286177, 0x616974206628652c, 0x542c69292c617c7c, 0x6177616974206628, + 0x652c7a2c6e657720, 0x4d6170285b5b226f, 0x7574707574222c6f, 0x5d5d292c7b646972, 0x6563746f72793a21, + 0x317d29292c7b7572, 0x6c3a602f77656266, 0x726f6e742f646576, 0x656c6f7065722f65, 0x6e7669726f6e6d65, + 0x6e742f247b732e65, 0x6e7472797d602c63, 0x6f6e6669673a7b2e, 0x2e2e742c73746172, 0x746572733a6c7d7d, + 0x7d69662874797065, 0x6f6620646f63756d, 0x656e743c22752226, 0x26646f63756d656e, 0x742e676574456c65, + 0x6d656e7442794964, 0x2822726574727922, 0x29297b6c65742065, 0x3d646f63756d656e, 0x742e676574456c65, + 0x6d656e7442794964, 0x2822737461747573, 0x22292c743d646f63, 0x756d656e742e6765, 0x74456c656d656e74, + 0x4279496428227265, 0x74727922292c723d, 0x6173796e6328293d, 0x3e7b742e68696464, 0x656e3d21303b7472, + 0x797b617761697420, 0x77656246726f6e74, 0x2e72656164793b6c, 0x6574206e3d4a534f, 0x4e2e706172736528, + 0x6177616974206328, 0x77656246726f6e74, 0x2c22636f6e666967, 0x2229292c7b75726c, 0x3a6f2c636f6e6669, + 0x673a617d3d617761, 0x6974204928776562, 0x46726f6e742c6e2c, 0x66657463682c733d, 0x3e7b652e74657874, + 0x436f6e74656e743d, 0x737d293b61776169, 0x7428617761697420, 0x696d706f7274286f, 0x29292e6d6f756e74, + 0x287b627269646765, 0x3a77656246726f6e, 0x742c636f6e666967, 0x3a612c7465726d69, 0x6e616c3a646f6375, + 0x6d656e742e676574, 0x456c656d656e7442, 0x7949642822746572, 0x6d696e616c22292c, 0x707265766965773a, + 0x646f63756d656e74, 0x2e676574456c656d, 0x656e744279496428, 0x2270726576696577, 0x22297d292c652e74, + 0x657874436f6e7465, 0x6e743d2244657665, 0x6c6f70657220656e, 0x7669726f6e6d656e, 0x742072656164792e, + 0x2054797065207766, 0x2068656c702e2041, 0x67656e74733a2064, 0x6f63732f44455645, 0x4c4f50494e475f57, + 0x4954485f4147454e, 0x54532e6d64202877, 0x662072756e732069, 0x6e204a534f4e206f, 0x72204d4350207769, + 0x7468207765626672, 0x6f6e742d6167656e, 0x74292e227d636174, 0x6368286e297b652e, 0x74657874436f6e74, + 0x656e743d60426f6f, 0x7473747261702066, 0x61696c65643a2024, 0x7b6e2e6d65737361, 0x67657d602c742e68, + 0x696464656e3d2131, 0x7d7d3b742e616464, 0x4576656e744c6973, 0x74656e6572282263, 0x6c69636b222c7229, + 0x2c7228297d0a0000 }; }; diff --git a/include/webfront/system/DeveloperStore.hpp b/include/webfront/system/DeveloperStore.hpp index 29512a7b..31dfbac6 100644 --- a/include/webfront/system/DeveloperStore.hpp +++ b/include/webfront/system/DeveloperStore.hpp @@ -4,8 +4,10 @@ #include "DeveloperFiles.hpp" #include "DeveloperJournal.hpp" #include "../utils/Json.hpp" +#include "../utils/Sha256.hpp" #include +#include #include #include #include @@ -17,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -79,6 +82,18 @@ class Store { return {text.begin(), text.end()}; } + /// The SHA-256 of every file below a project directory, as a JSON object from its '/'-separated path below that + /// directory to its lowercase hexadecimal digest. It walks the entries list() names, skipping links: a published + /// directory holds none, and a link that would make the walk loop or leave the project is simply absent, which a + /// caller comparing with the files it expects sees as a difference. A missing directory throws like list(). + std::vector digests(std::string directory) const { + std::scoped_lock lock(mutex); + std::string text = "{"; + digestInto(text, directory, ""); + text += "}"; + return {text.begin(), text.end()}; + } + /// The entries list() names, as a JSON array of {"name", "type", "size", "modified"} sorted like it: the name has /// no final '/', "type" is "directory" or "file" (a link inside the project reads as its target), "size" is in /// bytes (0 for a directory) and "modified" in milliseconds since the Unix epoch, both 0 when they cannot be read. @@ -242,6 +257,31 @@ class Store { } private: + // Appends to `text` the "path":"digest" members of digests() for `directory`, each path prefixed by `prefix`. + void digestInto(std::string& text, const std::string& directory, const std::string& prefix) const { + for (const auto& listed : listedNames(directory)) { + const bool isDirectory = listed.ends_with('/'); + const auto name = isDirectory ? listed.substr(0, listed.size() - 1) : listed; + const auto path = directory.empty() ? name : directory + "/" + name; + const auto source = projectPath(root, path); + std::error_code error; + if (std::filesystem::is_symlink(source, error) || error) + continue; + if (isDirectory) { + digestInto(text, path, prefix + name + "/"); + continue; + } + auto file = openForReading(source, path, 0); + utils::Sha256 hasher; + std::array buffer{}; + while (file.read(buffer.data(), buffer.size()) || file.gcount() > 0) + hasher.update(std::as_bytes(std::span(buffer.data(), static_cast(file.gcount())))); + if (file.bad()) + throw std::runtime_error("Cannot read project file: " + path); + text += (text.size() > 1 ? "," : "") + json::quote(prefix + name) + ":\"" + utils::Sha256::hex(hasher.finish()) + "\""; + } + } + // The sorted names of list(): directories end with '/'. std::vector listedNames(const std::string& directory) const { const auto source = directory.empty() ? root : projectPath(root, directory); diff --git a/include/webfront/utils/Sha256.hpp b/include/webfront/utils/Sha256.hpp new file mode 100644 index 00000000..a1849920 --- /dev/null +++ b/include/webfront/utils/Sha256.hpp @@ -0,0 +1,116 @@ +/// @brief SHA-256 (FIPS 180-4), computed incrementally: a file is digested block by block, without loading it whole. +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace webfront::utils { + +class Sha256 { +public: + using Digest = std::array; + + /// Adds `bytes` to the message. + void update(std::span bytes) { + length += bytes.size(); + for (const auto byte : bytes) { + block[filled++] = static_cast(byte); + if (filled == block.size()) { + compress(); + filled = 0; + } + } + } + void update(std::string_view text) { + update(std::as_bytes(std::span(text.data(), text.size()))); + } + + /// The digest of everything added. The object is then spent. + Digest finish() { + const auto bits = static_cast(length) * 8; + block[filled++] = 0x80; + if (filled > 56) { + while (filled < block.size()) + block[filled++] = 0; + compress(); + filled = 0; + } + while (filled < 56) + block[filled++] = 0; + for (int shift = 56; shift >= 0; shift -= 8) + block[filled++] = static_cast((bits >> shift) & 255); + compress(); + Digest digest{}; + for (std::size_t i = 0; i < state.size(); ++i) + for (std::size_t j = 0; j < 4; ++j) + digest[i * 4 + j] = static_cast((state[i] >> (24 - 8 * j)) & 255); + return digest; + } + + /// Lowercase hexadecimal of a digest. + static std::string hex(const Digest& digest) { + static constexpr std::string_view digits = "0123456789abcdef"; + std::string text; + text.reserve(digest.size() * 2); + for (const auto byte : digest) { + text += digits[byte >> 4]; + text += digits[byte & 15]; + } + return text; + } + +private: + void compress() { + static constexpr std::array constants{ + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, + 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, + 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, + 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, + 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; + std::array words{}; + for (std::size_t i = 0; i < 16; ++i) + words[i] = (std::uint32_t{block[i * 4]} << 24) | (std::uint32_t{block[i * 4 + 1]} << 16) | (std::uint32_t{block[i * 4 + 2]} << 8) + | std::uint32_t{block[i * 4 + 3]}; + for (std::size_t i = 16; i < 64; ++i) { + const auto x = words[i - 15], y = words[i - 2]; + words[i] = words[i - 16] + (std::rotr(x, 7) ^ std::rotr(x, 18) ^ (x >> 3)) + words[i - 7] + (std::rotr(y, 17) ^ std::rotr(y, 19) ^ (y >> 10)); + } + auto [a, b, c, d, e, f, g, h] = state; + for (std::size_t i = 0; i < 64; ++i) { + const auto first = h + (std::rotr(e, 6) ^ std::rotr(e, 11) ^ std::rotr(e, 25)) + ((e & f) ^ (~e & g)) + constants[i] + words[i]; + const auto second = (std::rotr(a, 2) ^ std::rotr(a, 13) ^ std::rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c)); + h = g; + g = f; + f = e; + e = d + first; + d = c; + c = b; + b = a; + a = first + second; + } + const std::array values{a, b, c, d, e, f, g, h}; + for (std::size_t i = 0; i < state.size(); ++i) + state[i] += values[i]; + } + + std::array state{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; + std::array block{}; + std::size_t filled = 0; + std::uint64_t length = 0; +}; + +/// The 32 raw bytes of the SHA-256 of `input`. +inline std::string sha256(std::string_view input) { + Sha256 hasher; + hasher.update(input); + const auto digest = hasher.finish(); + return {reinterpret_cast(digest.data()), digest.size()}; +} + +} // namespace webfront::utils diff --git a/js/core/bootstrap/bootstrap.bundle.mjs b/js/core/bootstrap/bootstrap.bundle.mjs index 496ad4f1..df121e31 100644 --- a/js/core/bootstrap/bootstrap.bundle.mjs +++ b/js/core/bootstrap/bootstrap.bundle.mjs @@ -1 +1 @@ -var l=new TextDecoder;async function w(e,t){let r=String(t??"").split(/\s+/).filter(a=>a.startsWith("sha512-")).map(a=>a.split("?")[0]);if(r.length===0)throw new Error("The package has no SHA-512 integrity");if(!globalThis.crypto?.subtle)throw new Error("Integrity checks need a secure context (https or localhost)");let n=new Uint8Array(await crypto.subtle.digest("SHA-512",e)),o="sha512-"+btoa(Array.from(n,a=>String.fromCharCode(a)).join(""));if(!r.includes(o))throw new Error(`Integrity check failed: expected ${r[0]}, got ${o}`);return o}async function h(e,t=268435456){let r=new Blob([e]).stream().pipeThrough(new DecompressionStream("gzip")).getReader(),n=[],o=0;for(let a=await r.read();!a.done;a=await r.read()){if(o+=a.value.length,o>t)throw await r.cancel(),new Error(`The tarball unpacks to more than ${t} bytes`);n.push(a.value)}return new Uint8Array(await new Blob(n).arrayBuffer())}function u(e,t,r){let n=e.subarray(t,t+r),o=n.indexOf(0);return l.decode(o<0?n:n.subarray(0,o))}function y(e,t,r){if(e[t]&128)return e.subarray(t+1,t+r).reduce((o,a)=>o*256+a,e[t]&127);let n=u(e,t,r).trim();if(!/^[0-7]*$/.test(n))throw new Error("Corrupt tar header");return n===""?0:parseInt(n,8)}function z(e){let t=0;for(let r=0;r<512;++r)t+=r>=148&&r<156?32:e[r];if(y(e,148,8)!==t)throw new Error("Corrupt tar header checksum")}function I(e){let t={},r=0;for(;r0)||r+o>e.length)throw new Error("Corrupt pax header");let a=l.decode(e.subarray(n+1,r+o-1)),i=a.indexOf("=");t[a.slice(0,i)]=a.slice(i+1),r+=o}return t}function B(e,t){let r=e.subarray(t,t+512);z(r);let n=y(r,124,12),o=t+512;if(o+n>e.length)throw new Error("Truncated tar archive");return{header:r,type:r[156]===0?"0":String.fromCharCode(r[156]),data:e.subarray(o,o+n),next:o+Math.ceil(n/512)*512}}function C(e,t){return t+512>e.length||e.subarray(t,t+512).every(r=>r===0)}function U(e){let t=u(e,0,100),r=u(e,257,5)==="ustar"?u(e,345,155):"";return r?`${r}/${t}`:t}var p={x:(e,t)=>({...e,pax:I(t).path}),L:(e,t)=>({...e,long:u(t,0,t.length)}),g:e=>e};function m(e){let t=[],r={};for(let n=0;!C(e,n);){let{header:o,type:a,data:i,next:s}=B(e,n);if(n=s,Object.hasOwn(p,a)){r=p[a](r,i);continue}(a==="0"||a==="7")&&t.push({path:r.pax??r.long??U(o),bytes:i}),r={}}return t}var c=(e,t,...r)=>e.cppFunction(`webfront.developer.${t}`)(...r);async function b(e,t){let r=[],n=0;for(;;){let o=await c(e,"read",t,n);if(r.push(o),n+=o.length,o.length<262144)break;if(n>256*1024*1024)throw new Error("Project file is too large")}return new Uint8Array(await new Blob(r).arrayBuffer())}async function $(e,{id:t,path:r,bytes:n},o){for(let a=0;a{}),s}}async function P(e,t){let r=[],n=0;for(;;){let{done:o,value:a}=await e.read();if(o)return r;if(n+=a.length,n>t)throw new Error("Compressed download exceeds its byte limit");r.push(a)}}async function O(e,t){try{return await P(e,t)}catch(r){throw await e.cancel().catch(()=>{}),r}finally{e.releaseLock()}}async function g(e,t=64*1024*1024){if(!e.ok)throw new Error(`Download: HTTP ${e.status}`);if(Number(e.headers.get("content-length"))>t)throw new Error("Compressed download exceeds its byte limit");return new Uint8Array(await new Blob(await O(e.body.getReader(),t)).arrayBuffer())}var j=/[\\\0:*?"<>|]/,A=e=>e.split("/").some(t=>!t||t==="."||t==="..");function v(e){return typeof e!="string"||!e||j.test(e)?!0:A(e)}var f=e=>!v(e),N=new TextDecoder;function F(e,t){if(!t.path.startsWith("package/"))throw new Error("Invalid environment archive root");let r=t.path.slice(8);if(!f(r)||e.has(r))throw new Error("Invalid or duplicate environment path");e.set(r,t.bytes)}function L(e){let t=new Map;for(let r of m(e))F(t,r);return t}var x={protocol:[1],api:[1]};function D(e,t){let r={protocol:e.protocol,api:e.api??1};for(let[n,o]of Object.entries(r)){let a=t?.[n]??x[n];if(!a.includes(o))throw new Error(`Incompatible WebFront developer environment: ${n} version ${o}, this application supports ${a.join(", ")}`)}}function R(e,t){if(typeof e.entry!="string"||!f(e.entry)||!t.has(e.entry))throw new Error("Unsupported WebFront developer environment: invalid entry")}async function E(e,t,r=x){await w(e,t);let n=L(await h(e));if(!n.has("environment.json"))throw new Error("Environment archive has no environment.json");let o=JSON.parse(N.decode(n.get("environment.json")));return D(o,r),R(o,n),{files:n,manifest:o}}var k=".webfront/developer/environment.tgz";async function W(e,t,r){try{let n=await b(e,k);return await E(n,t,r),n}catch{return null}}async function H(e,t){let r=new URL(e.environment,location.href);if(r.protocol!=="https:"&&r.origin!==location.origin)throw new Error("The environment must use HTTPS or the application origin");let n=await t(r);if(!n.ok)throw new Error(`Environment download: HTTP ${n.status}`);return g(n)}function M(e){return!e||!f(e.archive)?!1:String(e.integrity).startsWith("sha512-")}function J(e,t){return Object.fromEntries(Object.entries(e.starters??{}).map(([r,n])=>{if(!M(n))throw new Error("Invalid pinned starter metadata");return[r,{url:new URL(n.archive,t).href,integrity:n.integrity}]}))}async function S(e,t,r=fetch,n=()=>{}){let o=await W(e,t.integrity,t.supported);o?n("Using verified cached developer environment"):(n("Downloading developer environment"),o=await H(t,r));let{files:a,manifest:i}=await E(o,t.integrity,t.supported),s=J(i,new URL(t.environment,location.href));return await d(e,".webfront/developer/environment",a),await d(e,k,new Map([["output",o]]),{directory:!1}),{url:`/webfront/developer/environment/${i.entry}`,config:{...t,starters:s}}}if(typeof document<"u"&&document.getElementById("retry")){let e=document.getElementById("status"),t=document.getElementById("retry"),r=async()=>{t.hidden=!0;try{await webFront.ready;let n=JSON.parse(await c(webFront,"config")),{url:o,config:a}=await S(webFront,n,fetch,s=>{e.textContent=s});await(await import(o)).mount({bridge:webFront,config:a,terminal:document.getElementById("terminal"),preview:document.getElementById("preview")}),e.textContent="Developer environment ready. Type wf help. Agents: docs/DEVELOPING_WITH_AGENTS.md (wf runs in JSON or MCP with webfront-agent)."}catch(n){e.textContent=`Bootstrap failed: ${n.message}`,t.hidden=!1}};t.addEventListener("click",r),r()} +var d=new TextDecoder;async function w(e,t){let r=String(t??"").split(/\s+/).filter(a=>a.startsWith("sha512-")).map(a=>a.split("?")[0]);if(r.length===0)throw new Error("The package has no SHA-512 integrity");if(!globalThis.crypto?.subtle)throw new Error("Integrity checks need a secure context (https or localhost)");let n=new Uint8Array(await crypto.subtle.digest("SHA-512",e)),o="sha512-"+btoa(Array.from(n,a=>String.fromCharCode(a)).join(""));if(!r.includes(o))throw new Error(`Integrity check failed: expected ${r[0]}, got ${o}`);return o}async function y(e,t=268435456){let r=new Blob([e]).stream().pipeThrough(new DecompressionStream("gzip")).getReader(),n=[],o=0;for(let a=await r.read();!a.done;a=await r.read()){if(o+=a.value.length,o>t)throw await r.cancel(),new Error(`The tarball unpacks to more than ${t} bytes`);n.push(a.value)}return new Uint8Array(await new Blob(n).arrayBuffer())}function u(e,t,r){let n=e.subarray(t,t+r),o=n.indexOf(0);return d.decode(o<0?n:n.subarray(0,o))}function m(e,t,r){if(e[t]&128)return e.subarray(t+1,t+r).reduce((o,a)=>o*256+a,e[t]&127);let n=u(e,t,r).trim();if(!/^[0-7]*$/.test(n))throw new Error("Corrupt tar header");return n===""?0:parseInt(n,8)}function B(e){let t=0;for(let r=0;r<512;++r)t+=r>=148&&r<156?32:e[r];if(m(e,148,8)!==t)throw new Error("Corrupt tar header checksum")}function C(e){let t={},r=0;for(;r0)||r+o>e.length)throw new Error("Corrupt pax header");let a=d.decode(e.subarray(n+1,r+o-1)),i=a.indexOf("=");t[a.slice(0,i)]=a.slice(i+1),r+=o}return t}function U(e,t){let r=e.subarray(t,t+512);B(r);let n=m(r,124,12),o=t+512;if(o+n>e.length)throw new Error("Truncated tar archive");return{header:r,type:r[156]===0?"0":String.fromCharCode(r[156]),data:e.subarray(o,o+n),next:o+Math.ceil(n/512)*512}}function A(e,t){return t+512>e.length||e.subarray(t,t+512).every(r=>r===0)}function O(e){let t=u(e,0,100),r=u(e,257,5)==="ustar"?u(e,345,155):"";return r?`${r}/${t}`:t}var h={x:(e,t)=>({...e,pax:C(t).path}),L:(e,t)=>({...e,long:u(t,0,t.length)}),g:e=>e};function b(e){let t=[],r={};for(let n=0;!A(e,n);){let{header:o,type:a,data:i,next:s}=U(e,n);if(n=s,Object.hasOwn(h,a)){r=h[a](r,i);continue}(a==="0"||a==="7")&&t.push({path:r.pax??r.long??O(o),bytes:i}),r={}}return t}var c=(e,t,...r)=>e.cppFunction(`webfront.developer.${t}`)(...r);async function g(e,t){let r=[],n=0;for(;;){let o=await c(e,"read",t,n);if(r.push(o),n+=o.length,o.length<262144)break;if(n>256*1024*1024)throw new Error("Project file is too large")}return new Uint8Array(await new Blob(r).arrayBuffer())}async function P(e,{id:t,path:r,bytes:n},o){for(let a=0;a{}),s}}async function $(e,t){let r=[],n=0;for(;;){let{done:o,value:a}=await e.read();if(o)return r;if(n+=a.length,n>t)throw new Error("Compressed download exceeds its byte limit");r.push(a)}}async function j(e,t){try{return await $(e,t)}catch(r){throw await e.cancel().catch(()=>{}),r}finally{e.releaseLock()}}async function v(e,t=64*1024*1024){if(!e.ok)throw new Error(`Download: HTTP ${e.status}`);if(Number(e.headers.get("content-length"))>t)throw new Error("Compressed download exceeds its byte limit");return new Uint8Array(await new Blob(await j(e.body.getReader(),t)).arrayBuffer())}var N=/[\\\0:*?"<>|]/,F=e=>e.split("/").some(t=>!t||t==="."||t==="..");function x(e){return typeof e!="string"||!e||N.test(e)?!0:F(e)}var p=e=>!x(e),E=new TextDecoder;function L(e,t){if(!t.path.startsWith("package/"))throw new Error("Invalid environment archive root");let r=t.path.slice(8);if(!p(r)||e.has(r))throw new Error("Invalid or duplicate environment path");e.set(r,t.bytes)}function D(e){let t=new Map;for(let r of b(e))L(t,r);return t}var k={protocol:[1],api:[1]};function H(e,t){let r={protocol:e.protocol,api:e.api??1};for(let[n,o]of Object.entries(r)){let a=t?.[n]??k[n];if(!a.includes(o))throw new Error(`Incompatible WebFront developer environment: ${n} version ${o}, this application supports ${a.join(", ")}`)}}function R(e,t){if(typeof e.entry!="string"||!p(e.entry)||!t.has(e.entry))throw new Error("Unsupported WebFront developer environment: invalid entry")}async function S(e,t,r=k){await w(e,t);let n=D(await y(e));if(!n.has("environment.json"))throw new Error("Environment archive has no environment.json");let o=JSON.parse(E.decode(n.get("environment.json")));return H(o,r),R(o,n),{files:n,manifest:o}}var z=".webfront/developer/environment.tgz";async function W(e,t,r){try{let n=await g(e,z);return await S(n,t,r),n}catch{return null}}async function M(e,t){let r=new URL(e.environment,location.href);if(r.protocol!=="https:"&&r.origin!==location.origin)throw new Error("The environment must use HTTPS or the application origin");let n=await t(r);if(!n.ok)throw new Error(`Environment download: HTTP ${n.status}`);return v(n)}function J(e){return!e||!p(e.archive)?!1:String(e.integrity).startsWith("sha512-")}function G(e,t){return Object.fromEntries(Object.entries(e.starters??{}).map(([r,n])=>{if(!J(n))throw new Error("Invalid pinned starter metadata");return[r,{url:new URL(n.archive,t).href,integrity:n.integrity}]}))}var T=".webfront/developer/environment",_=e=>Array.from(new Uint8Array(e),t=>t.toString(16).padStart(2,"0")).join("");async function q(e,t){let r;try{r=JSON.parse(E.decode(await c(e,"digests",T)))}catch{return!1}if(Object.keys(r).length!==t.size)return!1;for(let[n,o]of t)if(r[n]!==_(await crypto.subtle.digest("SHA-256",o)))return!1;return!0}async function I(e,t,r=fetch,n=()=>{}){let o=await W(e,t.integrity,t.supported),a=o!==null;a?n("Using verified cached developer environment"):(n("Downloading developer environment"),o=await M(t,r));let{files:i,manifest:s}=await S(o,t.integrity,t.supported),l=G(s,new URL(t.environment,location.href));return(!a||!await q(e,i))&&(await f(e,T,i),a||await f(e,z,new Map([["output",o]]),{directory:!1})),{url:`/webfront/developer/environment/${s.entry}`,config:{...t,starters:l}}}if(typeof document<"u"&&document.getElementById("retry")){let e=document.getElementById("status"),t=document.getElementById("retry"),r=async()=>{t.hidden=!0;try{await webFront.ready;let n=JSON.parse(await c(webFront,"config")),{url:o,config:a}=await I(webFront,n,fetch,s=>{e.textContent=s});await(await import(o)).mount({bridge:webFront,config:a,terminal:document.getElementById("terminal"),preview:document.getElementById("preview")}),e.textContent="Developer environment ready. Type wf help. Agents: docs/DEVELOPING_WITH_AGENTS.md (wf runs in JSON or MCP with webfront-agent)."}catch(n){e.textContent=`Bootstrap failed: ${n.message}`,t.hidden=!1}};t.addEventListener("click",r),r()} diff --git a/js/developer/environment/bootstrap.mjs b/js/developer/environment/bootstrap.mjs index 8e889cba..694bc4c9 100644 --- a/js/developer/environment/bootstrap.mjs +++ b/js/developer/environment/bootstrap.mjs @@ -1,5 +1,5 @@ import {gunzip, readTar, verifyIntegrity} from './tarball.mjs'; -import {readProject, publish, responseBytes} from './io.mjs'; +import {call, readProject, publish, responseBytes} from './io.mjs'; import {isUnsafe} from './paths.mjs'; const safePath = path => !isUnsafe(path); @@ -80,17 +80,37 @@ export function pinnedStarters(manifest, environmentUrl) { })); } +const publishedPath = '.webfront/developer/environment'; + +const hex = digest => Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join(''); + +// Whether the published environment is exactly `files`: the C++ digests what it serves, without sending it back. Any +// difference, a file of its own, a missing one, or a C++ without the callback, means it must be published again. +async function publishedMatches(bridge, files) { + let published; + try { published = JSON.parse(decoder.decode(await call(bridge, 'digests', publishedPath))); } + catch { return false; } + if (Object.keys(published).length !== files.size) return false; + for (const [path, bytes] of files) + if (published[path] !== hex(await crypto.subtle.digest('SHA-256', bytes))) return false; + return true; +} + export async function bootstrap(bridge, config, fetchImpl = fetch, progress = () => {}) { let bytes = await verifiedCache(bridge, config.integrity, config.supported); - if (bytes) progress('Using verified cached developer environment'); + const cached = bytes !== null; + if (cached) progress('Using verified cached developer environment'); else { progress('Downloading developer environment'); bytes = await downloadEnvironment(config, fetchImpl); } const {files, manifest} = await environmentFiles(bytes, config.integrity, config.supported); const starters = pinnedStarters(manifest, new URL(config.environment, location.href)); - // All files and the trusted archive are published only after validation succeeds. - await publish(bridge, '.webfront/developer/environment', files); - await publish(bridge, cachedPath, new Map([['output', bytes]]), {directory: false}); + // All files and the trusted archive are published only after validation succeeds. A launch from the verified cache + // whose published files all match it rewrites nothing: the served files are still those of the verified archive. + if (!cached || !await publishedMatches(bridge, files)) { + await publish(bridge, publishedPath, files); + if (!cached) await publish(bridge, cachedPath, new Map([['output', bytes]]), {directory: false}); + } return {url: `/webfront/developer/environment/${manifest.entry}`, config: {...config, starters}}; } diff --git a/js/developer/environment/bundles.json b/js/developer/environment/bundles.json index 35000629..5410b41c 100644 --- a/js/developer/environment/bundles.json +++ b/js/developer/environment/bundles.json @@ -12,7 +12,7 @@ "js/developer/environment/typecheck-snapshot.mjs": "ec2336781df84bf8a739d578437a0ff737478d3ca57fecf534f18e699487ec94", "js/developer/environment/typecheck-worker.mjs": "9f89315a8f7dcb2a19141c321a79aa47eabefdf60d05c60d39016663f1bafecd", "js/developer/environment/packages-entry.mjs": "e0b4acd4330b8b51eb0e695384c8df2b103a4be4f598ac83277838022f747e30", - "js/developer/environment/bootstrap.mjs": "c39265627ba42bf17d810a46e0104f35cb63fda1477a92fe3e83507d638f7343", + "js/developer/environment/bootstrap.mjs": "e0a42c5cdaede1f32b34d6c4df64f2cade3b0ef10d74284461b101f4b5cb6cb8", "js/developer/environment/bootstrap-entry.mjs": "a79cbe2ccdfecd615434f3beba7d46ee3f5fb1fa256eca3f0ec2d127e70dfef7", "js/developer/environment/packages.mjs": "37fe2a410e211264cfeaedbff3df50f7e62b02f8088130127d94f92081e2a5ad", "js/developer/environment/starter.mjs": "fd43db1861121577c48a0327436ce39cb9076d7cad0e9e273d40955747cc6a48", @@ -50,7 +50,7 @@ "js/developer/tools/distribution.mjs": "43dfdccc912693db46fd095fd78ae79325abab672d99f89fd569914ef3499cad", "js/developer/scripts/build.mjs": "a577f4396ab867096bc6fe6876e16e5220121ec8e8dd234fa1e4819d0a1173e1", "js/developer/webfront.project.json": "a31b7313bedc47bd437fe68decb23e0788dcaa9903c4c5c2a31841b296a40776", - "js/core/bootstrap/bootstrap.bundle.mjs": "bd78d70acb13a492e510ef0982e982f652ea1558f37071d0adb3e717611367e8", + "js/core/bootstrap/bootstrap.bundle.mjs": "65af2f899f7ff4752400148933284e702b0bcb3285a7bd89f2db31da99080698", "js/developer/environment/packages.bundle.mjs": "5598a590a9e859a4128075387a6c32963b2246ca120c2d7775851c780ad437f2", "include/webfront/system/DeveloperReleaseData.hpp": "494086e6d8aaf8e6900fa8e4a9f7725c92db7feb847b24c8360c5cc39f9ab7a1" } diff --git a/test/developer/specs/environment-specs.mjs b/test/developer/specs/environment-specs.mjs index ec43552e..7b6d0d05 100644 --- a/test/developer/specs/environment-specs.mjs +++ b/test/developer/specs/environment-specs.mjs @@ -190,10 +190,58 @@ describe('The environment', () => { transactions.delete(id); }, abort: ([id]) => { transactions.delete(id); }, + digests: async ([directory]) => { + const digests = {}; + for (const [path, bytes] of storage) + if (path.startsWith(directory + '/')) { + const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes)); + digests[path.slice(directory.length + 1)] = Array.from(digest, byte => byte.toString(16).padStart(2, '0')).join(''); + } + return new TextEncoder().encode(JSON.stringify(digests)); + }, }; - return {cppFunction: name => async (...args) => operations[name.split('.').pop()](args)}; + const bridge = {calls: [], cppFunction: name => async (...args) => { + bridge.calls.push(name.split('.').pop()); + const operation = operations[name.split('.').pop()]; + if (!operation) throw new Error('C++ function not found: ' + name); + return operation(args); + }}; + return bridge; } + it('a launch from the verified cache rewrites nothing while every published file matches it', async () => { + const archive = compress(tar(new Map([['package/environment.json', encode({protocol: 1, entry: 'runtime.mjs'})], ['package/runtime.mjs', encode('export function mount() {}')]]))); + const storage = new Map([['.webfront/developer/environment.tgz', archive]]); + const bridge = memoryBridge(storage); + const config = {environment: 'https://offline.test/environment.tgz', integrity: await integrity(archive)}; + const offline = async () => { throw new Error('Offline'); }; + await bootstrap(bridge, config, offline); // first launch: nothing is published yet + expect(bridge.calls).toContain('commit'); + const runtime = '.webfront/developer/environment/runtime.mjs'; + const relaunch = async () => { bridge.calls.length = 0; await bootstrap(bridge, config, offline); return bridge.calls; }; + expect(await relaunch()).not.toContain('begin'); + // A published file changed on disk, or one that the archive does not have: everything is published again. + storage.set(runtime, encode('tampered')); + expect(await relaunch()).toContain('commit'); + expect(new TextDecoder().decode(storage.get(runtime))).toBe('export function mount() {}'); + storage.set('.webfront/developer/environment/extra.mjs', encode('extra')); + expect(await relaunch()).toContain('commit'); + // The archive itself is verified and never published again from the cache. + expect(bridge.calls.filter(name => name === 'begin').length).toBe(1); + }); + + it('a C++ without the digests callback publishes the environment at every launch, as before', async () => { + const archive = compress(tar(new Map([['package/environment.json', encode({protocol: 1, entry: 'runtime.mjs'})], ['package/runtime.mjs', encode('')]]))); + const storage = new Map([['.webfront/developer/environment.tgz', archive]]); + const bridge = memoryBridge(storage); + const plain = {calls: bridge.calls, cppFunction: name => name.endsWith('.digests') ? async () => { throw new Error('C++ function not found'); } : bridge.cppFunction(name)}; + const config = {environment: 'https://offline.test/environment.tgz', integrity: await integrity(archive)}; + await bootstrap(plain, config, async () => { throw new Error('Offline'); }); + bridge.calls.length = 0; + await bootstrap(plain, config, async () => { throw new Error('Offline'); }); + expect(bridge.calls).toContain('commit'); + }); + it('a pinned cached environment bootstraps offline and failed refreshes preserve publications', async () => { const archive = compress(tar(new Map([['package/environment.json', encode({protocol: 1, entry: 'runtime.mjs'})], ['package/runtime.mjs', encode('export function mount() {}')]]))); const storage = new Map([['.webfront/developer/environment.tgz', archive]]); diff --git a/test/unit/DeveloperTests.cpp b/test/unit/DeveloperTests.cpp index e5cbaba3..9423c78d 100644 --- a/test/unit/DeveloperTests.cpp +++ b/test/unit/DeveloperTests.cpp @@ -875,6 +875,65 @@ const Scenario secondOpener{"A second opener of the project waits for a publicat }); }}; +const Scenario sha256Vectors{"SHA-256 matches the FIPS 180-4 vectors at every padding boundary, fed whole or in pieces", [] { + using utils::Sha256; + const auto digest = [](std::string_view text, std::size_t piece) { + Sha256 hasher; + for (std::size_t offset = 0; offset < text.size(); offset += piece) + hasher.update(text.substr(offset, piece)); + return Sha256::hex(hasher.finish()); + }; + const std::string million(1000000, 'a'); + for (const auto& [text, expected] : std::initializer_list>{ + {"", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}, + {"abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"}, + {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"}, + {std::string_view(million).substr(0, 55), "9f4390f8d30c2dd92ec9f095b65e2b9ae9b0a925a5258e241c9f1e910f734318"}, + {std::string_view(million).substr(0, 56), "b35439a4ac6f0948b6d6f9e3c6af0f5f590ce20f1bde7090ef7970686ec6738a"}, + {std::string_view(million).substr(0, 64), "ffe054fe7ae0cb6dc65c3af9b61d5209f439851db43d0ba5997337df154668eb"}, + {million, "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"}}) + for (const std::size_t piece : {std::size_t{1}, std::size_t{63}, std::size_t{4096}, std::max(text.size(), 1)}) { + auto scope = info("length ", text.size(), ", pieces of ", piece); + require(digest(text, piece) == expected); + } +}}; + +const Scenario projectDirectoryDigested{"A project directory is digested without leaving the project or following links", [] { + TemporaryDirectory project; + TemporaryDirectory outside; + developer::Store store{project.path()}; + publishFile(store, "ui/main.tsx", "main"); + publishFile(store, "ui/parts/a.ts", "a"); + publishFile(store, "ui/b.css", "b"); + const auto text = [&](std::string path) { + const auto bytes = store.digests(std::move(path)); + return std::string(bytes.begin(), bytes.end()); + }; + const std::string expected = R"({"b.css":"3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d",)" + R"("main.tsx":"0d6e4079e36703ebd37c00722f5891d28b0e2811dc114b129215123adcce3605",)" + R"("parts/a.ts":"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"})"; + then("every file below the directory, nested ones included, maps its relative path to its SHA-256", [&] { + require(text("ui") == expected); + require(text("ui/parts") == R"({"a.ts":"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"})"); + }); + then("a missing directory, a file or a path leaving the project is refused", [&] { + requireThrows([&] { store.digests("missing"); }, contains("Cannot list project directory")); + requireThrows([&] { store.digests("ui/main.tsx"); }, contains("Cannot list project directory")); + requireThrows([&] { store.digests("../outside"); }); + }); + given("links to a directory outside the project and back to the directory itself", [&] { + std::ofstream(outside.path() / "secret.txt") << "secret"; + std::error_code outError, loopError; + std::filesystem::create_directory_symlink(outside.path(), project.path() / "ui" / "escape", outError); + std::filesystem::create_directory_symlink(project.path() / "ui", project.path() / "ui" / "parts" / "loop", loopError); + if (!outError && !loopError) { + then("they are absent from the digests, which neither leave the project nor loop", [&] { + require(text("ui") == expected); + }); + } + }); +}}; + const Scenario projectDirectoryListed{"A project directory is listed without leaving the project", [] { TemporaryDirectory project; TemporaryDirectory outside; From f0bccf467414ca80a12dbe1812cf259bccd8e842 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:49:00 +0100 Subject: [PATCH 2/4] Report every link of a published directory as a difference (#570) digests() skipped links: with every file of the archive in place, an extra link to another file of the project made the publication look identical, so it was not replaced, and DeveloperFS followed the link and served that file under /webfront/developer/environment/. digests() now reports every entry of the directory, in path order: a regular file gets its digest, a directory is walked, and a link, never followed, maps to "link" (any other entry to "other"), which no digest equals. The bootstrap then publishes the environment again, and the directory replacement removes the link. The digests scenario now holds an extra file link, a link outside the project and a looping one, each reported. The bootstrap spec's in-memory store replaces a published directory as the C++ store does, and checks that the next launch removes the extra entry. --- include/webfront/system/DeveloperStore.hpp | 42 +++++++++++++++------- test/developer/specs/environment-specs.mjs | 6 +++- test/unit/DeveloperTests.cpp | 15 ++++---- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/include/webfront/system/DeveloperStore.hpp b/include/webfront/system/DeveloperStore.hpp index 31dfbac6..a12fa964 100644 --- a/include/webfront/system/DeveloperStore.hpp +++ b/include/webfront/system/DeveloperStore.hpp @@ -83,9 +83,9 @@ class Store { } /// The SHA-256 of every file below a project directory, as a JSON object from its '/'-separated path below that - /// directory to its lowercase hexadecimal digest. It walks the entries list() names, skipping links: a published - /// directory holds none, and a link that would make the walk loop or leave the project is simply absent, which a - /// caller comparing with the files it expects sees as a difference. A missing directory throws like list(). + /// directory to its lowercase hexadecimal digest, in path order. Every entry is there: a link, which is never + /// followed, maps to "link" and any other entry that is neither a file nor a directory to "other", so that a caller + /// comparing with the files it expects sees it as a difference. A missing directory, or one that is a link, throws. std::vector digests(std::string directory) const { std::scoped_lock lock(mutex); std::string text = "{"; @@ -258,27 +258,43 @@ class Store { private: // Appends to `text` the "path":"digest" members of digests() for `directory`, each path prefixed by `prefix`. + // Every entry counts, so that nothing the archive lacks can stay published unnoticed: a regular file gets its digest, a + // directory is walked, and anything else (a link, followed by nothing here, a special file, an entry whose type cannot + // be read) gets a value that no digest equals. void digestInto(std::string& text, const std::string& directory, const std::string& prefix) const { - for (const auto& listed : listedNames(directory)) { - const bool isDirectory = listed.ends_with('/'); - const auto name = isDirectory ? listed.substr(0, listed.size() - 1) : listed; - const auto path = directory.empty() ? name : directory + "/" + name; - const auto source = projectPath(root, path); - std::error_code error; - if (std::filesystem::is_symlink(source, error) || error) + const auto source = directory.empty() ? root : projectPath(root, directory); + std::error_code error; + if (!std::filesystem::is_directory(source, error) || std::filesystem::is_symlink(source, error)) + throw std::runtime_error("Cannot list project directory: " + directory); + std::vector sorted; + for (std::filesystem::directory_iterator entries(source, error); !error && entries != std::filesystem::directory_iterator{}; + entries.increment(error)) + sorted.push_back(*entries); + if (error) + throw std::runtime_error("Cannot list project directory: " + directory); + std::ranges::sort(sorted, {}, [](const auto& entry) { return entry.path().filename(); }); + for (const auto& entry : sorted) { + const auto name = entry.path().filename().generic_string(); + const auto path = directory.empty() ? name : directory + "/" + name; + const auto key = json::quote(prefix + name); + const auto status = entry.symlink_status(error); + if (error || (!std::filesystem::is_regular_file(status) && !std::filesystem::is_directory(status))) { + text += (text.size() > 1 ? "," : "") + key + ":\"" + (std::filesystem::is_symlink(status) ? "link" : "other") + "\""; + error = {}; continue; - if (isDirectory) { + } + if (std::filesystem::is_directory(status)) { digestInto(text, path, prefix + name + "/"); continue; } - auto file = openForReading(source, path, 0); + auto file = openForReading(entry.path(), path, 0); utils::Sha256 hasher; std::array buffer{}; while (file.read(buffer.data(), buffer.size()) || file.gcount() > 0) hasher.update(std::as_bytes(std::span(buffer.data(), static_cast(file.gcount())))); if (file.bad()) throw std::runtime_error("Cannot read project file: " + path); - text += (text.size() > 1 ? "," : "") + json::quote(prefix + name) + ":\"" + utils::Sha256::hex(hasher.finish()) + "\""; + text += (text.size() > 1 ? "," : "") + key + ":\"" + utils::Sha256::hex(hasher.finish()) + "\""; } } diff --git a/test/developer/specs/environment-specs.mjs b/test/developer/specs/environment-specs.mjs index 7b6d0d05..0b5aaf30 100644 --- a/test/developer/specs/environment-specs.mjs +++ b/test/developer/specs/environment-specs.mjs @@ -186,6 +186,8 @@ describe('The environment', () => { }, commit: ([id]) => { const {destination, directory, files} = transactions.get(id); + // A directory publication replaces the whole directory, as the C++ store does. + if (directory) for (const path of [...storage.keys()]) if (path.startsWith(destination + '/')) storage.delete(path); for (const [path, bytes] of files) storage.set(directory ? `${destination}/${path}` : destination, bytes); transactions.delete(id); }, @@ -224,8 +226,10 @@ describe('The environment', () => { storage.set(runtime, encode('tampered')); expect(await relaunch()).toContain('commit'); expect(new TextDecoder().decode(storage.get(runtime))).toBe('export function mount() {}'); - storage.set('.webfront/developer/environment/extra.mjs', encode('extra')); + // An entry the archive lacks (the C++ reports a link as "link") is removed by the next launch. + storage.set('.webfront/developer/environment/link.mjs', encode('served from elsewhere')); expect(await relaunch()).toContain('commit'); + expect(storage.has('.webfront/developer/environment/link.mjs')).toBe(false); // The archive itself is verified and never published again from the cache. expect(bridge.calls.filter(name => name === 'begin').length).toBe(1); }); diff --git a/test/unit/DeveloperTests.cpp b/test/unit/DeveloperTests.cpp index 9423c78d..3148417d 100644 --- a/test/unit/DeveloperTests.cpp +++ b/test/unit/DeveloperTests.cpp @@ -898,7 +898,7 @@ const Scenario sha256Vectors{"SHA-256 matches the FIPS 180-4 vectors at every pa } }}; -const Scenario projectDirectoryDigested{"A project directory is digested without leaving the project or following links", [] { +const Scenario projectDirectoryDigested{"A project directory is digested whole, its links marked and never followed", [] { TemporaryDirectory project; TemporaryDirectory outside; developer::Store store{project.path()}; @@ -921,14 +921,17 @@ const Scenario projectDirectoryDigested{"A project directory is digested without requireThrows([&] { store.digests("ui/main.tsx"); }, contains("Cannot list project directory")); requireThrows([&] { store.digests("../outside"); }); }); - given("links to a directory outside the project and back to the directory itself", [&] { + given("an extra link to a file of the project, a link outside it and one back to the directory itself", [&] { std::ofstream(outside.path() / "secret.txt") << "secret"; - std::error_code outError, loopError; + std::error_code fileError, outError, loopError; + std::filesystem::create_symlink(project.path() / "ui" / "main.tsx", project.path() / "ui" / "alias.txt", fileError); std::filesystem::create_directory_symlink(outside.path(), project.path() / "ui" / "escape", outError); std::filesystem::create_directory_symlink(project.path() / "ui", project.path() / "ui" / "parts" / "loop", loopError); - if (!outError && !loopError) { - then("they are absent from the digests, which neither leave the project nor loop", [&] { - require(text("ui") == expected); + if (!fileError && !outError && !loopError) { + then("each is a difference that no digest equals, and none is followed", [&] { + require(text("ui") == R"({"alias.txt":"link","b.css":"3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d",)" + R"("escape":"link","main.tsx":"0d6e4079e36703ebd37c00722f5891d28b0e2811dc114b129215123adcce3605",)" + R"("parts/a.ts":"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb","parts/loop":"link"})"); }); } }); From 4550b184d4d7403dabe2526de610320cd7f5f6da Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:24:30 +0100 Subject: [PATCH 3/4] Read each digested file with a heap buffer, outside the recursion (#570) digestInto() declared a 64 KiB array in its body, so every level of the recursion reserved it on the stack: a few tens of nested directories exceeded a 1 MiB Windows thread stack. fileDigest() now reads a file with a buffer on the heap, and the recursive frames keep none. The bootstrap spec also checks that a launch after the repair rewrites nothing, and the changelog entry leaves the mechanism to the guide. --- docs/CHANGELOG.md | 2 +- include/webfront/system/DeveloperStore.hpp | 23 +++++++++++++--------- test/developer/specs/environment-specs.mjs | 2 ++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d5fd056c..cefd805a 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -24,7 +24,7 @@ these entries stays in the GitHub releases and the Git history. ### Changed -- Launch the developer environment from its verified cache without rewriting it: the bootstrap compares the SHA-256 of the published files, which the new `webfront.developer.digests` callback computes in C++, with the verified archive, and publishes it again only when a file differs. A `wf` command of the tests starts in 1.6 s instead of 2.5 s (#570, `docs/DEVELOPER_ENVIRONMENT.md`). +- Launch the developer environment from its verified cache without rewriting it while its published files match the archive: a `wf` command of the tests starts in 1.6 s instead of 2.5 s (#570, `docs/DEVELOPER_ENVIRONMENT.md`). - Replace Catch2 with `test/unit/Bdd.hpp`, a single-header BDD framework: scenarios are `Scenario` objects whose `given`/`when`/`then` sections are lambdas, checked with `require`/`check`. The tests no longer download or compile Catch2; `tests` takes `--exact`, `--list`, `--junit` and the former filters, order and shard options (ADR-012, `docs/TEST_TIERS.md`). - Wait for asynchronous states in unit tests with `requireEventually`, `checkEventually`, `requireReady` and `checkReady` of `Bdd.hpp`, which report the delay and the time waited and replace nine local helpers; `WEBFRONT_TEST_TIMEOUT_FACTOR` lengthens the delays of a slow job (#582, `docs/TEST_TIERS.md`, ADR-012). - Keep automated browser windows visible (#544, #556, `docs/TEST_PERFORMANCE.md`). diff --git a/include/webfront/system/DeveloperStore.hpp b/include/webfront/system/DeveloperStore.hpp index a12fa964..d911d528 100644 --- a/include/webfront/system/DeveloperStore.hpp +++ b/include/webfront/system/DeveloperStore.hpp @@ -7,7 +7,6 @@ #include "../utils/Sha256.hpp" #include -#include #include #include #include @@ -287,17 +286,23 @@ class Store { digestInto(text, path, prefix + name + "/"); continue; } - auto file = openForReading(entry.path(), path, 0); - utils::Sha256 hasher; - std::array buffer{}; - while (file.read(buffer.data(), buffer.size()) || file.gcount() > 0) - hasher.update(std::as_bytes(std::span(buffer.data(), static_cast(file.gcount())))); - if (file.bad()) - throw std::runtime_error("Cannot read project file: " + path); - text += (text.size() > 1 ? "," : "") + key + ":\"" + utils::Sha256::hex(hasher.finish()) + "\""; + text += (text.size() > 1 ? "," : "") + key + ":\"" + fileDigest(entry.path(), path) + "\""; } } + // The SHA-256 of a project file, read in blocks of a buffer on the heap: the recursion of digestInto() keeps no + // buffer in its frames, however deep the directories. + static std::string fileDigest(const std::filesystem::path& source, const std::string& path) { + auto file = openForReading(source, path, 0); + utils::Sha256 hasher; + std::vector buffer(64 * 1024); + while (file.read(buffer.data(), static_cast(buffer.size())) || file.gcount() > 0) + hasher.update(std::as_bytes(std::span(buffer.data(), static_cast(file.gcount())))); + if (file.bad()) + throw std::runtime_error("Cannot read project file: " + path); + return utils::Sha256::hex(hasher.finish()); + } + // The sorted names of list(): directories end with '/'. std::vector listedNames(const std::string& directory) const { const auto source = directory.empty() ? root : projectPath(root, directory); diff --git a/test/developer/specs/environment-specs.mjs b/test/developer/specs/environment-specs.mjs index 0b5aaf30..795100dc 100644 --- a/test/developer/specs/environment-specs.mjs +++ b/test/developer/specs/environment-specs.mjs @@ -232,6 +232,8 @@ describe('The environment', () => { expect(storage.has('.webfront/developer/environment/link.mjs')).toBe(false); // The archive itself is verified and never published again from the cache. expect(bridge.calls.filter(name => name === 'begin').length).toBe(1); + // The repaired publication matches again: the next launch rewrites nothing. + expect(await relaunch()).not.toContain('begin'); }); it('a C++ without the digests callback publishes the environment at every launch, as before', async () => { From 86d2b9c996b2d1463c2cabf6f03f70f59c0ee25f Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:47:57 +0100 Subject: [PATCH 4/4] Compress whole SHA-256 blocks where they lie (#570) update() copied the message byte by byte into its block before compressing it. It now copies only a partial block and compresses the whole ones in place: 0.93 s instead of 1.03 s for the 14 MB of an environment in an unoptimized build with coverage, as the Linux job builds it. The FIPS vectors, fed in pieces of 1, 63 and 4096 bytes, and the RFC 4231 HMAC vectors pass. --- include/webfront/utils/Sha256.hpp | 39 +++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/include/webfront/utils/Sha256.hpp b/include/webfront/utils/Sha256.hpp index a1849920..e77ada21 100644 --- a/include/webfront/utils/Sha256.hpp +++ b/include/webfront/utils/Sha256.hpp @@ -1,10 +1,12 @@ /// @brief SHA-256 (FIPS 180-4), computed incrementally: a file is digested block by block, without loading it whole. #pragma once +#include #include #include #include #include +#include #include #include #include @@ -15,15 +17,27 @@ class Sha256 { public: using Digest = std::array; - /// Adds `bytes` to the message. + /// Adds `bytes` to the message. Whole blocks are compressed where they lie; only a partial one is copied. void update(std::span bytes) { length += bytes.size(); - for (const auto byte : bytes) { - block[filled++] = static_cast(byte); - if (filled == block.size()) { - compress(); - filled = 0; - } + const auto* data = reinterpret_cast(bytes.data()); + std::size_t size = bytes.size(); + if (filled != 0) { + const auto taken = std::min(size, block.size() - filled); + std::memcpy(block.data() + filled, data, taken); + filled += taken; + data += taken; + size -= taken; + if (filled < block.size()) + return; + compress(block.data()); + filled = 0; + } + for (; size >= block.size(); data += block.size(), size -= block.size()) + compress(data); + if (size != 0) { + std::memcpy(block.data(), data, size); + filled = size; } } void update(std::string_view text) { @@ -37,14 +51,14 @@ class Sha256 { if (filled > 56) { while (filled < block.size()) block[filled++] = 0; - compress(); + compress(block.data()); filled = 0; } while (filled < 56) block[filled++] = 0; for (int shift = 56; shift >= 0; shift -= 8) block[filled++] = static_cast((bits >> shift) & 255); - compress(); + compress(block.data()); Digest digest{}; for (std::size_t i = 0; i < state.size(); ++i) for (std::size_t j = 0; j < 4; ++j) @@ -65,7 +79,8 @@ class Sha256 { } private: - void compress() { + // Compresses the 64 bytes at `input` into the state. + void compress(const std::uint8_t* input) { static constexpr std::array constants{ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, @@ -75,8 +90,8 @@ class Sha256 { 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; std::array words{}; for (std::size_t i = 0; i < 16; ++i) - words[i] = (std::uint32_t{block[i * 4]} << 24) | (std::uint32_t{block[i * 4 + 1]} << 16) | (std::uint32_t{block[i * 4 + 2]} << 8) - | std::uint32_t{block[i * 4 + 3]}; + words[i] = (std::uint32_t{input[i * 4]} << 24) | (std::uint32_t{input[i * 4 + 1]} << 16) | (std::uint32_t{input[i * 4 + 2]} << 8) + | std::uint32_t{input[i * 4 + 3]}; for (std::size_t i = 16; i < 64; ++i) { const auto x = words[i - 15], y = words[i - 2]; words[i] = words[i - 16] + (std::rotr(x, 7) ^ std::rotr(x, 18) ^ (x >> 3)) + words[i - 7] + (std::rotr(y, 17) ^ std::rotr(y, 19) ^ (y >> 10));