-
Notifications
You must be signed in to change notification settings - Fork 93
/
functions.rs
474 lines (436 loc) · 17.1 KB
/
functions.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use super::RequestInfo;
use crate::webserver::{http::SingleOrVec, ErrorWithStatus};
use anyhow::{anyhow, Context};
use futures_util::StreamExt;
use std::{borrow::Cow, ffi::OsStr, str::FromStr};
super::function_definition_macro::sqlpage_functions! {
basic_auth_password((&RequestInfo));
basic_auth_username((&RequestInfo));
cookie((&RequestInfo), name: Cow<str>);
current_working_directory();
environment_variable(name: Cow<str>);
exec((&RequestInfo), program_name: Cow<str>, args: Vec<Cow<str>>);
fetch(http_request: SqlPageFunctionParam<super::http_fetch_request::HttpFetchRequest<'_>>);
hash_password(password: String);
header((&RequestInfo), name: Cow<str>);
path((&RequestInfo));
persist_uploaded_file((&RequestInfo), field_name: Cow<str>, folder: Option<Cow<str>>, allowed_extensions: Option<Cow<str>>);
protocol((&RequestInfo));
random_string(string_length: SqlPageFunctionParam<usize>);
read_file_as_data_url((&RequestInfo), file_path: Option<Cow<str>>);
read_file_as_text((&RequestInfo), file_path: Option<Cow<str>>);
run_sql((&RequestInfo), sql_file_path: Option<Cow<str>>);
uploaded_file_mime_type((&RequestInfo), upload_name: Cow<str>);
uploaded_file_path((&RequestInfo), upload_name: Cow<str>);
url_encode(raw_text: Option<Cow<str>>);
variables((&RequestInfo), get_or_post: Option<Cow<str>>);
version();
}
/// Returns the password from the HTTP basic auth header, if present.
async fn basic_auth_password(request: &RequestInfo) -> anyhow::Result<&str> {
let password = extract_basic_auth(request)?.password().ok_or_else(|| {
anyhow::Error::new(ErrorWithStatus {
status: actix_web::http::StatusCode::UNAUTHORIZED,
})
})?;
Ok(password)
}
/// Returns the username from the HTTP basic auth header, if present.
/// Otherwise, returns an HTTP 401 Unauthorized error.
async fn basic_auth_username(request: &RequestInfo) -> anyhow::Result<&str> {
Ok(extract_basic_auth(request)?.user_id())
}
fn extract_basic_auth(
request: &RequestInfo,
) -> anyhow::Result<&actix_web_httpauth::headers::authorization::Basic> {
request
.basic_auth
.as_ref()
.ok_or_else(|| {
anyhow::Error::new(ErrorWithStatus {
status: actix_web::http::StatusCode::UNAUTHORIZED,
})
})
.with_context(|| "Expected the user to be authenticated with HTTP basic auth")
}
async fn cookie<'a>(request: &'a RequestInfo, name: Cow<'a, str>) -> Option<Cow<'a, str>> {
request.cookies.get(&*name).map(SingleOrVec::as_json_str)
}
async fn current_working_directory() -> anyhow::Result<String> {
std::env::current_dir()
.with_context(|| "unable to access the current working directory")
.map(|x| x.to_string_lossy().into_owned())
}
/// Returns the value of an environment variable.
async fn environment_variable(name: Cow<'_, str>) -> anyhow::Result<Cow<'_, str>> {
std::env::var(&*name)
.with_context(|| format!("unable to access the environment variable {name}"))
.map(Cow::Owned)
}
/// Executes an external command and returns its output.
async fn exec<'a>(
request: &'a RequestInfo,
program_name: Cow<'a, str>,
args: Vec<Cow<'a, str>>,
) -> anyhow::Result<String> {
if !request.app_state.config.allow_exec {
anyhow::bail!("The sqlpage.exec() function is disabled in the configuration, for security reasons.
Make sure you understand the security implications before enabling it, and never allow user input to be passed as the first argument to this function.
You can enable it by setting the allow_exec option to true in the sqlpage.json configuration file.")
}
let res = tokio::process::Command::new(&*program_name)
.args(args.iter().map(|x| &**x))
.output()
.await
.with_context(|| {
let mut s = format!("Unable to execute command: {program_name}");
for arg in args {
s.push(' ');
s.push_str(&arg);
}
s
})?;
if !res.status.success() {
anyhow::bail!(
"Command '{program_name}' failed with exit code {}: {}",
res.status,
String::from_utf8_lossy(&res.stderr)
);
}
Ok(String::from_utf8_lossy(&res.stdout).into_owned())
}
async fn fetch(
http_request: super::http_fetch_request::HttpFetchRequest<'_>,
) -> anyhow::Result<String> {
use awc::http::Method;
let client = awc::Client::builder()
.add_default_header((awc::http::header::USER_AGENT, env!("CARGO_PKG_NAME")))
.finish();
let method = if let Some(method) = http_request.method {
Method::from_str(&method)?
} else {
Method::GET
};
let mut req = client.request(method, http_request.url.as_ref());
for (k, v) in http_request.headers {
req = req.insert_header((k.as_ref(), v.as_ref()));
}
log::info!("Fetching {}", http_request.url);
let mut response = if let Some(body) = http_request.body {
let val = body.get();
// The body can be either json, or a string representing a raw body
let body = if val.starts_with('"') {
serde_json::from_str::<'_, String>(val)?
} else {
req = req.content_type("application/json");
val.to_owned()
};
req.send_body(body)
} else {
req.send()
}
.await
.map_err(|e| anyhow!("Unable to fetch {}: {e}", http_request.url))?;
log::debug!(
"Finished fetching {}. Status: {}",
http_request.url,
response.status()
);
let body = response.body().await?.to_vec();
let response_str = String::from_utf8(body)?;
log::debug!("Fetch response: {response_str}");
Ok(response_str)
}
pub(crate) async fn hash_password(password: String) -> anyhow::Result<String> {
actix_web::rt::task::spawn_blocking(move || {
// Hashes a password using Argon2. This is a CPU-intensive blocking operation.
let phf = argon2::Argon2::default();
let salt = password_hash::SaltString::generate(&mut password_hash::rand_core::OsRng);
let password_hash = &password_hash::PasswordHash::generate(phf, password, &salt)
.map_err(|e| anyhow!("Unable to hash password: {}", e))?;
Ok(password_hash.to_string())
})
.await?
}
async fn header<'a>(request: &'a RequestInfo, name: Cow<'a, str>) -> Option<Cow<'a, str>> {
request.headers.get(&*name).map(SingleOrVec::as_json_str)
}
/// Returns the path component of the URL of the current request.
async fn path(request: &RequestInfo) -> &str {
&request.path
}
const DEFAULT_ALLOWED_EXTENSIONS: &str =
"jpg,jpeg,png,gif,bmp,webp,pdf,txt,doc,docx,xls,xlsx,csv,mp3,mp4,wav,avi,mov";
async fn persist_uploaded_file<'a>(
request: &'a RequestInfo,
field_name: Cow<'a, str>,
folder: Option<Cow<'a, str>>,
allowed_extensions: Option<Cow<'a, str>>,
) -> anyhow::Result<String> {
let folder = folder.unwrap_or(Cow::Borrowed("uploads"));
let allowed_extensions_str =
allowed_extensions.unwrap_or(Cow::Borrowed(DEFAULT_ALLOWED_EXTENSIONS));
let allowed_extensions = allowed_extensions_str.split(',');
let uploaded_file = request
.uploaded_files
.get(&field_name.to_string())
.ok_or_else(|| {
anyhow!(
"no file uploaded with field name {field_name}. Uploaded files: {:?}",
request.uploaded_files.keys()
)
})?;
let file_name = uploaded_file.file_name.as_deref().unwrap_or_default();
let extension = file_name.split('.').last().unwrap_or_default();
if !allowed_extensions
.clone()
.any(|x| x.eq_ignore_ascii_case(extension))
{
let exts = allowed_extensions.collect::<Vec<_>>().join(", ");
anyhow::bail!("file extension {extension} is not allowed. Allowed extensions: {exts}");
}
// resolve the folder path relative to the web root
let web_root = &request.app_state.config.web_root;
let target_folder = web_root.join(&*folder);
// create the folder if it doesn't exist
tokio::fs::create_dir_all(&target_folder)
.await
.with_context(|| format!("unable to create folder {target_folder:?}"))?;
let date = chrono::Utc::now().format("%Y-%m-%d %Hh%Mm%Ss");
let random_part = random_string_sync(8);
let random_target_name = format!("{date} {random_part}.{extension}");
let target_path = target_folder.join(&random_target_name);
tokio::fs::copy(&uploaded_file.file.path(), &target_path)
.await
.with_context(|| {
format!("unable to copy uploaded file {field_name:?} to {target_path:?}")
})?;
// remove the WEB_ROOT prefix from the path, but keep the leading slash
let path = "/".to_string()
+ target_path
.strip_prefix(web_root)?
.to_str()
.with_context(|| format!("unable to convert path {target_path:?} to a string"))?;
Ok(path)
}
/// Returns the protocol of the current request (http or https).
async fn protocol(request: &RequestInfo) -> &str {
&request.protocol
}
/// Returns a random string of the specified length.
pub(crate) async fn random_string(len: usize) -> anyhow::Result<String> {
// OsRng can block on Linux, so we run this on a blocking thread.
Ok(tokio::task::spawn_blocking(move || random_string_sync(len)).await?)
}
/// Returns a random string of the specified length.
pub(crate) fn random_string_sync(len: usize) -> String {
use rand::{distributions::Alphanumeric, Rng};
password_hash::rand_core::OsRng
.sample_iter(&Alphanumeric)
.take(len)
.map(char::from)
.collect()
}
#[tokio::test]
async fn test_random_string() {
let s = random_string(10).await.unwrap();
assert_eq!(s.len(), 10);
}
async fn read_file_bytes<'a>(
request: &'a RequestInfo,
path_str: &str,
) -> Result<Vec<u8>, anyhow::Error> {
let path = std::path::Path::new(path_str);
// If the path is relative, it's relative to the web root, not the current working directory,
// and it can be fetched from the on-database filesystem table
if path.is_relative() {
request
.app_state
.file_system
.read_file(&request.app_state, path, true)
.await
} else {
tokio::fs::read(path)
.await
.with_context(|| format!("Unable to read file {path:?}"))
}
}
async fn read_file_as_data_url<'a>(
request: &'a RequestInfo,
file_path: Option<Cow<'a, str>>,
) -> Result<Option<Cow<'a, str>>, anyhow::Error> {
let Some(file_path) = file_path else {
log::debug!("read_file: first argument is NULL, returning NULL");
return Ok(None);
};
let bytes = read_file_bytes(request, &file_path).await?;
let mime = mime_from_upload_path(request, &file_path).map_or_else(
|| Cow::Owned(mime_guess_from_filename(&file_path)),
Cow::Borrowed,
);
let mut data_url = format!("data:{}/{};base64,", mime.type_(), mime.subtype());
base64::Engine::encode_string(
&base64::engine::general_purpose::STANDARD,
bytes,
&mut data_url,
);
Ok(Some(Cow::Owned(data_url)))
}
/// Returns the contents of a file as a string
async fn read_file_as_text<'a>(
request: &'a RequestInfo,
file_path: Option<Cow<'a, str>>,
) -> Result<Option<Cow<'a, str>>, anyhow::Error> {
let Some(file_path) = file_path else {
log::debug!("read_file: first argument is NULL, returning NULL");
return Ok(None);
};
let bytes = read_file_bytes(request, &file_path).await?;
let as_str = String::from_utf8(bytes).with_context(|| {
format!("read_file_as_text: {file_path} does not contain raw UTF8 text")
})?;
Ok(Some(Cow::Owned(as_str)))
}
fn mime_from_upload_path<'a>(request: &'a RequestInfo, path: &str) -> Option<&'a mime_guess::Mime> {
request.uploaded_files.values().find_map(|uploaded_file| {
if uploaded_file.file.path() == OsStr::new(path) {
uploaded_file.content_type.as_ref()
} else {
None
}
})
}
fn mime_guess_from_filename(filename: &str) -> mime_guess::Mime {
let maybe_mime = mime_guess::from_path(filename).first();
maybe_mime.unwrap_or(mime_guess::mime::APPLICATION_OCTET_STREAM)
}
async fn run_sql<'a>(
request: &'a RequestInfo,
sql_file_path: Option<Cow<'a, str>>,
) -> anyhow::Result<Option<Cow<'a, str>>> {
use serde::ser::{SerializeSeq, Serializer};
let Some(sql_file_path) = sql_file_path else {
log::debug!("run_sql: first argument is NULL, returning NULL");
return Ok(None);
};
let sql_file = request
.app_state
.sql_file_cache
.get_with_privilege(
&request.app_state,
std::path::Path::new(sql_file_path.as_ref()),
true,
)
.await
.with_context(|| format!("run_sql: invalid path {sql_file_path:?}"))?;
let mut tmp_req = request.clone();
if tmp_req.clone_depth > 8 {
anyhow::bail!("Too many nested inclusions. run_sql can include a file that includes another file, but the depth is limited to 8 levels. \n\
Executing sqlpage.run_sql('{sql_file_path}') would exceed this limit. \n\
This is to prevent infinite loops and stack overflows.\n\
Make sure that your SQL file does not try to run itself, directly or through a chain of other files.");
}
let mut results_stream =
crate::webserver::database::execute_queries::stream_query_results_boxed(
&request.app_state.db,
&sql_file,
&mut tmp_req,
);
let mut json_results_bytes = Vec::new();
let mut json_encoder = serde_json::Serializer::new(&mut json_results_bytes);
let mut seq = json_encoder.serialize_seq(None)?;
while let Some(db_item) = results_stream.next().await {
use crate::webserver::database::DbItem::{Error, FinishedQuery, Row};
match db_item {
Row(row) => {
log::debug!("run_sql: row: {:?}", row);
seq.serialize_element(&row)?;
}
FinishedQuery => log::trace!("run_sql: Finished query"),
Error(err) => {
return Err(err.context(format!("run_sql: unable to run {sql_file_path:?}")))
}
}
}
seq.end()?;
Ok(Some(Cow::Owned(String::from_utf8(json_results_bytes)?)))
}
#[tokio::test]
async fn test_hash_password() {
let s = hash_password("password".to_string()).await.unwrap();
assert!(s.starts_with("$argon2"));
}
async fn uploaded_file_mime_type<'a>(
request: &'a RequestInfo,
upload_name: Cow<'a, str>,
) -> Option<Cow<'a, str>> {
let mime = request
.uploaded_files
.get(&*upload_name)?
.content_type
.as_ref()?;
Some(Cow::Borrowed(mime.as_ref()))
}
async fn uploaded_file_path<'a>(
request: &'a RequestInfo,
upload_name: Cow<'a, str>,
) -> Option<Cow<'a, str>> {
let uploaded_file = request.uploaded_files.get(&*upload_name)?;
Some(uploaded_file.file.path().to_string_lossy())
}
/// escapes a string for use in a URL using percent encoding
/// for example, spaces are replaced with %20, '/' with %2F, etc.
/// This is useful for constructing URLs in SQL queries.
/// If this function is passed a NULL value, it will return NULL (None in Rust),
/// rather than an empty string or an error.
async fn url_encode(raw_text: Option<Cow<'_, str>>) -> Option<Cow<'_, str>> {
Some(match raw_text? {
Cow::Borrowed(inner) => {
let encoded = percent_encoding::percent_encode(
inner.as_bytes(),
percent_encoding::NON_ALPHANUMERIC,
);
encoded.into()
}
Cow::Owned(inner) => {
let encoded = percent_encoding::percent_encode(
inner.as_bytes(),
percent_encoding::NON_ALPHANUMERIC,
);
Cow::Owned(encoded.collect())
}
})
}
/// Returns all variables in the request as a JSON object.
async fn variables<'a>(
request: &'a RequestInfo,
get_or_post: Option<Cow<'a, str>>,
) -> anyhow::Result<String> {
Ok(if let Some(get_or_post) = get_or_post {
if get_or_post.eq_ignore_ascii_case("get") {
serde_json::to_string(&request.get_variables)?
} else if get_or_post.eq_ignore_ascii_case("post") {
serde_json::to_string(&request.post_variables)?
} else {
return Err(anyhow!(
"Expected 'get' or 'post' as the argument to sqlpage.all_variables"
));
}
} else {
use serde::{ser::SerializeMap, Serializer};
let mut res = Vec::new();
let mut serializer = serde_json::Serializer::new(&mut res);
let len = request.get_variables.len() + request.post_variables.len();
let mut ser = serializer.serialize_map(Some(len))?;
let iter = request.get_variables.iter().chain(&request.post_variables);
for (k, v) in iter {
ser.serialize_entry(k, v)?;
}
ser.end()?;
String::from_utf8(res)?
})
}
/// Returns the version of the sqlpage that is running.
async fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}