-
Notifications
You must be signed in to change notification settings - Fork 37
/
assets.rs
309 lines (271 loc) · 9.85 KB
/
assets.rs
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use crate::accounts_store::encode_metrics;
use crate::metrics_encoder::MetricsEncoder;
use crate::state::STATE;
use crate::StableState;
use candid::{CandidType, Decode, Encode};
use dfn_core::api::ic0::time;
use ic_certified_map::{labeled, labeled_hash, AsHashTree, Hash, RbTree};
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::io::Read;
type HeaderField = (String, String);
#[derive(Clone, Debug, CandidType, Deserialize)]
pub struct HttpRequest {
method: String,
url: String,
headers: Vec<(String, String)>,
body: ByteBuf,
}
#[derive(Clone, Debug, CandidType, Deserialize)]
pub struct HttpResponse {
status_code: u16,
headers: Vec<HeaderField>,
body: ByteBuf,
}
const LABEL_ASSETS: &[u8] = b"http_assets";
#[derive(Default)]
pub struct AssetHashes(RbTree<Vec<u8>, Hash>);
impl From<&Assets> for AssetHashes {
fn from(assets: &Assets) -> Self {
let mut asset_hashes = Self::default();
for (path, asset) in assets.0.iter() {
asset_hashes
.0
.insert(path.as_bytes().to_vec(), hash_bytes(&asset.bytes));
}
asset_hashes
}
}
/// An asset to be served via HTTP requests.
#[derive(CandidType, Clone, Deserialize, PartialEq, Debug)]
pub struct Asset {
headers: Vec<HeaderField>,
bytes: Vec<u8>,
// Whether the asset is persisted across upgrades.
stable: bool,
}
impl Asset {
pub fn new(bytes: Vec<u8>) -> Self {
Self {
headers: vec![],
bytes,
stable: false,
}
}
pub fn new_stable(bytes: Vec<u8>) -> Self {
Self {
headers: vec![],
bytes,
stable: true,
}
}
pub fn with_header<S: Into<String>>(mut self, key: S, val: S) -> Self {
self.headers.push((key.into(), val.into()));
self
}
}
#[derive(Default, CandidType, Deserialize, PartialEq, Debug)]
pub struct Assets(HashMap<String, Asset>);
impl Assets {
fn insert<S: Into<String>>(&mut self, path: S, asset: Asset) {
self.0.insert(path.into(), asset);
}
fn get(&self, path: &str) -> Option<&Asset> {
self.0.get(path)
}
}
pub fn http_request(req: HttpRequest) -> HttpResponse {
let parts: Vec<&str> = req.url.split('?').collect();
match parts[0] {
"/metrics" => {
let now;
unsafe {
now = time();
};
let mut writer = MetricsEncoder::new(vec![], now / 1_000_000);
match encode_metrics(&mut writer) {
Ok(()) => {
let body = writer.into_inner();
HttpResponse {
status_code: 200,
headers: vec![
("Content-Type".to_string(), "text/plain; version=0.0.4".to_string()),
("Content-Length".to_string(), body.len().to_string()),
],
body: ByteBuf::from(body),
}
}
Err(err) => HttpResponse {
status_code: 500,
headers: vec![],
body: ByteBuf::from(format!("Failed to encode metrics: {}", err)),
},
}
}
request_path => STATE.with(|s| {
let mut headers = security_headers();
let certificate_header = make_asset_certificate_header(&s.asset_hashes.borrow(), request_path);
headers.push(certificate_header);
match s.assets.borrow().get(request_path) {
Some(asset) => {
headers.extend(asset.headers.clone());
if let Some(content_type) = content_type_of(request_path) {
headers.push(("Content-Type".to_string(), content_type.to_string()));
}
HttpResponse {
status_code: 200,
headers,
body: ByteBuf::from(asset.bytes.clone()),
}
}
None => HttpResponse {
status_code: 404,
headers,
body: ByteBuf::from(format!("Asset {} not found.", request_path)),
},
}
}),
}
}
fn content_type_of(request_path: &str) -> Option<&'static str> {
if request_path.ends_with('/') {
return Some("text/html");
}
request_path
.split('.')
.last()
.map(|suffix| match suffix {
"css" => Some("text/css"),
"html" => Some("text/html"),
"js" => Some("application/javascript"),
"json" => Some("application/json"),
"svg" => Some("image/svg+xml"),
_ => None,
})
.flatten()
}
/// List of recommended security headers as per https://owasp.org/www-project-secure-headers/
/// These headers enable browser security features (like limit access to platform apis and set
/// iFrame policies, etc.).
/// TODO https://dfinity.atlassian.net/browse/L2-185: Add CSP and Permissions-Policy
fn security_headers() -> Vec<HeaderField> {
vec![
("X-Frame-Options".to_string(), "DENY".to_string()),
("X-Content-Type-Options".to_string(), "nosniff".to_string()),
(
"Strict-Transport-Security".to_string(),
"max-age=31536000 ; includeSubDomains".to_string(),
),
// "Referrer-Policy: no-referrer" would be more strict, but breaks local dev deployment
// same-origin is still ok from a security perspective
("Referrer-Policy".to_string(), "same-origin".to_string()),
]
}
fn make_asset_certificate_header(asset_hashes: &AssetHashes, asset_name: &str) -> (String, String) {
let certificate = dfn_core::api::data_certificate().unwrap_or_else(|| {
dfn_core::api::trap_with("data certificate is only available in query calls");
unreachable!()
});
let witness = asset_hashes.0.witness(asset_name.as_bytes());
let tree = labeled(LABEL_ASSETS, witness);
let mut serializer = serde_cbor::ser::Serializer::new(vec![]);
serializer.self_describe().unwrap();
tree.serialize(&mut serializer)
.unwrap_or_else(|e| dfn_core::api::trap_with(&format!("failed to serialize a hash tree: {}", e)));
(
"IC-Certificate".to_string(),
format!(
"certificate=:{}:, tree=:{}:",
base64::encode(&certificate),
base64::encode(&serializer.into_inner())
),
)
}
pub fn hash_bytes(value: impl AsRef<[u8]>) -> Hash {
let mut hasher = Sha256::new();
hasher.update(value.as_ref());
hasher.finalize().into()
}
/// Insert an asset into the state.
pub fn insert_asset<S: Into<String> + Clone>(path: S, asset: Asset) {
dfn_core::api::print(format!("Inserting asset {}", &path.clone().into()));
STATE.with(|s| {
let mut asset_hashes = s.asset_hashes.borrow_mut();
let mut assets = s.assets.borrow_mut();
let path = path.into();
let index = "index.html";
if path.split('/').last() == Some(index) {
// Add the directory, with trailing slash, as an alternative path.
// Note: Without the trailing slash the location of "." is the parent, so breaks resource links.
let prefix_len = path.len() - index.len();
let dirname = &path[..prefix_len];
asset_hashes
.0
.insert(dirname.as_bytes().to_vec(), hash_bytes(&asset.bytes));
assets.insert(dirname, asset.clone());
}
asset_hashes
.0
.insert(path.as_bytes().to_vec(), hash_bytes(&asset.bytes));
assets.insert(path, asset);
update_root_hash(&asset_hashes);
});
}
// used both in init and post_upgrade
pub fn init_assets() {
let compressed = include_bytes!("../../assets.tar.xz").to_vec();
let mut decompressed = Vec::new();
lzma_rs::xz_decompress(&mut compressed.as_ref(), &mut decompressed).unwrap();
let mut tar: tar::Archive<&[u8]> = tar::Archive::new(decompressed.as_ref());
for entry in tar.entries().unwrap() {
let mut entry = entry.unwrap();
if !entry.header().entry_type().is_file() {
continue;
}
let name_bytes = entry.path_bytes().into_owned().strip_prefix(b".").unwrap().to_vec();
let name = String::from_utf8(name_bytes.clone()).unwrap_or_else(|e| {
dfn_core::api::trap_with(&format!(
"non-utf8 file name {}: {}",
String::from_utf8_lossy(&name_bytes),
e
));
unreachable!()
});
let mut bytes = Vec::new();
entry.read_to_end(&mut bytes).unwrap();
dfn_core::api::print(format!("{}: {}", &name, bytes.len()));
insert_asset(name, Asset::new(bytes));
}
}
impl StableState for Assets {
fn encode(&self) -> Vec<u8> {
// Encode all stable assets.
let stable_assets: Assets = Assets(self.0.clone().into_iter().filter(|(_, asset)| asset.stable).collect());
Encode!(&stable_assets).unwrap()
}
fn decode(bytes: Vec<u8>) -> Result<Self, String> {
Decode!(&bytes, Assets).map_err(|err| err.to_string())
}
}
fn update_root_hash(a: &AssetHashes) {
let prefixed_root_hash = &labeled_hash(LABEL_ASSETS, &a.0.root_hash());
dfn_core::api::set_certified_data(&prefixed_root_hash[..]);
}
#[test]
fn encode_decode() {
// Test that encoding/decoding preserves stable assets.
use maplit::hashmap;
let assets = Assets(hashmap! {
"x".to_string() => Asset::new_stable(vec![1,2,3]),
"y".to_string() => Asset::new(vec![4,5,6])
});
let assets = Assets::decode(assets.encode()).unwrap();
assert_eq!(
assets,
Assets(hashmap! {
"x".to_string() => Asset::new_stable(vec![1,2,3])
})
);
}