From a54188bcc19850ae6c292c9b4850241ce8af5334 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc Date: Fri, 23 Oct 2026 17:35:55 +0200 Subject: [PATCH 1/3] Guard the minimal core with a normative list and a size budget, generate the favicon in pure CMake [#314] --- .gitattributes | 1 + AGENTS.md | 2 +- docs/CHANGELOG.md | 6 ++ docs/EMBEDDED_ASSETS.md | 16 ++- docs/adr/ADR-016-bootstrap-comme-socle.md | 8 +- include/webfront/system/FaviconData.hpp | 37 +++++++ include/webfront/system/IndexFS.hpp | 18 +--- js/core/EmbedBinaryAsset.cmake | 76 ++++++++++++++ js/core/WebFrontAssets.cmake | 24 +++++ js/core/favicon.ico | Bin 0 -> 766 bytes test/cmake/CheckMinimalCoreAssets.cmake | 96 ++++++++++++++++++ .../CheckMinimalCoreAssetsNegative.cmake | 59 +++++++++++ test/unit/DefaultFSTests.cpp | 5 + test/unit/IndexFSTests.cpp | 10 +- 14 files changed, 330 insertions(+), 28 deletions(-) create mode 100644 include/webfront/system/FaviconData.hpp create mode 100644 js/core/EmbedBinaryAsset.cmake create mode 100644 js/core/favicon.ico create mode 100644 test/cmake/CheckMinimalCoreAssets.cmake create mode 100644 test/cmake/CheckMinimalCoreAssetsNegative.cmake diff --git a/.gitattributes b/.gitattributes index 36f7436a..d760580b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -53,6 +53,7 @@ # re-read those sources, so Windows checkouts must preserve LF for every asset. js/core/WebFront.js text eol=lf js/core/bootstrap/* text eol=lf +js/core/favicon.ico binary include/webfront/system/WebFrontJsData.hpp text eol=lf js/core/fallback/index.html text eol=lf js/core/fallback/webfront-fallback.mjs text eol=lf diff --git a/AGENTS.md b/AGENTS.md index 7dfb7f47..d75b55bf 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -15,7 +15,7 @@ The README introduces WebFront through the Game of Life walkthrough, demonstrati - `include/webfront/`: public header-only library, included as ``. `WebFront.hpp` is the main orchestration API; `Commands.hpp` adds an application's C++ commands to the developer environment's terminal. - `include/webfront/http/`: HTTP and WebSocket protocol implementation. - `include/webfront/weblink/`: bridge message encoding and routing. -- `include/webfront/system/`: layered virtual filesystems and embedded web resources. +- `include/webfront/system/`: layered virtual filesystems and the embedded web resources, a closed list guarded by the `MinimalCoreAssets` CTest case (normative list and 150 KiB decoded budget in ADR-016; changing it means amending the ADR and `test/cmake/CheckMinimalCoreAssets.cmake` together). - `include/webfront/frontend/`: system-browser, CEF, optional Windows WebView2 and macOS WKWebView frontend adapters. - `examples/`: only Hello World (`hello-world/`) and Game of Life (`life/`, with console, developer and embedded stages). - `js/core/`: readable browser bridge, fallback and bootstrap loader compiled into `include/webfront/`, plus the embedding CMake modules; `js/developer/`: developer environment (its terminal and `wf`), build and vendoring tools. Production C++ remains header-only in `include/webfront/`. diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 5d67f27e..69619707 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -86,6 +86,12 @@ ### Changed +- Guard of the minimal core (#314): the `MinimalCoreAssets` CTest case lists the generated resource headers of + `include/webfront/` and fails on one outside the normative list of ADR-016 or when the decoded payloads exceed 150 KiB + (currently 35 766 bytes in 99 341 bytes of headers; `include/webfront/system` went from 2.7 MB in #303 to 188 KB). + The default favicon is the source `js/core/favicon.ico` rendered by `js/core/EmbedBinaryAsset.cmake`, pure CMake with + no Node.js, into `FaviconData.hpp` (`FaviconAssetFresh` checks it); it is no longer a hand-written gzip array in + `IndexFS.hpp` and is served unencoded. `IndexFS::WebFrontIco` stays an alias of the generated struct. - The browser integration suite `test/web/` runs through `wf test` (#311): `test/web/suite/` is a WebFront project (`jasmine-core`, `react`, `react-dom` from the local registry), `webtest` is the `webfront-wf` driver with the callbacks of the specs (`test/driver/WfDriver.hpp`), and the Signal Studio page is a JSX module built by esbuild-wasm. diff --git a/docs/EMBEDDED_ASSETS.md b/docs/EMBEDDED_ASSETS.md index 9df48544..a45ee87b 100644 --- a/docs/EMBEDDED_ASSETS.md +++ b/docs/EMBEDDED_ASSETS.md @@ -3,7 +3,7 @@ The C++ library embeds only what every application needs: the browser bridge (`WebFront.js`), the fallback page and module, and the bootstrap page and loader of the developer environment, with the pinned release data (`Developer*Data.hpp`). Each is generated from a readable source under `js/core/` -into a header under `include/webfront/system/`. Libraries such as React, Babel and Jasmine are not +(including the default favicon, `js/core/favicon.ico`) into a header under `include/webfront/system/`. Libraries such as React, Babel and Jasmine are not embedded: they are npm packages that `wf install` locks and downloads (see [DEVELOPER_ENVIRONMENT.md](DEVELOPER_ENVIRONMENT.md)), and the tests obtain them from a local registry. Normal C++ consumer builds use the checked-in headers and need neither Node.js nor npm. @@ -15,6 +15,17 @@ were removed. An application that served them replaces them with `wf install` of (`react`, `react-dom`, `@babel/standalone`, `jasmine-core`) and `wf embed`, which generates a header-only provider for the application's own build output. +## Guard + +The `MinimalCoreAssets` CTest case (`test/cmake/CheckMinimalCoreAssets.cmake`, pure CMake) lists every +resource header of `include/webfront/` (named `*Data.hpp` or declaring a `dataSize` payload) and fails on one +outside the normative list of [ADR-016](adr/ADR-016-bootstrap-comme-socle.md), or when the decoded payloads exceed +150 KiB. The budget counts decoded bytes (gzip streams are measured by their trailer), so it does not depend on the +encoding; the test also prints the encoded bytes and the size of the headers. Adding a resource means amending the ADR +and the list in the script together. `MinimalCoreAssetsRejectsUnlisted` proves the guard fails on an unlisted +header, a hidden payload and an oversized resource. `du -sh include/webfront/system` was 2.7 MB when the epic began +and is 188 KB now. + ## Maintenance Use Node.js 22.18 or newer (Node 24 in CI) and CMake 3.31 or newer. These tools are needed for @@ -34,6 +45,9 @@ intermediate files. `EmbeddedAssetGenerator` covers empty and binary inputs, wor line wrapping and a nonzero status for stale or missing files; a per-asset `*AssetFresh` test attributes each stale header. `.gitattributes` keeps sources and headers at LF. +The favicon is binary and small, so `js/core/EmbedBinaryAsset.cmake` renders it (identity encoded, same header format) +with CMake alone: no Node.js and no compressor. `FaviconAssetFresh` compares the checked-in `FaviconData.hpp` with it. + The renderer can pack gzip-compressed bytes (`encoding = "gzip"`, `dataSize` excluding the trailing word padding) with the vendored fflate `gzipSync` (level 9, mem 8, mtime 0, no filename, fixed OS byte), so the streams are identical across platforms. The current assets are identity encoded because they are diff --git a/docs/adr/ADR-016-bootstrap-comme-socle.md b/docs/adr/ADR-016-bootstrap-comme-socle.md index ea5862da..8b7306ff 100644 --- a/docs/adr/ADR-016-bootstrap-comme-socle.md +++ b/docs/adr/ADR-016-bootstrap-comme-socle.md @@ -5,6 +5,7 @@ | Version | Date | État | Auteur | Évolution | Commit | | --- | --- | --- | --- | --- | --- | | 1.0 | 2026-10-22 | Acceptée | Ambroise Leclerc | Décision initiale : environnement bootstrap comme architecture de base, liste normative du cœur, scripts autorisés, indépendance vis-à-vis de Node.js. | [404fed2](https://github.com/ambroise-leclerc/WebFront/commit/404fed2c2145d4faba8efee035e10522ff87a91f) | +| 1.1 | 2026-10-23 | Acceptée | Ambroise Leclerc | Garde-fou `MinimalCoreAssets` (#314) : la liste est vérifiée par CTest, budget de 150 Kio d'octets décodés ; le favicon devient `FaviconData`, généré par CMake pur depuis `js/core/favicon.ico`. | — | ## Statut et date @@ -43,15 +44,16 @@ téléchargée et vérifiée, tandis que le code source de WebFront redevient mi | `WebFrontJsData` | Bridge `WebFront.js` | | `FallbackIndexHtmlData` | Page de repli | | `FallbackModuleData` | Module de repli | -| favicon (`WebFrontIco`, servi par `IndexFS`) | Icône par défaut | +| `FaviconData` (alias `IndexFS::WebFrontIco`, source `js/core/favicon.ico`) | Icône par défaut | | `DeveloperIndexHtmlData` | Page du bootstrap développeur | | `DeveloperBootstrapData` | Chargeur bootstrap (`bootstrap.bundle.mjs`) | | `DeveloperReleaseData` | Release épinglée (URL et SHA-512) | Toute autre ressource web (bibliothèques, runner de test, pages de test, terminal, providers applicatifs) est produite par un script autorisé et livrée par l'archive d'environnement ou par un -paquet npm verrouillé par intégrité. Cette liste est normative : un test garde-fou (#314) la -compare aux fichiers de ressources de `include/webfront/system/` et impose un budget de taille ; +paquet npm verrouillé par intégrité. Cette liste est normative : le test garde-fou `MinimalCoreAssets` (#314) la +compare aux en-têtes de ressources de `include/webfront/` et impose un budget de 150 Kio d'octets décodés +(somme des `dataSize` après décodage gzip, indépendante de l'encodage) ; ajouter une ressource exige de modifier cette ADR (nouvelle version) et ce test dans le même changement. Le C++ header-only (filesystems, serveur, bridge, frontends) n'est pas concerné. diff --git a/include/webfront/system/FaviconData.hpp b/include/webfront/system/FaviconData.hpp new file mode 100644 index 00000000..4dba382d --- /dev/null +++ b/include/webfront/system/FaviconData.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include +#include + +namespace webfront::fs::generated { + +struct FaviconData { + static constexpr std::string_view encoding{}; + static constexpr std::size_t dataSize{766}; + static constexpr std::array data{ + 0x0000010001002020, 0x100001000400e802, 0x0000160000002800, 0x0000200000004000, 0x0000010004000000, + 0x0000000000000000, 0x0000000000001000, 0x0000000000003834, 0x3200e09b1200ead9, 0xb0006c676200ae9f, + 0x7500e6b24200524e, 0x4800e1a429009d7b, 0x2f00bfbab900857e, 0x6f00fbf8ef00b887, 0x2000866d3800e8c2, + 0x6d0063594600bb23, 0x0000000000000000, 0x0000000032bbb900, 0x0000000000000000, 0x00000000009b2000, + 0x0000000000000000, 0x000000000002a000, 0x000000006d8cc8d6, 0x0000000000036000, 0x006000d111717111, + 0x1d00000000000000, 0x00006c1777777777, 0x7110000000000000, 0x0006117777777777, 0x7777f00000000000, + 0x00f1777777777777, 0x77771f0000000000, 0x0017777171717177, 0x7777710000000000, 0x0117111717171717, + 0x1717171000000060, 0xf111711111111111, 0x1171111f00600000, 0xc111111111111111, 0x1111111100000600, + 0x1111111217be7e1b, 0xe71111116000000d, 0x111111eb7ebb111b, 0x21111111d000060d, 0x111111227b2b5112, + 0x211111118060000c, 0x111115b5eb72211b, 0xb2251111c0000668, 0x11111221b21eb11b, 0x2ee5111180600608, + 0x11117be5b711be1b, 0xe1111111c060066d, 0x1111eb1221112b1b, 0xe7111111d660066f, 0x1111be7be1117b5b, + 0xbbbbb111f0600666, 0x1111111111111111, 0x1111111166600666, 0xd717171717171711, 0x7171771d66600f66, + 0x6777777177717777, 0x1777717666606666, 0x6377777777777777, 0x7777773666f006ff, 0xf685575777575777, + 0x57775a6f6f6666f6, 0xff3a575555757555, 0x5555affff6f066ff, 0x3f3fa55555555557, 0x555af33636f6f6ff, + 0xf333334555555555, 0xea333333636faff3, 0xf3333333a445444a, 0x3a33333ff664b36f, 0xf333a3aaaaaaaaaa, + 0xaa3a3333f332b243, 0x363f333333333333, 0x33333fffaa2bbbb9, 0x9999999999999999, 0x999999999bbbc000, + 0x0003800000010000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, + 0x0000000000000002, 0x1000001310000005, 0x0000002418000008, 0x9000004890000040, 0x500000905f800000, + 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000800000008000, + 0x0001e00000070000 + }; +}; + +} // namespace webfront::fs::generated diff --git a/include/webfront/system/IndexFS.hpp b/include/webfront/system/IndexFS.hpp index 290d6b3b..9ab3c224 100644 --- a/include/webfront/system/IndexFS.hpp +++ b/include/webfront/system/IndexFS.hpp @@ -5,6 +5,7 @@ #include "FallbackIndexHtmlData.hpp" #include "FallbackModuleData.hpp" +#include "FaviconData.hpp" #include "FileSystem.hpp" #include "WebFrontJsData.hpp" @@ -24,22 +25,7 @@ class IndexFS { using IndexHtml = generated::FallbackIndexHtmlData; using FallbackModule = generated::FallbackModuleData; - struct WebFrontIco { - static constexpr std::string_view encoding{"gzip"}; - static constexpr size_t dataSize{510}; - static constexpr std::array data{ - 0x1f8b080000000000, 0x02ff8d524f68d360, 0x14ffb56adc4171af, 0xa211c25cea1f500a, 0x0a8d94b14b863a19, 0x1e4485509c07831d, 0xdf3cac8408358740, - 0xe9657af1b0ab1451, 0x705e7670ae225a03, 0xb57af2a89789b7f5, 0x301ddbc18e40103c, 0xc497a4ceced37e5f, 0x7e79eff7beefbdef, 0x7df902a478a8ea20, - 0xbf77632d0d1c0670, 0x8aa932c79851fc7f, 0x0cf6ecc8b93c56ea, 0x19ac7f7b8999e9db, 0x78f1a482ef8df3b8, 0x7e65029df9d378ec, 0x9ec5fb774dcc562d, - 0xfcfef5136f1fa8b8, 0x5f1ec1dac7324a37, 0x2ec13bde5f35ef35, 0xb76f5357b7ebf4d3, 0xd8941f7e5a8e9d5d, 0x2660e20bd9360d6d, 0x2d9a911d86fdb745, - 0x899c04dd446ff6a4, 0x339c68d9b1195146, 0x2c5332c93d4415cc, 0x4db229864dc3bc17, 0x3ed03f7071b0c9c8, 0xadaaf2831d9edecf, 0x66a3ea919265e733, - 0xa4481f7373d73291, 0xae99d84774e8f5c6, 0xddacd23849d48674, 0x87d3b38da34bca99, 0xd5685e1a207257df, 0x504be9f0fab62995, 0xb91ce7e6e2facba6, - 0x6411b5dc0eb9373d, 0x6f89baa624fada21, 0xc1fa6bd23b7f1267, 0x489807c4341f8c87, 0xc3c7bc274c214ace, 0x160aa22b85c16cb1, 0xe8448f3369594204, - 0xe168d1302a15c330, 0x16c3a02b425d7fce, 0xbe513426fd422108, 0x425fd3c6a3c0baa6, 0x69256bd167adcd8f, 0x5fbc3caa697a30f5, 0xcaf2b5670b3138e0, - 0xe71b170abad6831e, 0x2ee4bce6a33ed4bd, 0x36df612dfac5768a, 0x345fca41e61ef64f, 0x1c0106e68009e6d8, 0x5560ee566d27256a, 0x09532bc0de3f23cd, - 0x980cfe0200000000, - }; - }; + using WebFrontIco = generated::FaviconData; using WebFrontJs = generated::WebFrontJsData; diff --git a/js/core/EmbedBinaryAsset.cmake b/js/core/EmbedBinaryAsset.cmake new file mode 100644 index 00000000..0178a8ff --- /dev/null +++ b/js/core/EmbedBinaryAsset.cmake @@ -0,0 +1,76 @@ +# Renders a small binary asset (identity encoded) as a header in the format of the embedded assets, +# in pure CMake: no Node.js, no script. Bounded lines are joined once, so the string never grows +# word by word. Used for the default favicon; larger or compressed assets are out of scope. +# cmake -DINPUT= -DSTRUCT= (-DOUTPUT=
| -DCHECK=
) -P EmbedBinaryAsset.cmake +if(NOT DEFINED INPUT) + message(FATAL_ERROR "INPUT must name the binary asset source") +endif() +if(NOT DEFINED STRUCT) + message(FATAL_ERROR "STRUCT must name the generated struct, e.g. FaviconData") +endif() +if(NOT DEFINED OUTPUT AND NOT DEFINED CHECK) + message(FATAL_ERROR "Set OUTPUT to regenerate the header or CHECK to verify it") +endif() + +file(SIZE "${INPUT}" size) +file(READ "${INPUT}" hex HEX) +math(EXPR word_count "(${size} + 7) / 8") + +set(lines "") +string(LENGTH "${hex}" hex_length) +set(offset 0) +while(offset LESS hex_length) + math(EXPR remaining "${hex_length} - ${offset}") + if(remaining GREATER 80) + set(remaining 80) + endif() + string(SUBSTRING "${hex}" ${offset} ${remaining} chunk) + string(REGEX MATCHALL ".[^;]?[^;]?[^;]?[^;]?[^;]?[^;]?[^;]?[^;]?[^;]?[^;]?[^;]?[^;]?[^;]?[^;]?[^;]?" words "${chunk}") + set(rendered "") + foreach(word IN LISTS words) + string(LENGTH "${word}" word_length) + math(EXPR missing "16 - ${word_length}") + string(REPEAT "0" ${missing} padding) + list(APPEND rendered "0x${word}${padding}") + endforeach() + list(JOIN rendered ", " line) + list(APPEND lines " ${line}") + math(EXPR offset "${offset} + 80") +endwhile() +list(JOIN lines ",\n" body) +if(body) + set(body "\n${body}") +endif() + +set(contents "#pragma once + +#include +#include +#include +#include + +namespace webfront::fs::generated { + +struct ${STRUCT} { + static constexpr std::string_view encoding{}; + static constexpr std::size_t dataSize{${size}}; + static constexpr std::array data{${body} + }; +}; + +} // namespace webfront::fs::generated +") + +if(DEFINED CHECK) + if(NOT EXISTS "${CHECK}") + message(FATAL_ERROR "${CHECK} is missing; run the webfront-embedded-assets target") + endif() + file(READ "${CHECK}" current) + if(NOT current STREQUAL contents) + message(FATAL_ERROR "${CHECK} is stale; run the webfront-embedded-assets target") + endif() +else() + get_filename_component(directory "${OUTPUT}" DIRECTORY) + file(MAKE_DIRECTORY "${directory}") + file(WRITE "${OUTPUT}" "${contents}") +endif() diff --git a/js/core/WebFrontAssets.cmake b/js/core/WebFrontAssets.cmake index 1f99683b..24127c08 100644 --- a/js/core/WebFrontAssets.cmake +++ b/js/core/WebFrontAssets.cmake @@ -28,6 +28,21 @@ function(_webfront_add_embedded_text_asset source header struct test_name) endif() endfunction() +# The favicon is binary and small: a pure CMake renderer, with no Node.js, produces its header. +function(_webfront_add_embedded_binary_asset source header struct test_name) + add_custom_command(TARGET webfront-embedded-assets POST_BUILD + COMMAND ${CMAKE_COMMAND} -DINPUT=${source} -DOUTPUT=${header} -DSTRUCT=${struct} + -P ${_WEBFRONT_ASSETS_DIR}/EmbedBinaryAsset.cmake + COMMENT "Regenerating ${header}" + VERBATIM + ) + if(ENABLE_TESTING) + add_test(NAME ${test_name} + COMMAND ${CMAKE_COMMAND} -DINPUT=${source} -DCHECK=${header} -DSTRUCT=${struct} + -P ${_WEBFRONT_ASSETS_DIR}/EmbedBinaryAsset.cmake) + endif() +endfunction() + function(webfront_add_embedded_assets) find_package(Git QUIET) set(root "${_WEBFRONT_ASSETS_DIR}/../..") @@ -42,6 +57,9 @@ function(webfront_add_embedded_assets) _webfront_add_embedded_text_asset( "${root}/js/core/fallback/webfront-fallback.mjs" "${root}/include/webfront/system/FallbackModuleData.hpp" FallbackModuleData FallbackModuleAssetFresh) + _webfront_add_embedded_binary_asset( + "${root}/js/core/favicon.ico" "${root}/include/webfront/system/FaviconData.hpp" + FaviconData FaviconAssetFresh) _webfront_add_embedded_text_asset( "${root}/js/core/bootstrap/index.html" "${root}/include/webfront/system/DeveloperIndexHtmlData.hpp" @@ -51,6 +69,12 @@ function(webfront_add_embedded_assets) DeveloperBootstrapData DeveloperBootstrapAssetFresh) if(ENABLE_TESTING) + add_test(NAME MinimalCoreAssets + COMMAND ${CMAKE_COMMAND} -DROOT=${root} -P ${root}/test/cmake/CheckMinimalCoreAssets.cmake) + # The guard must fail on an asset header outside the normative list. + add_test(NAME MinimalCoreAssetsRejectsUnlisted + COMMAND ${CMAKE_COMMAND} -DROOT=${root} -DWORK_DIR=${CMAKE_CURRENT_BINARY_DIR}/minimal-core-assets + -P ${root}/test/cmake/CheckMinimalCoreAssetsNegative.cmake) find_program(WEBFRONT_NODE_EXECUTABLE NAMES node nodejs REQUIRED) add_test(NAME DeveloperJavaScript COMMAND "${WEBFRONT_NODE_EXECUTABLE}" --test "${root}/js/developer/tools/check.mjs" "${root}/js/developer/tools/distribution-check.mjs" diff --git a/js/core/favicon.ico b/js/core/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..f7243737436ec7973f744d1adc06b46668c21057 GIT binary patch literal 766 zcma)4F=*6a6#nk2v4dj&6O9mRb5=n)sBn>^bV*Sw#X(dEN9$nl^^Lyfnxy95Ov}xQO5s9-%eood>(7H3^u>M!1UB@JbzrlxB3O#xH^vZ;#oZM&f(qL z2Ii{{e*OG`H}`eiD-UDiLm8!s%kT%gJLmmIw*+eXpE~^{*5wBuS7WEg25fw0uFLx4 zfHc#pAh^3nRG5^6PLi>mC?N(CtqPYwK{wVZm8mVUG6Kr zjCt#?SyO9Me#=(M)Q)O%asO3YaTsfQZRXwd{T0@+6;5e;;H1AKho@H}`ni3WPtjns z1dTb%ZO%)(;|o#X9VH6qi$x(XG3hv*htWvUvMLqJvX-N;!z0stM!r?FCWAsD45Pp> z#$;gAFpQG39FR5^$1YtNF$^=j^U?{7r%N#?5ajC@3nnGRFr%fM-+0)Cn%@F?=HxH- kzev+ZIYQh|1*eV!y$irNar7FnaEn^~8#0NQS_2OJ1~gK&rT_o{ literal 0 HcmV?d00001 diff --git a/test/cmake/CheckMinimalCoreAssets.cmake b/test/cmake/CheckMinimalCoreAssets.cmake new file mode 100644 index 00000000..845c3340 --- /dev/null +++ b/test/cmake/CheckMinimalCoreAssets.cmake @@ -0,0 +1,96 @@ +# Guard of the minimal core (ADR-016): the generated resource headers of include/webfront/ are a +# closed, normative list with a size budget. Adding a web resource means changing this list and the +# ADR in the same change. +# cmake -DROOT= -P test/cmake/CheckMinimalCoreAssets.cmake +cmake_minimum_required(VERSION 3.31) + +if(NOT ROOT) + message(FATAL_ERROR "ROOT is required") +endif() + +# Normative list: header file name under include/webfront/system/. +set(allowed + WebFrontJsData.hpp # bridge WebFront.js + FallbackIndexHtmlData.hpp # fallback page + FallbackModuleData.hpp # fallback module + FaviconData.hpp # default favicon + DeveloperIndexHtmlData.hpp # bootstrap page + DeveloperBootstrapData.hpp # bootstrap loader + DeveloperReleaseData.hpp) # pinned release (URL and SHA-512, no payload) +# Budget on the decoded bytes of the embedded payloads, independent of their encoding. +set(budget_bytes 153600) + +file(GLOB_RECURSE headers RELATIVE "${ROOT}/include/webfront" "${ROOT}/include/webfront/*.hpp") +list(SORT headers) + +set(generated "") +foreach(header IN LISTS headers) + get_filename_component(name "${header}" NAME) + file(READ "${ROOT}/include/webfront/${header}" text) + # A resource header is named *Data.hpp or embeds a payload (a dataSize constant). + if(name MATCHES "Data\\.hpp$" OR text MATCHES "dataSize[ \t]*[{=]") + list(APPEND generated "${header}") + endif() +endforeach() + +set(unexpected "") +set(decoded_total 0) +set(encoded_total 0) +set(file_total 0) +foreach(header IN LISTS generated) + get_filename_component(name "${header}" NAME) + if(NOT header STREQUAL "system/${name}" OR NOT name IN_LIST allowed) + list(APPEND unexpected "${header}") + continue() + endif() + file(SIZE "${ROOT}/include/webfront/${header}" file_size) + math(EXPR file_total "${file_total} + ${file_size}") + file(READ "${ROOT}/include/webfront/${header}" text) + if(NOT text MATCHES "dataSize\\{([0-9]+)\\}") + set(encoded 0) + set(decoded 0) + set(kind "no payload") + else() + set(encoded ${CMAKE_MATCH_1}) + set(decoded ${encoded}) + set(kind "identity") + if(text MATCHES "encoding\\{\"gzip\"\\}") + # The decoded size is the ISIZE trailer: the last four bytes, little endian. + string(REGEX MATCHALL "0x[0-9a-f]+" words "${text}") + string(REGEX REPLACE "0x" "" words "${words}") + string(REPLACE ";" "" hex "${words}") + math(EXPR trailer "(${encoded} - 4) * 2") + string(SUBSTRING "${hex}" ${trailer} 8 isize) + string(REGEX REPLACE "^(..)(..)(..)(..)$" "0x\\4\\3\\2\\1" isize "${isize}") + math(EXPR decoded "${isize}") + set(kind "gzip") + endif() + endif() + math(EXPR decoded_total "${decoded_total} + ${decoded}") + math(EXPR encoded_total "${encoded_total} + ${encoded}") + message(STATUS "${header}: ${kind}, ${decoded} decoded, ${encoded} encoded bytes") +endforeach() + +# Every listed resource must exist: a stale list is as wrong as a stale directory. +set(missing "") +foreach(name IN LISTS allowed) + if(NOT EXISTS "${ROOT}/include/webfront/system/${name}") + list(APPEND missing "${name}") + endif() +endforeach() + +message(STATUS "Embedded resources: ${decoded_total} decoded, ${encoded_total} encoded bytes, " + "${file_total} bytes of headers (budget ${budget_bytes} decoded)") + +if(unexpected) + list(JOIN unexpected "\n " list) + message(FATAL_ERROR "Resource headers outside the normative list of ADR-016:\n ${list}\n" + "Remove them, or amend the ADR and test/cmake/CheckMinimalCoreAssets.cmake together.") +endif() +if(missing) + list(JOIN missing "\n " list) + message(FATAL_ERROR "Listed resource headers are missing:\n ${list}") +endif() +if(decoded_total GREATER budget_bytes) + message(FATAL_ERROR "Embedded resources total ${decoded_total} decoded bytes, over the budget of ${budget_bytes}") +endif() diff --git a/test/cmake/CheckMinimalCoreAssetsNegative.cmake b/test/cmake/CheckMinimalCoreAssetsNegative.cmake new file mode 100644 index 00000000..565c36ea --- /dev/null +++ b/test/cmake/CheckMinimalCoreAssetsNegative.cmake @@ -0,0 +1,59 @@ +# The guard of the minimal core must fail on an unlisted resource header and pass once it is removed. +# cmake -DROOT= -DWORK_DIR= -P test/cmake/CheckMinimalCoreAssetsNegative.cmake +cmake_minimum_required(VERSION 3.31) + +if(NOT ROOT OR NOT WORK_DIR) + message(FATAL_ERROR "ROOT and WORK_DIR are required") +endif() +file(REMOVE_RECURSE "${WORK_DIR}") +file(MAKE_DIRECTORY "${WORK_DIR}/include") +file(COPY "${ROOT}/include/webfront" DESTINATION "${WORK_DIR}/include") + +function(run_guard result_variable output_variable) + execute_process( + COMMAND "${CMAKE_COMMAND}" -DROOT=${WORK_DIR} -P "${CMAKE_CURRENT_LIST_DIR}/CheckMinimalCoreAssets.cmake" + RESULT_VARIABLE result OUTPUT_VARIABLE output ERROR_VARIABLE error) + set(${result_variable} "${result}" PARENT_SCOPE) + set(${output_variable} "${output}${error}" PARENT_SCOPE) +endfunction() + +run_guard(clean_result clean_output) +if(NOT clean_result EQUAL 0) + message(FATAL_ERROR "The guard rejects a clean copy of the headers:\n${clean_output}") +endif() + +set(rogue "${WORK_DIR}/include/webfront/system/RogueData.hpp") +file(WRITE "${rogue}" "#pragma once\n// not a normative resource\n") +run_guard(rogue_result rogue_output) +if(rogue_result EQUAL 0 OR NOT rogue_output MATCHES "system/RogueData\\.hpp") + message(FATAL_ERROR "The guard accepts an unlisted *Data.hpp:\n${rogue_output}") +endif() + +# A payload hidden behind another name is a resource too. +file(REMOVE "${rogue}") +file(WRITE "${WORK_DIR}/include/webfront/system/Sneaky.hpp" + "#pragma once\nstruct Sneaky { static constexpr unsigned long dataSize{1}; };\n") +run_guard(sneaky_result sneaky_output) +if(sneaky_result EQUAL 0 OR NOT sneaky_output MATCHES "system/Sneaky\\.hpp") + message(FATAL_ERROR "The guard accepts an unlisted payload header:\n${sneaky_output}") +endif() + +# A resource that outgrows the budget is refused as well. +file(REMOVE "${WORK_DIR}/include/webfront/system/Sneaky.hpp") +file(WRITE "${WORK_DIR}/include/webfront/system/FaviconData.hpp" + "#pragma once\nstruct FaviconData { static constexpr std::string_view encoding{}; static constexpr std::size_t dataSize{999999}; };\n") +run_guard(budget_result budget_output) +if(budget_result EQUAL 0 OR NOT budget_output MATCHES "over the budget") + message(FATAL_ERROR "The guard accepts an oversized resource:\n${budget_output}") +endif() + +# The budget counts decoded bytes: a small gzip stream whose trailer announces 196608 bytes is over it. +file(WRITE "${WORK_DIR}/include/webfront/system/FaviconData.hpp" + "#pragma once\nstruct FaviconData {\n static constexpr std::string_view encoding{\"gzip\"};\n" + " static constexpr std::size_t dataSize{8};\n static constexpr std::array data{ 0x0000000000000300 };\n};\n") +run_guard(gzip_result gzip_output) +if(gzip_result EQUAL 0 OR NOT gzip_output MATCHES "gzip, 196608 decoded" OR NOT gzip_output MATCHES "over the budget") + message(FATAL_ERROR "The guard ignores the decoded size of a gzip resource:\n${gzip_output}") +endif() + +file(REMOVE_RECURSE "${WORK_DIR}") diff --git a/test/unit/DefaultFSTests.cpp b/test/unit/DefaultFSTests.cpp index b531b0b3..86b35911 100644 --- a/test/unit/DefaultFSTests.cpp +++ b/test/unit/DefaultFSTests.cpp @@ -7,6 +7,7 @@ #include #include #include +#include using namespace std; using namespace webfront; @@ -36,6 +37,9 @@ string readAll(fs::File& file) { } // namespace +// The default composition is the protected bridge, the optional root and IndexFS, nothing else. +static_assert(std::is_same_v>); + SCENARIO("DefaultFS serves only embedded assets when no document root is configured") { GIVEN("a DefaultFS constructed with an empty document root") { fs::DefaultFS defaultFS{filesystem::path{}}; @@ -48,6 +52,7 @@ SCENARIO("DefaultFS serves only embedded assets when no document root is configu THEN("an arbitrary file is not found, and no local filesystem access is attempted") { REQUIRE_FALSE(defaultFS.open("does-not-exist.txt").has_value()); + REQUIRE_FALSE(defaultFS.open("webfront-bootstrap.mjs").has_value()); } } } diff --git a/test/unit/IndexFSTests.cpp b/test/unit/IndexFSTests.cpp index d1453dbc..5f89530b 100644 --- a/test/unit/IndexFSTests.cpp +++ b/test/unit/IndexFSTests.cpp @@ -1,5 +1,4 @@ #include -#include #include #include @@ -54,12 +53,9 @@ SCENARIO("IndexFileSystem provides basic files for browser support") { array buffer; auto readSize = faviconFile->read(span(reinterpret_cast(buffer.data()), buffer.size())); array head, tail; - REQUIRE(faviconFile->getEncoding() == "gzip"); - const auto decoded = http::gzip::decodeGzip(string_view(reinterpret_cast(buffer.data()), readSize)); - REQUIRE(decoded); - REQUIRE(decoded->size() == 766); - copy(decoded->begin(), decoded->end(), buffer.begin()); - readSize = decoded->size(); + REQUIRE_FALSE(faviconFile->isEncoded()); + REQUIRE(faviconFile->getEncoding().empty()); + REQUIRE(readSize == 766); head = {0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x20, 0x20, 0x10, 0x00, 0x01, 0x00, 0x04, 0x00, 0xE8, 0x02}; tail = {0X00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x07}; REQUIRE(equal(head.begin(), head.end(), buffer.begin())); From 240b793fd5e5090cfe4a02ee1d909af693d94f1e Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc Date: Fri, 23 Oct 2026 17:36:11 +0200 Subject: [PATCH 2/3] Correct the include size reference in the guard documentation [#314] --- docs/CHANGELOG.md | 2 +- docs/EMBEDDED_ASSETS.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 69619707..26b16aa6 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -88,7 +88,7 @@ - Guard of the minimal core (#314): the `MinimalCoreAssets` CTest case lists the generated resource headers of `include/webfront/` and fails on one outside the normative list of ADR-016 or when the decoded payloads exceed 150 KiB - (currently 35 766 bytes in 99 341 bytes of headers; `include/webfront/system` went from 2.7 MB in #303 to 188 KB). + (currently 35 766 bytes in 99 341 bytes of headers; `include/webfront/system` went from about 2.5 MB at the start of #303 to 188 KB). The default favicon is the source `js/core/favicon.ico` rendered by `js/core/EmbedBinaryAsset.cmake`, pure CMake with no Node.js, into `FaviconData.hpp` (`FaviconAssetFresh` checks it); it is no longer a hand-written gzip array in `IndexFS.hpp` and is served unencoded. `IndexFS::WebFrontIco` stays an alias of the generated struct. diff --git a/docs/EMBEDDED_ASSETS.md b/docs/EMBEDDED_ASSETS.md index a45ee87b..ccbb6b13 100644 --- a/docs/EMBEDDED_ASSETS.md +++ b/docs/EMBEDDED_ASSETS.md @@ -23,7 +23,7 @@ outside the normative list of [ADR-016](adr/ADR-016-bootstrap-comme-socle.md), o 150 KiB. The budget counts decoded bytes (gzip streams are measured by their trailer), so it does not depend on the encoding; the test also prints the encoded bytes and the size of the headers. Adding a resource means amending the ADR and the list in the script together. `MinimalCoreAssetsRejectsUnlisted` proves the guard fails on an unlisted -header, a hidden payload and an oversized resource. `du -sh include/webfront/system` was 2.7 MB when the epic began +header, a hidden payload and an oversized resource. `du -sh include/webfront/system` was about 2.5 MB when the epic began and is 188 KB now. ## Maintenance From d0504db3086cb5532d45d85083dae4d19c925996 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc Date: Fri, 23 Oct 2026 18:07:41 +0200 Subject: [PATCH 3/3] Refuse resource sizes the guard cannot read, except for the release data [#314] --- test/cmake/CheckMinimalCoreAssets.cmake | 27 ++++++++++++- .../CheckMinimalCoreAssetsNegative.cmake | 39 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/test/cmake/CheckMinimalCoreAssets.cmake b/test/cmake/CheckMinimalCoreAssets.cmake index 845c3340..eda898fd 100644 --- a/test/cmake/CheckMinimalCoreAssets.cmake +++ b/test/cmake/CheckMinimalCoreAssets.cmake @@ -34,6 +34,7 @@ foreach(header IN LISTS headers) endforeach() set(unexpected "") +set(unreadable "") set(decoded_total 0) set(encoded_total 0) set(file_total 0) @@ -46,15 +47,30 @@ foreach(header IN LISTS generated) file(SIZE "${ROOT}/include/webfront/${header}" file_size) math(EXPR file_total "${file_total} + ${file_size}") file(READ "${ROOT}/include/webfront/${header}" text) - if(NOT text MATCHES "dataSize\\{([0-9]+)\\}") + # Only DeveloperReleaseData.hpp carries no payload. Any other header must declare one decimal + # dataSize ({n} or = n, spaces allowed) and a known encoding: a size the guard cannot read is refused. + string(REGEX MATCHALL "dataSize" size_mentions "${text}") + list(LENGTH size_mentions mention_count) + if(name STREQUAL "DeveloperReleaseData.hpp") + if(mention_count) + list(APPEND unreadable "${header}: the release data must not carry a payload") + endif() set(encoded 0) set(decoded 0) set(kind "no payload") + elseif(NOT mention_count EQUAL 1 + OR NOT text MATCHES "dataSize[ \t\r\n]*[{=][ \t\r\n]*([0-9]+)[ \t\r\n]*[;}]") + list(APPEND unreadable "${header}: expected exactly one decimal 'dataSize' declaration") + continue() else() set(encoded ${CMAKE_MATCH_1}) set(decoded ${encoded}) set(kind "identity") - if(text MATCHES "encoding\\{\"gzip\"\\}") + if(NOT text MATCHES "encoding[ \t\r\n]*[{=][ \t\r\n]*(\"[a-z]*\")?[ \t\r\n]*[;}]") + list(APPEND unreadable "${header}: unreadable 'encoding' declaration") + continue() + endif() + if(CMAKE_MATCH_1 STREQUAL "\"gzip\"") # The decoded size is the ISIZE trailer: the last four bytes, little endian. string(REGEX MATCHALL "0x[0-9a-f]+" words "${text}") string(REGEX REPLACE "0x" "" words "${words}") @@ -64,6 +80,9 @@ foreach(header IN LISTS generated) string(REGEX REPLACE "^(..)(..)(..)(..)$" "0x\\4\\3\\2\\1" isize "${isize}") math(EXPR decoded "${isize}") set(kind "gzip") + elseif(NOT CMAKE_MATCH_1 STREQUAL "" AND NOT CMAKE_MATCH_1 STREQUAL "\"\"") + list(APPEND unreadable "${header}: unsupported encoding ${CMAKE_MATCH_1}") + continue() endif() endif() math(EXPR decoded_total "${decoded_total} + ${decoded}") @@ -87,6 +106,10 @@ if(unexpected) message(FATAL_ERROR "Resource headers outside the normative list of ADR-016:\n ${list}\n" "Remove them, or amend the ADR and test/cmake/CheckMinimalCoreAssets.cmake together.") endif() +if(unreadable) + list(JOIN unreadable "\n " list) + message(FATAL_ERROR "Resource headers the guard cannot measure:\n ${list}") +endif() if(missing) list(JOIN missing "\n " list) message(FATAL_ERROR "Listed resource headers are missing:\n ${list}") diff --git a/test/cmake/CheckMinimalCoreAssetsNegative.cmake b/test/cmake/CheckMinimalCoreAssetsNegative.cmake index 565c36ea..4be32793 100644 --- a/test/cmake/CheckMinimalCoreAssetsNegative.cmake +++ b/test/cmake/CheckMinimalCoreAssetsNegative.cmake @@ -38,6 +38,45 @@ if(sneaky_result EQUAL 0 OR NOT sneaky_output MATCHES "system/Sneaky\\.hpp") message(FATAL_ERROR "The guard accepts an unlisted payload header:\n${sneaky_output}") endif() +file(REMOVE "${WORK_DIR}/include/webfront/system/Sneaky.hpp") +# A size the guard cannot read is refused rather than counted as zero. +foreach(declaration "dataSize = 999999;" "dataSize{ 999999 };" "dataSize{0x10};" "dataSize{1'000};" "dataSize{kSize};") + file(WRITE "${WORK_DIR}/include/webfront/system/FaviconData.hpp" + "#pragma once\nstruct FaviconData { static constexpr std::string_view encoding{}; static constexpr std::size_t ${declaration} };\n") + run_guard(unreadable_result unreadable_output) + if(unreadable_result EQUAL 0 OR NOT unreadable_output MATCHES "FaviconData\\.hpp") + message(FATAL_ERROR "The guard accepts '${declaration}':\n${unreadable_output}") + endif() +endforeach() +# Spaced and '=' forms are read, so their size is counted against the budget. +foreach(declaration "dataSize = 999999;" "dataSize{ 999999 };") + file(WRITE "${WORK_DIR}/include/webfront/system/FaviconData.hpp" + "#pragma once\nstruct FaviconData { static constexpr std::string_view encoding{}; static constexpr std::size_t ${declaration} };\n") + run_guard(spaced_result spaced_output) + if(spaced_result EQUAL 0 OR NOT spaced_output MATCHES "over the budget") + message(FATAL_ERROR "The guard does not count '${declaration}':\n${spaced_output}") + endif() +endforeach() +# An unknown encoding and a missing size are refused, and the release data must stay payload free. +file(WRITE "${WORK_DIR}/include/webfront/system/FaviconData.hpp" + "#pragma once\nstruct FaviconData { static constexpr std::string_view encoding{\"br\"}; static constexpr std::size_t dataSize{8}; };\n") +run_guard(encoding_result encoding_output) +if(encoding_result EQUAL 0 OR NOT encoding_output MATCHES "unsupported encoding") + message(FATAL_ERROR "The guard accepts an unknown encoding:\n${encoding_output}") +endif() +file(WRITE "${WORK_DIR}/include/webfront/system/FaviconData.hpp" "#pragma once\nstruct FaviconData {};\n") +run_guard(nosize_result nosize_output) +if(nosize_result EQUAL 0 OR NOT nosize_output MATCHES "FaviconData\\.hpp") + message(FATAL_ERROR "The guard accepts a resource without a size:\n${nosize_output}") +endif() +file(COPY "${ROOT}/include/webfront/system/FaviconData.hpp" DESTINATION "${WORK_DIR}/include/webfront/system") +file(APPEND "${WORK_DIR}/include/webfront/system/DeveloperReleaseData.hpp" "inline constexpr unsigned dataSize{999999};\n") +run_guard(release_result release_output) +if(release_result EQUAL 0 OR NOT release_output MATCHES "must not carry a payload") + message(FATAL_ERROR "The guard accepts a payload in the release data:\n${release_output}") +endif() +file(COPY "${ROOT}/include/webfront/system/DeveloperReleaseData.hpp" DESTINATION "${WORK_DIR}/include/webfront/system") + # A resource that outgrows the budget is refused as well. file(REMOVE "${WORK_DIR}/include/webfront/system/Sneaky.hpp") file(WRITE "${WORK_DIR}/include/webfront/system/FaviconData.hpp"