Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit bb8187159700af59ea02c09a7fab21262ec85ca7
parent 0753dbc428699a363a9afa9739953faa1b6886c3
Author: sentomk <sentomk@proton.me>
Date:   Wed,  7 Oct 2026 17:31:05 +0800

fix: reject unsafe paths and unknown tables in kumo.toml

`kumoko clean` removes the whole `out` directory, and `out` is now a
setting, so `out = "."` or `out = "src"` made it delete the project or
its sources. An absolute `src` made the object files land next to the
sources instead of under `out`.

Require `src` and `out` to be relative paths inside the project, `out`
not to be the project directory, and `out` and `src` not to contain one
another. Require `name` to be a file name rather than a path.

Also report a misspelled platform table such as `[linxu]` even when it
has no keys, which used to be ignored silently.

Diffstat:
Mkumo_builder.cpp | 54+++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 53 insertions(+), 1 deletion(-)

diff --git a/kumo_builder.cpp b/kumo_builder.cpp @@ -184,6 +184,9 @@ struct Config { std::exit(EXIT_FAILURE); } +/// @brief The tables a platform can have, named like #HOST_NAME. +static constexpr const char* PLATFORM_TABLES[] = {"mac", "linux", "windows"}; + /// @brief One `key = value` pair of the file. struct Entry { bool is_list = false; ///< Which of #text and #list holds the value. @@ -286,6 +289,12 @@ class ConfigParser { if (peek() != ']') fail("expected ']'"); ++pos_; end_line(); + // Checked here rather than with the keys, so that a misspelled table with + // nothing in it is reported as well. + if (std::find(std::begin(PLATFORM_TABLES), std::end(PLATFORM_TABLES), + name) == std::end(PLATFORM_TABLES)) { + config_error(line_here(), "unknown table '[" + name + "]'"); + } return name; } @@ -365,6 +374,30 @@ class ConfigParser { } }; +/// @brief Tells whether a path from the configuration stays inside the project. +/// +/// It must be relative, must not be empty or `.`, and must not climb out with +/// `..`. Anything else could make the builder write, or `clean` delete, outside +/// the project directory or the project directory itself. +static bool is_inside_project(const fs::path& path) { + if (path.empty() || path.is_absolute() || path.has_root_name()) return false; + const fs::path normal = path.lexically_normal(); + if (normal == "." || normal.empty()) return false; + for (const fs::path& part : normal) { + if (part == "..") return false; + } + return true; +} + +/// @brief Tells whether @p inner is @p outer itself or lies below it. +static bool is_within(const fs::path& inner, const fs::path& outer) { + // lexically_relative() yields "../..." when inner is not below outer, and + // "." when the two are the same; trailing separators are ignored. + fs::path relative = + inner.lexically_normal().lexically_relative(outer.lexically_normal()); + return !relative.empty() && *relative.begin() != ".."; +} + /// @brief Reads and checks #CONFIG_FILE. /// /// Exits with a message if the file is missing, malformed or contains a key @@ -417,7 +450,7 @@ static Config load_config() { // Every platform's table is read, so that it counts as known, but only the // one of this platform is used. - for (const char* platform : {"mac", "linux", "windows"}) { + for (const char* platform : PLATFORM_TABLES) { const std::string section = platform; Cmd flags = take_list(section + ".flags"); Cmd link = take_list(section + ".link"); @@ -439,6 +472,25 @@ static Config load_config() { if (first) config_error(first->line, "unknown key '" + first_key + "'"); if (name.empty()) config_error(0, "'name' must not be empty"); + if (name.find_first_of("/\\") != std::string::npos) { + config_error(0, "'name' must be a file name, not a path"); + } + // `clean` deletes the whole of `out`, so it must be a directory of its own: + // not the project, not the sources, and not something holding them. + if (!is_inside_project(cfg.src_dir)) { + config_error(0, "'src' must be a relative path inside the project"); + } + if (!is_inside_project(cfg.out_dir)) { + config_error(0, + "'out' must be a relative path inside the project, and " + "not the project directory itself"); + } + if (is_within(cfg.out_dir, cfg.src_dir) || + is_within(cfg.src_dir, cfg.out_dir)) { + config_error(0, + "'out' and 'src' must not be the same directory or " + "contain one another"); + } cfg.exe = cfg.out_dir / (name + EXE_SUFFIX); return cfg; }