/// @brief Optional local developer session controller. Permissions are fixed by its native owner. #pragma once #include "../system/DeveloperPaths.hpp" #include "Contract.hpp" #include "Credentials.hpp" #include #include #include #include namespace webfront::automation { struct SessionOptions { std::filesystem::path project; std::vector allow; std::chrono::seconds timeout{120}; /// Application callbacks that `call` may invoke, fixed by the native owner. They need the `call` grant, and none refuses every call. std::vector callbacks{}; }; class Session { using Clock = std::chrono::steady_clock; struct Run { std::string id, operation, arguments, state{"queued"}, result{"null"}, output; bool cancel = false; Clock::time_point deadline; }; public: explicit Session(SessionOptions configuration) : options(std::move(configuration)) { this->options.project = developer::canonicalDirectory(this->options.project); if (this->options.timeout < std::chrono::seconds{1} || this->options.timeout > std::chrono::seconds{3600}) throw std::invalid_argument("Automation timeout must be 1..3600 seconds"); for (const auto& permission : this->options.allow) if (!knownPermission(permission)) throw std::invalid_argument("Unknown automation permission: " + permission); for (const auto& callback : this->options.callbacks) if (!callbackName(callback)) throw std::invalid_argument("Invalid allow-listed callback name: " + callback); if (!this->options.callbacks.empty() && std::ranges::find(this->options.allow, "call") == this->options.allow.end()) throw std::invalid_argument("Allow-listed callbacks need the call grant"); identity = channel::nonce(); } /// Granted operations that only newer environments implement, with the first version that does. std::vector> versionedOperations() const { std::vector> result; for (const auto& [name, definition] : operationDefinitions()) if (!definition.since.empty() && std::ranges::find(options.allow, definition.permission) != options.allow.end()) result.emplace_back(name, definition.since); return result; } std::string describe() const { std::scoped_lock lock{mutex}; return "{\"version\":1,\"session\":" + json::quote(identity) + ",\"project\":" + json::quote(options.project.generic_string()) + ",\"state\":" + json::quote(closed ? "closed" : ready ? "ready" : "starting") + ",\"allow\":" + stringArray(options.allow) + ",\"callbacks\":" + stringArray(options.callbacks) + ",\"failure\":" + json::quote(failure) + ",\"audit\":" + auditJson() + "}"; } std::string start(const Object& request, Clock::time_point now = Clock::now()) { std::scoped_lock lock{mutex}; try { authorize(request); } catch (const OperationError&) { audit("denied", operationLabel(request), ""); throw; } const auto operation = request.string("operation"); const auto definition = operationDefinitions().find(operation); if (definition == operationDefinitions().end() || std::ranges::find(options.allow, definition->second.permission) == options.allow.end()) { audit("denied", operationLabel(request), ""); throw OperationError("not_authorized", "Automation operation is not authorized"); } request.only({"session", "operation", "arguments"}); const Object arguments{request.raw("arguments", "{}")}; validateArguments(operation, arguments); if (operation == "call" && std::ranges::find(options.callbacks, arguments.string("callback")) == options.callbacks.end()) { audit("denied", "call", ""); throw OperationError("not_authorized", "This callback is not allow-listed for call"); } if (!ready || closed) throw OperationError("not_ready", "Developer session is not ready"); if (active) throw OperationError("busy", "Developer session is busy"); if (runs.size() == 64) runs.pop_front(); const auto id = "run-" + std::to_string(++counter); runs.push_back({id, operation, objectJson(arguments.members()), "queued", "null", "", false, now + options.timeout}); active = id; audit("allowed", operation, id); return snapshot(runs.back()); } std::string result(const Object& request) const { request.only({"session", "runId"}); std::scoped_lock lock{mutex}; authorize(request); return snapshot(find(request.string("runId"))); } std::string cancel(const Object& request) { request.only({"session", "runId"}); std::scoped_lock lock{mutex}; authorize(request); auto& run = find(request.string("runId")); if (active && *active == run.id) run.cancel = true; return snapshot(run); } // Only the browser selected by the native binder may call these methods. std::string poll(std::string output = {}) { std::scoped_lock lock{mutex}; if (closed) return "{\"close\":true}"; ready = true; if (!active) return "null"; auto& run = find(*active); if (output.size() > 8192) throw std::invalid_argument("Automation progress exceeds 8192 bytes"); if (run.state == "running") run.output = std::move(output); if (run.cancel) { if (run.state == "queued") { run.state = "cancelled"; active.reset(); return "null"; } return "{\"cancel\":" + json::quote(run.id) + "}"; } if (run.state != "queued") return "null"; run.state = "running"; return "{\"runId\":" + json::quote(run.id) + ",\"operation\":" + json::quote(run.operation) + ",\"arguments\":" + run.arguments + "}"; } // A completion the page cannot make valid (for example text that is not UTF-8) must not leave the run active until the // deadline revokes the whole session: the run fails, with the reason as its output. void complete(std::string_view id, std::string result) { std::scoped_lock lock{mutex}; if (!active || *active != id) throw std::runtime_error("Unexpected or late automation completion"); auto& run = find(*active); int status = 1; std::string output; bool interrupted = false; try { const Object checked{result}; status = completionStatus(checked); const auto flag = checked.raw("cancelled", "false"); if (flag != "true" && flag != "false") throw std::invalid_argument("Invalid cancellation marker"); interrupted = flag == "true"; output = checked.string("output"); } catch (const std::exception& error) { status = 1; interrupted = false; std::string reason{error.what()}; if (reason.size() > 1024) { std::size_t end = 1024; while (end && (static_cast(reason[end]) & 0xC0) == 0x80) --end; reason.resize(end); } output = "Invalid automation completion: " + reason; result = objectJson({ {"exitCode", "1"}, { "output", json::quote(output)} }); } run.result = std::move(result); run.output = std::move(output); run.state = status == 0 ? "succeeded" : status == 130 && (run.cancel || interrupted) ? "cancelled" : "failed"; active.reset(); } void close(std::string reason = {}) { std::scoped_lock lock{mutex}; closeLocked(std::move(reason)); } // A deadline closes this session: the renderer may still execute code, so it must not be reused. bool expire(Clock::time_point now = Clock::now()) { std::scoped_lock lock{mutex}; if (!active || now < find(*active).deadline) return false; auto& run = find(*active); run.state = "timeout"; active.reset(); closeLocked("Native operation deadline exceeded; session revoked"); return true; } // Readiness and startup expiry must be decided under the same lock: a poll arriving before this check wins. bool expireStartup(Clock::time_point deadline, Clock::time_point now = Clock::now()) { std::scoped_lock lock{mutex}; if (closed || ready || now < deadline) return false; closeLocked("Native automation startup deadline exceeded"); return true; } bool isClosed() const { std::scoped_lock lock{mutex}; return closed; } bool wasReady() const { std::scoped_lock lock{mutex}; return ready; } private: SessionOptions options; std::string identity, failure; mutable std::mutex mutex; std::deque runs; std::deque auditEntries; std::optional active; std::uint64_t counter = 0; bool ready = false, closed = false; void audit(std::string_view decision, std::string_view operation, std::string_view run) { if (auditEntries.size() == 64) auditEntries.pop_front(); auditEntries.push_back(objectJson({ { "decision", json::quote(decision)}, {"operation", json::quote(operation)}, { "runId", json::quote(run)} })); } std::string auditJson() const { std::string value = "["; for (const auto& entry : auditEntries) value += (value.size() > 1 ? "," : "") + entry; return value + "]"; } static int completionStatus(const Object& checked) { checked.only({"exitCode", "output", "report", "truncated", "data", "diagnostics", "cancelled"}); const auto code = checked.raw("exitCode"); int status = -1; const auto [end, error] = std::from_chars(code.data(), code.data() + code.size(), status); if (error != std::errc{} || end != code.data() + code.size() || status < 0 || status > 255) throw std::invalid_argument("Invalid automation exitCode"); checked.string("output"); return status; } /// A name an application registered, never an internal `webfront.*` one: letters, digits, `.`, `_`, `-`, at most 128. static bool callbackName(std::string_view name) { return !name.empty() && name.size() <= 128 && !name.starts_with("webfront.") && std::ranges::all_of(name, [](char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-'; }); } static bool knownPermission(std::string_view name) { for (const auto& [operation, definition] : operationDefinitions()) if (definition.permission == name) return true; return false; } static std::string operationLabel(const Object& request) { try { const auto operation = request.string("operation"); if (!operation.empty() && operation.size() <= 64) return operation; } catch (const std::exception&) { } return ""; } void authorize(const Object& request) const { std::string claimed; try { claimed = request.string("session"); } catch (const std::exception&) { } if (!channel::sameToken(claimed, identity)) throw OperationError("wrong_session", "Wrong automation session"); } // Match manifest.mjs labels before applying path rules: labels may contain ':' and consecutive dots. static bool entryLabel(std::string_view value) { const auto alphanumeric = [](char c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'); }; return !value.empty() && value.size() <= 64 && alphanumeric(value.front()) && std::ranges::all_of(value, [&](char c) { return alphanumeric(c) || c == ':' || c == '_' || c == '.' || c == '-'; }); } static void validateArguments(std::string_view operation, const Object& args) { validateOperation(operation, args); for (const auto& [key, raw] : args.members()) { const auto value = stringValue(raw); if (key == "entry" && !value.empty() && (value.starts_with('-') || value.contains('\\') || value.starts_with('/'))) throw std::invalid_argument("Entry must be a project-relative path or manifest entry"); if (key == "entry" && !value.empty() && !entryLabel(value)) fs::safeRelativePath(value); if (key == "callback" && !callbackName(value)) throw std::invalid_argument("Callback must be a plain application callback name"); if (key == "pattern" && value.starts_with('-')) throw std::invalid_argument("Pattern must not be an option"); if ((key == "name" || key == "starter" || key == "package" || key == "provider") && (value.starts_with('-') || value.contains('\\') || value.contains(' '))) throw std::invalid_argument("Argument must be a plain name, not an option or a path"); if ((key == "directory" || key == "output") && (value.empty() || value.starts_with('-') || value.contains('\\') || value.starts_with('/'))) throw std::invalid_argument("Argument must be a project-relative path"); if (key == "directory" || key == "output") fs::safeRelativePath(value); } if (operation == "embed") { const bool provider = args.has("provider"), entry = args.has("entry"), output = args.has("output"); if (provider == entry || entry != output) throw std::invalid_argument("embed needs either provider, or entry together with output"); } } Run& find(std::string_view id) { for (auto& run : runs) if (run.id == id) return run; throw OperationError("unknown_run", "Unknown or expired runId"); } const Run& find(std::string_view id) const { for (const auto& run : runs) if (run.id == id) return run; throw OperationError("unknown_run", "Unknown or expired runId"); } static std::string snapshot(const Run& run) { return "{\"runId\":" + json::quote(run.id) + ",\"operation\":" + json::quote(run.operation) + ",\"state\":" + json::quote(run.state) + ",\"cancelRequested\":" + (run.cancel ? "true" : "false") + ",\"output\":" + json::quote(run.output) + ",\"result\":" + run.result + "}"; } void closeLocked(std::string reason) { if (closed) return; closed = true; failure = std::move(reason); if (active) { find(*active).state = "disconnected"; active.reset(); } } }; } // namespace webfront::automation