Skip to content

Commit

Permalink
lib: Add validation for cursor file names and propagate the error fro…
Browse files Browse the repository at this point in the history
…m parsing HL cursor (#32)

* Validate cursor file names

* Propagate errors from parsing HL cursor

* Validate cursor directory names
  • Loading branch information
SoSeDiK authored Apr 12, 2024
1 parent f6a6322 commit 1787177
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions hyprcursor-util/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <array>
#include <format>
#include <algorithm>
#include <regex>
#include <hyprlang.hpp>
#include "internalSharedTypes.hpp"
#include "manifest.hpp"
Expand Down Expand Up @@ -98,6 +99,9 @@ static std::optional<std::string> createCursorThemeFromPath(const std::string& p
// iterate over the directory and record all cursors

for (auto& dir : std::filesystem::directory_iterator(CURSORDIR)) {
if (!std::regex_match(dir.path().stem().string(), std::regex("^[A-Za-z0-9_\\-\\.]+$")))
return "Invalid cursor directory name at " + dir.path().string() + " : characters must be within [A-Za-z0-9_\\-\\.]";

const auto METAPATH = dir.path().string() + "/meta";

auto& SHAPE = currentTheme.shapes.emplace_back(std::make_unique<SCursorShape>());
Expand Down
10 changes: 9 additions & 1 deletion libhyprcursor/meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <hyprlang.hpp>
#include <toml++/toml.hpp>
#include <filesystem>
#include <regex>

#include "VarList.hpp"

Expand Down Expand Up @@ -95,6 +96,11 @@ static Hyprlang::CParseResult parseDefineSize(const char* C, const char* V) {
RHS = LL;
}

if (!std::regex_match(RHS, std::regex("^[A-Za-z0-9_\\-\\.]+$"))) {
result.setError("Invalid cursor file name, characters must be within [A-Za-z0-9_\\-\\.] (if this seems like a mistake, check for invisible characters)");
return result;
}

size.file = RHS;

if (!size.file.ends_with(".svg")) {
Expand Down Expand Up @@ -132,7 +138,9 @@ std::optional<std::string> CMeta::parseHL() {
meta->registerHandler(::parseDefineSize, "define_size", {.allowFlags = false});
meta->registerHandler(::parseOverride, "define_override", {.allowFlags = false});
meta->commence();
meta->parse();
const auto RESULT = meta->parse();
if (RESULT.error)
return RESULT.getError();
} catch (const char* err) { return "failed parsing meta: " + std::string{err}; }

parsedData.hotspotX = std::any_cast<Hyprlang::FLOAT>(meta->getConfigValue("hotspot_x"));
Expand Down

0 comments on commit 1787177

Please sign in to comment.