1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use std::borrow::Cow;
use hearth_foundation::context::Cx;
use hearth_view::{AttributeValueViewParts, PartsWriter};
use serde::Serialize;
/// JavaScript source and captured values for a runtime expression.
///
/// Captured Rust values are serialized as data. Embedding this in a view
/// registers the trusted code for the runtime module route and writes its
/// reference with captures escaped for the surrounding HTML context.
#[derive(Debug, Clone)]
pub struct Js {
parts: Vec<JsPart>,
compiled: Option<(&'static str, Vec<String>, Vec<String>)>,
}
#[derive(Debug, Clone)]
enum JsPart {
/// Source text, escaped for the position it renders in.
Source(Cow<'static, str>),
/// Trusted scaffolding written verbatim, like the `const [..] = [..]`
/// wrapper around captured values.
Raw(&'static str),
/// A captured value as JSON, hydrated on the client.
Surrogate(String),
}
impl Js {
/// Trusted JavaScript source without captured values.
///
/// Never interpolate user input here. Use [`JsBuilder::surrogate`] for
/// values so they remain data and do not enter shared code modules.
#[must_use]
pub fn source(js: impl Into<Cow<'static, str>>) -> Self {
Self {
parts: vec![JsPart::Source(js.into())],
compiled: None,
}
}
/// Starts source that interleaves captured values.
#[must_use]
pub fn builder() -> JsBuilder {
JsBuilder {
parts: Vec::new(),
compiled: None,
}
}
/// Returns executable JavaScript, including serialized captured values.
///
/// The source expects the runtime context to be available as `cx`. It is
/// not escaped for embedding in HTML.
#[must_use]
pub fn to_source(&self) -> String {
let mut source = String::new();
for part in &self.parts {
match part {
JsPart::Source(js) => source.push_str(js),
JsPart::Raw(js) => source.push_str(js),
JsPart::Surrogate(json) => {
source.push_str("cx.hydrate(");
source.push_str(json);
source.push(')');
}
}
}
source
}
/// Returns a CSP-safe function reference with captured values kept as data.
///
/// The router's runtime serves the trusted function body as a same-origin
/// module. Captured values never enter that module or its cache key.
#[must_use]
pub fn to_client(&self) -> String {
if let Some((source, expressions, values)) = &self.compiled {
let id = crate::expression_module::digest(source);
return format!(
"#{id}:{{\"expressions\":[{}],\"values\":[{}]}}",
expressions.join(","),
values.join(",")
);
}
let mut source = String::new();
let mut captures = Vec::new();
for part in &self.parts {
match part {
JsPart::Source(js) => source.push_str(js),
JsPart::Raw(js) => source.push_str(js),
JsPart::Surrogate(json) => {
use std::fmt::Write;
let _ = write!(source, "cx.hydrate(__hearthCaptures[{}])", captures.len());
captures.push(json.as_str());
}
}
}
let id = crate::expression_module::digest(&source);
format!("#{id}:[{}]", captures.join(","))
}
/// Writes a function reference and captures, escaped for the HTML context.
pub(crate) fn write(&self, parts: &mut PartsWriter<'_>) {
parts.push_string(self.to_client());
}
}
/// The source as the value of a `data-hearth-*` attribute, like an event
/// handler or a bind expression.
impl AttributeValueViewParts for Js {
#[inline]
fn attribute_present(&self) -> bool {
true
}
#[inline]
fn into_view_parts(self, _cx: &Cx, parts: &mut PartsWriter<'_>) {
self.write(parts);
}
}
/// Builds a [`Js`] from source and captured values, in order.
#[derive(Debug)]
pub struct JsBuilder {
parts: Vec<JsPart>,
compiled: Option<(&'static str, Vec<String>, Vec<String>)>,
}
impl JsBuilder {
/// Uses a link-time registered function shape with expression references as captures.
#[must_use]
pub fn compiled(mut self, source: &'static str) -> Self {
self.compiled = Some((source, Vec::new(), Vec::new()));
self
}
/// Appends an expression in parentheses, preserving its captured values.
///
/// # Panics
///
/// Panics if serializing the nested expression reference fails.
#[must_use]
pub fn expression<T>(mut self, expression: &crate::Expr<T>) -> Self {
if let Some((_, expressions, _)) = &mut self.compiled {
expressions.push(serde_json::to_string(&expression.js.to_client()).unwrap());
}
self.parts.push(JsPart::Raw("("));
self.parts.extend(expression.js.parts.iter().cloned());
self.parts.push(JsPart::Raw(")"));
self
}
/// Appends trusted source text. Use [`Self::surrogate`] for user data.
#[must_use]
pub fn source(mut self, js: impl Into<Cow<'static, str>>) -> Self {
self.parts.push(JsPart::Source(js.into()));
self
}
/// Appends trusted scaffolding, written verbatim.
#[must_use]
pub fn raw(mut self, js: &'static str) -> Self {
self.parts.push(JsPart::Raw(js));
self
}
/// Adds data to a registered function without appending to the diagnostic source.
///
/// # Panics
///
/// Panics without a compiled function or if the value fails to serialize.
#[must_use]
pub fn capture(mut self, value: &(impl Serialize + ?Sized)) -> Self {
let json = serde_json::to_string(value).expect("failed to serialize capture");
self.compiled
.as_mut()
.expect("capture requires a compiled function")
.2
.push(json);
self
}
/// Appends a captured value, serialized now so the Rust expression can
/// consume it afterwards.
///
/// # Panics
///
/// Panics if the value fails to serialize.
#[must_use]
pub fn surrogate(mut self, value: &(impl Serialize + ?Sized)) -> Self {
let json = serde_json::to_string(value).expect("failed to serialize surrogate value");
if let Some((_, _, values)) = &mut self.compiled {
values.push(json.clone());
}
self.parts.push(JsPart::Surrogate(json));
self
}
/// Finishes the builder into a [`Js`].
#[must_use]
pub fn build(self) -> Js {
Js {
parts: self.parts,
compiled: self.compiled,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn client_reference_shares_code_without_sharing_captured_values() {
let make = |value: &str| {
Js::builder()
.source("[cx.some(")
.surrogate(value)
.source("), ")
.surrogate(&42)
.source("]")
.build()
.to_client()
};
let first = make("</script>\"&");
let second = make("different");
let (id, data) = first.split_once(':').unwrap();
assert_eq!(id, second.split_once(':').unwrap().0);
assert_eq!(
serde_json::from_str::<serde_json::Value>(data).unwrap(),
serde_json::json!(["</script>\"&", 42])
);
assert_ne!(first, second);
}
#[test]
fn executable_source_preserves_javascript_and_capture_escaping() {
let js = Js::builder()
.raw("(() => { const value = ")
.surrogate(&"\"<>&\n")
.source("; return value; })()")
.build();
assert_eq!(
js.to_source(),
r#"(() => { const value = cx.hydrate("\"<>&\n"); return value; })()"#,
);
}
}
inventory::submit! { crate::expression_module::ExpressionSource("cx.hydrate(__hearthCaptures.values[0])") }