Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
reload_autocaps.hpp
Go to the documentation of this file.
1#pragma once
2
3// Infer the capabilities a scripted UI needs by scanning its JS for the effectful
4// bridge functions it calls. This backs an automatic capability declaration: the
5// developer writes normal UI code and the tooling fills in the granted set, so
6// least-privilege costs them nothing. It is a conservative token scan, not a full
7// parser — it maps each effectful bridge function name to the capability that gates
8// its group (kept in lockstep with the bridge's group gating, so a UI that calls a
9// gated function always declares the capability that function needs).
10//
11// A pure-UI call (canvas/layout/widgets/style) maps to nothing. Missing a call here
12// is not a security hole — the bridge still refuses an ungranted function at load;
13// the developer just sees a clear "declare X" error. Over-declaring is the safe
14// direction, so a mixed group maps every one of its functions to the group's
15// capability.
16
18
19#include <algorithm>
20#include <string>
21#include <string_view>
22#include <utility>
23#include <vector>
24
25namespace pulp::view {
26
29inline const std::vector<std::pair<std::string_view, ReloadCapability>>&
31 static const std::vector<std::pair<std::string_view, ReloadCapability>> table = {
32 {"exec", ReloadCapability::Exec},
33 {"execAsync", ReloadCapability::Exec},
34 {"setAICli", ReloadCapability::Ai},
35 {"getAICli", ReloadCapability::Ai},
36 {"readClipboard", ReloadCapability::Clipboard},
37 {"writeClipboard", ReloadCapability::Clipboard},
38 {"showOpenDialog", ReloadCapability::Filesystem},
39 {"showSaveDialog", ReloadCapability::Filesystem},
40 {"chooseFolder", ReloadCapability::Filesystem},
41 {"setImageSource", ReloadCapability::Filesystem},
42 {"setKnobSpriteStrip", ReloadCapability::Filesystem},
43 {"setKnobSpriteCore", ReloadCapability::Filesystem},
44 {"setKnobCapturedIndicator", ReloadCapability::Filesystem},
45 {"setFaderCapturedArt", ReloadCapability::Filesystem},
46 {"setFaderSkin", ReloadCapability::Filesystem},
47 {"setFaderTrackWidth", ReloadCapability::Filesystem},
48 {"setFaderTrackBorder", ReloadCapability::Filesystem},
49 {"setMeterColors", ReloadCapability::Filesystem},
50 {"setMeterBarRatio", ReloadCapability::Filesystem},
51 {"__loadAssetSync__", ReloadCapability::Filesystem},
52 {"loadFont", ReloadCapability::Filesystem},
53 {"registerFont", ReloadCapability::Filesystem},
54 {"storageGetItem", ReloadCapability::Storage},
55 {"storageSetItem", ReloadCapability::Storage},
56 {"storageRemoveItem", ReloadCapability::Storage},
57 {"setWidgetSchema", ReloadCapability::Storage},
58 {"setWidgetLottie", ReloadCapability::Storage},
59 {"seekWidgetLottie", ReloadCapability::Storage},
60 {"clearWidgetSchema", ReloadCapability::Storage},
61 {"saveStylePreset", ReloadCapability::Storage},
62 {"loadStylePreset", ReloadCapability::Storage},
63 {"__pulpRuntimeImport__", ReloadCapability::RuntimeImport},
64 {"__pulpRuntimeSettle__", ReloadCapability::RuntimeImport},
65 };
66 return table;
67}
68
69namespace detail {
70inline bool is_js_ident_char(char c) {
71 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
72 (c >= '0' && c <= '9') || c == '_' || c == '$';
73}
74
76inline bool contains_call_token(std::string_view src, std::string_view name) {
77 std::size_t pos = 0;
78 while ((pos = src.find(name, pos)) != std::string_view::npos) {
79 const bool left_ok = pos == 0 || !is_js_ident_char(src[pos - 1]);
80 const std::size_t after = pos + name.size();
81 const bool right_ok = after >= src.size() || !is_js_ident_char(src[after]);
82 if (left_ok && right_ok) return true;
83 pos = after;
84 }
85 return false;
86}
87} // namespace detail
88
90inline CapabilitySet infer_capabilities_from_js(std::string_view js_source) {
91 CapabilitySet caps;
92 for (const auto& [name, cap] : autocaps_function_table())
93 if (detail::contains_call_token(js_source, name)) caps.grant(cap);
94 return caps;
95}
96
99inline std::vector<std::string> infer_capability_names_from_js(std::string_view js_source) {
100 const auto caps = infer_capabilities_from_js(js_source);
101 std::vector<std::string> names;
106 if (caps.has(c)) names.emplace_back(capability_name(c));
107 }
108 return names;
109}
110
111} // namespace pulp::view
A granted-capability set.
Definition reload_capabilities.hpp:74
void grant(ReloadCapability c)
Definition reload_capabilities.hpp:85
bool contains_call_token(std::string_view src, std::string_view name)
Whole-token match: name occurs bounded by non-identifier chars (so exec matches pulp....
Definition reload_autocaps.hpp:76
bool is_js_ident_char(char c)
Definition reload_autocaps.hpp:70
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
std::vector< std::string > infer_capability_names_from_js(std::string_view js_source)
The capability NAMES a scripted UI needs, sorted + de-duplicated — ready to drop into a manifest's de...
Definition reload_autocaps.hpp:99
const std::vector< std::pair< std::string_view, ReloadCapability > > & autocaps_function_table()
Effectful bridge function name → the capability that gates it.
Definition reload_autocaps.hpp:30
CapabilitySet infer_capabilities_from_js(std::string_view js_source)
Scan js_source and return the granted set it needs.
Definition reload_autocaps.hpp:90
@ c
Definition input_events.hpp:39
std::string_view capability_name(ReloadCapability c)
Stable wire name for a capability (used in the SIGNED pack manifest + config).
Definition reload_capabilities.hpp:45
@ RuntimeImport
evaluate a runtime-imported JS bundle (dynamic code load)
Definition reload_capabilities.hpp:38
@ Clipboard
read/write the system clipboard
Definition reload_capabilities.hpp:34
@ Ai
AI-CLI bridge.
Definition reload_capabilities.hpp:37
@ Storage
persistent key/value + preset temp files
Definition reload_capabilities.hpp:36
@ Exec
shell exec/execAsync — ALSO compiled out unless the PULP_DEV_BRIDGE_EXEC CMake option is ON (release/...
Definition reload_capabilities.hpp:32
@ Filesystem
read arbitrary file paths (asset file loads, native dialogs)
Definition reload_capabilities.hpp:35
@ Network
outbound network (reserved; used by remote-UX-push, not the bridge today)
Definition reload_capabilities.hpp:39