Browse Source

Prevent XSS

master
Dylan Baker 5 years ago
parent
commit
7d3fc1ef4d
4 changed files with 18 additions and 3 deletions
  1. 7
    0
      Cargo.lock
  2. 1
    0
      Cargo.toml
  3. 1
    0
      src/main.rs
  4. 9
    3
      src/snippet.rs

+ 7
- 0
Cargo.lock View File

@@ -343,6 +343,7 @@ dependencies = [
343 343
  "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
344 344
  "diesel 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
345 345
  "dotenv 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
346
+ "htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
346 347
  "r2d2 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
347 348
  "r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
348 349
  "rocket 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -358,6 +359,11 @@ name = "glob"
358 359
 version = "0.2.11"
359 360
 source = "registry+https://github.com/rust-lang/crates.io-index"
360 361
 
362
+[[package]]
363
+name = "htmlescape"
364
+version = "0.3.1"
365
+source = "registry+https://github.com/rust-lang/crates.io-index"
366
+
361 367
 [[package]]
362 368
 name = "httparse"
363 369
 version = "1.3.3"
@@ -1516,6 +1522,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1516 1522
 "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
1517 1523
 "checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592"
1518 1524
 "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
1525
+"checksum htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e9025058dae765dee5070ec375f591e2ba14638c63feff74f13805a72e523163"
1519 1526
 "checksum httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e8734b0cfd3bc3e101ec59100e101c2eecd19282202e87808b3037b442777a83"
1520 1527
 "checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e"
1521 1528
 "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114"

+ 1
- 0
Cargo.toml View File

@@ -8,6 +8,7 @@ edition = "2018"
8 8
 chrono = { version = "0.4.6", features = ["serde"] }
9 9
 diesel = { version = "1.0.0", features = ["postgres", "chrono"] }
10 10
 dotenv = "0.9.0"
11
+htmlescape = "0.3.1"
11 12
 r2d2 = "0.8.3"
12 13
 r2d2-diesel = "1.0.0"
13 14
 rocket = "0.4.0"

+ 1
- 0
src/main.rs View File

@@ -4,6 +4,7 @@ extern crate chrono;
4 4
 #[macro_use]
5 5
 extern crate diesel;
6 6
 extern crate dotenv;
7
+extern crate htmlescape;
7 8
 extern crate r2d2;
8 9
 extern crate r2d2_diesel;
9 10
 #[macro_use]

+ 9
- 3
src/snippet.rs View File

@@ -35,7 +35,12 @@ fn format_snippet(filetype: Option<String>, body: String) -> String {
35 35
             ps.find_syntax_by_extension(&filetype).map_or(
36 36
                 format!("<pre class='plaintext'>{}</pre>", body),
37 37
                 |syntax| {
38
-                    highlighted_html_for_string(&body, &ps, syntax, &ts.themes["Solarized (light)"])
38
+                    highlighted_html_for_string(
39
+                        &htmlescape::decode_html(&body).expect("Invalid HTML"),
40
+                        &ps,
41
+                        syntax,
42
+                        &ts.themes["Solarized (light)"],
43
+                    )
39 44
                 },
40 45
             )
41 46
         },
@@ -47,11 +52,12 @@ pub fn get(connection: &PGC, id: i32) -> QueryResult<Snippet> {
47 52
 }
48 53
 
49 54
 pub fn insert(snippet: InsertableSnippet, connection: &PGC) -> QueryResult<Snippet> {
55
+    let body = htmlescape::encode_minimal(&snippet.body.clone());
50 56
     let formatted_snippet = InsertableSnippet {
51 57
         filetype: snippet.filetype.clone(),
52 58
         title: snippet.title,
53
-        body: snippet.body.clone(),
54
-        formatted_body: format_snippet(snippet.filetype, snippet.body),
59
+        body: body.clone(),
60
+        formatted_body: format_snippet(snippet.filetype, body),
55 61
     };
56 62
     diesel::insert_into(snippets::table)
57 63
         .values(formatted_snippet)

Loading…
Cancel
Save