瀏覽代碼

Prevent XSS

master
Dylan Baker 5 年之前
父節點
當前提交
7d3fc1ef4d
共有 4 個檔案被更改,包括 18 行新增3 行删除
  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 查看文件

343
  "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
343
  "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
344
  "diesel 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
344
  "diesel 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
345
  "dotenv 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
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
  "r2d2 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
347
  "r2d2 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
347
  "r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
348
  "r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
348
  "rocket 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
349
  "rocket 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
358
 version = "0.2.11"
359
 version = "0.2.11"
359
 source = "registry+https://github.com/rust-lang/crates.io-index"
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
 [[package]]
367
 [[package]]
362
 name = "httparse"
368
 name = "httparse"
363
 version = "1.3.3"
369
 version = "1.3.3"
1516
 "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
1522
 "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
1517
 "checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592"
1523
 "checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592"
1518
 "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
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
 "checksum httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e8734b0cfd3bc3e101ec59100e101c2eecd19282202e87808b3037b442777a83"
1526
 "checksum httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e8734b0cfd3bc3e101ec59100e101c2eecd19282202e87808b3037b442777a83"
1520
 "checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e"
1527
 "checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e"
1521
 "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114"
1528
 "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114"

+ 1
- 0
Cargo.toml 查看文件

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

+ 1
- 0
src/main.rs 查看文件

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

+ 9
- 3
src/snippet.rs 查看文件

35
             ps.find_syntax_by_extension(&filetype).map_or(
35
             ps.find_syntax_by_extension(&filetype).map_or(
36
                 format!("<pre class='plaintext'>{}</pre>", body),
36
                 format!("<pre class='plaintext'>{}</pre>", body),
37
                 |syntax| {
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
 }
52
 }
48
 
53
 
49
 pub fn insert(snippet: InsertableSnippet, connection: &PGC) -> QueryResult<Snippet> {
54
 pub fn insert(snippet: InsertableSnippet, connection: &PGC) -> QueryResult<Snippet> {
55
+    let body = htmlescape::encode_minimal(&snippet.body.clone());
50
     let formatted_snippet = InsertableSnippet {
56
     let formatted_snippet = InsertableSnippet {
51
         filetype: snippet.filetype.clone(),
57
         filetype: snippet.filetype.clone(),
52
         title: snippet.title,
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
     diesel::insert_into(snippets::table)
62
     diesel::insert_into(snippets::table)
57
         .values(formatted_snippet)
63
         .values(formatted_snippet)

Loading…
取消
儲存