[core] V0.1: parse, compose, reply/forward, safe names, threading, rules + Sieve, sanitiser, search (55 tests green)
This commit is contained in:
+7
-4
@@ -144,7 +144,11 @@ fn body_text() -> impl Strategy<Value = String> {
|
||||
fn attachment() -> impl Strategy<Value = OutAttachment> {
|
||||
(
|
||||
"[A-Za-z0-9_-]{1,20}",
|
||||
prop_oneof![Just(("pdf", "application/pdf")), Just(("png", "image/png")), Just(("bin", "application/octet-stream"))],
|
||||
prop_oneof![
|
||||
Just(("pdf", "application/pdf")),
|
||||
Just(("png", "image/png")),
|
||||
Just(("bin", "application/octet-stream"))
|
||||
],
|
||||
proptest::collection::vec(any::<u8>(), 0..3000),
|
||||
)
|
||||
.prop_map(|(stem, (ext, ct), data)| OutAttachment {
|
||||
@@ -165,9 +169,8 @@ fn draft() -> impl Strategy<Value = Draft> {
|
||||
proptest::collection::vec(attachment(), 0..3),
|
||||
)
|
||||
.prop_map(|(from, to, cc, subject, text, with_html, attachments)| {
|
||||
let html = with_html.then(|| {
|
||||
format!("<p>{}</p>", text.replace('&', "&").replace('<', "<").replace('\n', "<br>"))
|
||||
});
|
||||
let html = with_html
|
||||
.then(|| format!("<p>{}</p>", text.replace('&', "&").replace('<', "<").replace('\n', "<br>")));
|
||||
Draft { from, to, cc, subject, text, html, attachments, ..Default::default() }
|
||||
})
|
||||
}
|
||||
|
||||
+8
-14
@@ -2,8 +2,8 @@
|
||||
|
||||
use base64::Engine;
|
||||
use epistula_core::mime::{MAX_MESSAGE_BYTES, ParseError, parse};
|
||||
use epistula_core::sanitize::{InlineImage, SanitizeOptions, sanitize};
|
||||
use epistula_core::safe_filename;
|
||||
use epistula_core::sanitize::{InlineImage, SanitizeOptions, sanitize};
|
||||
use proptest::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -31,8 +31,8 @@ fn noise(len: usize, mut seed: u64) -> Vec<u8> {
|
||||
}
|
||||
|
||||
const PNG_1X1: &[u8] = &[
|
||||
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0, 0, 0, 0x0D, 0x49, 0x48, 0x44, 0x52, 0, 0, 0, 1, 0, 0,
|
||||
0, 1, 8, 6, 0, 0, 0, 0x1F, 0x15, 0xC4, 0x89,
|
||||
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0, 0, 0, 0x0D, 0x49, 0x48, 0x44, 0x52, 0, 0, 0, 1, 0, 0, 0, 1, 8,
|
||||
6, 0, 0, 0, 0x1F, 0x15, 0xC4, 0x89,
|
||||
];
|
||||
|
||||
fn nested_fixture() -> Vec<u8> {
|
||||
@@ -120,10 +120,8 @@ fn inline_cid_image_becomes_a_data_uri_in_the_reader() {
|
||||
inline.content_id.clone().unwrap(),
|
||||
InlineImage { content_type: &inline.content_type, data: &inline.data },
|
||||
);
|
||||
let out = sanitize(
|
||||
m.html.as_deref().unwrap(),
|
||||
&SanitizeOptions { load_remote_images: false, inline_images: images },
|
||||
);
|
||||
let out =
|
||||
sanitize(m.html.as_deref().unwrap(), &SanitizeOptions { load_remote_images: false, inline_images: images });
|
||||
assert!(out.html.contains("data:image/png;base64,"), "{}", out.html);
|
||||
assert!(!out.html.contains("cid"), "{}", out.html);
|
||||
assert_eq!(out.blocked_images, 0);
|
||||
@@ -153,7 +151,8 @@ fn broken_charsets_do_not_panic_and_keep_text() {
|
||||
let m = parse(unknown).unwrap();
|
||||
assert!(m.text.unwrap().contains("ok"));
|
||||
|
||||
let invalid_utf8 = b"Subject: =?UTF-8?B?/w==?=\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nbad \xFF\xFE bytes\r\n";
|
||||
let invalid_utf8 =
|
||||
b"Subject: =?UTF-8?B?/w==?=\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nbad \xFF\xFE bytes\r\n";
|
||||
let m = parse(invalid_utf8).unwrap();
|
||||
assert!(m.text.unwrap().contains("bytes"));
|
||||
|
||||
@@ -215,12 +214,7 @@ fn two_hundred_kilobyte_header_block() {
|
||||
|
||||
#[test]
|
||||
fn hostile_filenames_in_messages_are_made_safe() {
|
||||
let names = [
|
||||
"../../../etc/passwd",
|
||||
"..\\..\\Windows\\win.ini",
|
||||
"CON.txt",
|
||||
"invoice\u{202E}fdp.exe",
|
||||
];
|
||||
let names = ["../../../etc/passwd", "..\\..\\Windows\\win.ini", "CON.txt", "invoice\u{202E}fdp.exe"];
|
||||
let mut raw = String::from("Subject: files\r\nContent-Type: multipart/mixed; boundary=f\r\n\r\n");
|
||||
for name in names {
|
||||
raw.push_str(&format!(
|
||||
|
||||
@@ -7,3 +7,5 @@
|
||||
cc 4e10d11e138d641f5cd862bf9d0328534293c16602b25811c13e17f483d0c012 # shrinks to parts = [], load = false
|
||||
cc 9298836d4e0c985227afc1ac30519c2685a4e3d945870a0e9f17cc9e627b725c # shrinks to parts = []
|
||||
cc 04d94bdb6810dae2da78cbe7203c4f7076050d7986ba6572441864111c45a79e # shrinks to text = ""
|
||||
cc b2079ba7c7b64a2e3b52515689e13d44fb7a5dfb8f1b4e0bb7f24d35fc4cfb28 # shrinks to parts = ["<p title=\"", "javascript:", "<div style=\"", "<script>"]
|
||||
cc c1fdc436d014d409bf600850adb48e9a7d8f2528f1a3c68f69569840c4dd81f1 # shrinks to parts = ["<p title=\"", "javascript:", "<p title=\"", "<script>"], load = false
|
||||
|
||||
+93
-10
@@ -9,11 +9,47 @@ fn clean(html: &str) -> String {
|
||||
|
||||
/// Anything executable, anything that loads something, anything that navigates by itself.
|
||||
const FORBIDDEN: &[&str] = &[
|
||||
"<script", "javascript:", "vbscript:", "onerror", "onload", "onclick", "onmouseover", "onfocus",
|
||||
"onbegin", "ontoggle", "expression(", "<form", "<input", "<button", "<textarea", "<select",
|
||||
"<meta", "http-equiv", "refresh", "<iframe", "<frame", "<object", "<embed", "<applet", "<base",
|
||||
"<link", "<style", "@import", "url(", "behavior:", "-moz-binding", "<svg", "<math", "srcdoc",
|
||||
"formaction", "data:text/html", "evil.example", "<template", "<noscript", "<xml", "xlink:href",
|
||||
"<script",
|
||||
"javascript:",
|
||||
"vbscript:",
|
||||
"onerror",
|
||||
"onload",
|
||||
"onclick",
|
||||
"onmouseover",
|
||||
"onfocus",
|
||||
"onbegin",
|
||||
"ontoggle",
|
||||
"expression(",
|
||||
"<form",
|
||||
"<input",
|
||||
"<button",
|
||||
"<textarea",
|
||||
"<select",
|
||||
"<meta",
|
||||
"http-equiv",
|
||||
"refresh",
|
||||
"<iframe",
|
||||
"<frame",
|
||||
"<object",
|
||||
"<embed",
|
||||
"<applet",
|
||||
"<base",
|
||||
"<link",
|
||||
"<style",
|
||||
"@import",
|
||||
"url(",
|
||||
"behavior:",
|
||||
"-moz-binding",
|
||||
"<svg",
|
||||
"<math",
|
||||
"srcdoc",
|
||||
"formaction",
|
||||
"data:text/html",
|
||||
"evil.example",
|
||||
"<template",
|
||||
"<noscript",
|
||||
"<xml",
|
||||
"xlink:href",
|
||||
];
|
||||
|
||||
const CORPUS: &[&str] = &[
|
||||
@@ -98,13 +134,22 @@ fn tags(out: &str) -> Vec<(String, Vec<(String, String)>)> {
|
||||
let mut end = rest.len();
|
||||
loop {
|
||||
while let Some(&(_, c)) = chars.peek() {
|
||||
if c.is_whitespace() || c == '/' { chars.next(); } else { break; }
|
||||
if c.is_whitespace() || c == '/' {
|
||||
chars.next();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let Some(&(i, c)) = chars.peek() else { break };
|
||||
if c == '>' { end = i + 1; break; }
|
||||
if c == '>' {
|
||||
end = i + 1;
|
||||
break;
|
||||
}
|
||||
let mut attr = String::new();
|
||||
while let Some(&(_, c)) = chars.peek() {
|
||||
if c == '=' || c == '>' || c.is_whitespace() { break; }
|
||||
if c == '=' || c == '>' || c.is_whitespace() {
|
||||
break;
|
||||
}
|
||||
attr.push(c);
|
||||
chars.next();
|
||||
}
|
||||
@@ -114,7 +159,9 @@ fn tags(out: &str) -> Vec<(String, Vec<(String, String)>)> {
|
||||
if let Some(&(_, '"')) = chars.peek() {
|
||||
chars.next();
|
||||
for (_, c) in chars.by_ref() {
|
||||
if c == '"' { break; }
|
||||
if c == '"' {
|
||||
break;
|
||||
}
|
||||
value.push(c);
|
||||
}
|
||||
}
|
||||
@@ -140,9 +187,45 @@ fn dangerous(out: &str) -> Option<String> {
|
||||
}
|
||||
let compact: String = value.chars().filter(|c| !c.is_whitespace() && !c.is_control()).collect();
|
||||
let compact = compact.to_ascii_lowercase();
|
||||
if compact.starts_with("javascript:") || compact.starts_with("vbscript:") || compact.starts_with("data:text") {
|
||||
let url_attribute = matches!(
|
||||
attr.as_str(),
|
||||
"href"
|
||||
| "src"
|
||||
| "action"
|
||||
| "formaction"
|
||||
| "background"
|
||||
| "poster"
|
||||
| "data"
|
||||
| "srcset"
|
||||
| "lowsrc"
|
||||
| "dynsrc"
|
||||
| "cite"
|
||||
| "xlink:href"
|
||||
| "longdesc"
|
||||
| "usemap"
|
||||
| "codebase"
|
||||
);
|
||||
if url_attribute
|
||||
&& (compact.starts_with("javascript:")
|
||||
|| compact.starts_with("vbscript:")
|
||||
|| compact.starts_with("data:text"))
|
||||
{
|
||||
return Some(format!("url in {attr}"));
|
||||
}
|
||||
if attr == "style"
|
||||
&& ["expression", "url(", "javascript", "@import", "behavior", "binding"]
|
||||
.iter()
|
||||
.any(|b| compact.contains(b))
|
||||
{
|
||||
return Some("css".into());
|
||||
}
|
||||
if url_attribute
|
||||
&& attr != "href"
|
||||
&& (compact.starts_with("http:") || compact.starts_with("https:"))
|
||||
&& !compact.contains("cdn.example")
|
||||
{
|
||||
return Some(format!("remote load in {attr}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
use epistula_core::rules::RuleError;
|
||||
use epistula_core::search::FTS_COLUMNS;
|
||||
use epistula_core::{
|
||||
Address, Condition, Field, Rule, RuleMessage, ThreadInput, first_match, normalize_subject, parse_query,
|
||||
thread, to_sieve,
|
||||
Address, Condition, Field, Rule, RuleMessage, ThreadInput, first_match, normalize_subject, parse_query, thread,
|
||||
to_sieve,
|
||||
};
|
||||
use proptest::prelude::*;
|
||||
|
||||
@@ -54,10 +54,8 @@ fn references_join_across_a_missing_message() {
|
||||
|
||||
#[test]
|
||||
fn a_reply_that_arrives_before_its_parent_still_joins() {
|
||||
let t = thread(&[
|
||||
msg(7, Some("b@x"), &["a@x"], &["a@x"], "Re: Plan", 200),
|
||||
msg(9, Some("a@x"), &[], &[], "Plan", 100),
|
||||
]);
|
||||
let t =
|
||||
thread(&[msg(7, Some("b@x"), &["a@x"], &["a@x"], "Re: Plan", 200), msg(9, Some("a@x"), &[], &[], "Plan", 100)]);
|
||||
assert_eq!(t, vec![9, 9], "thread id = earliest message by date");
|
||||
}
|
||||
|
||||
@@ -131,10 +129,8 @@ fn rule(name: &str, conditions: &[(Field, &str)], folder: &str) -> Rule {
|
||||
|
||||
#[test]
|
||||
fn first_match_wins() {
|
||||
let rules = vec![
|
||||
rule("bank", &[(Field::From, "@bank.cz")], "Bank"),
|
||||
rule("all-from-cz", &[(Field::From, ".cz")], "Czech"),
|
||||
];
|
||||
let rules =
|
||||
vec![rule("bank", &[(Field::From, "@bank.cz")], "Bank"), rule("all-from-cz", &[(Field::From, ".cz")], "Czech")];
|
||||
let from = [addr(None, "info@bank.cz")];
|
||||
let m = RuleMessage { from: &from, to: &[], cc: &[], subject: "Výpis" };
|
||||
assert_eq!(first_match(&rules, &m).unwrap().folder, "Bank");
|
||||
@@ -261,7 +257,14 @@ fn search_finds_diacritics_insensitively() {
|
||||
FTS_COLUMNS.join(", ")
|
||||
))
|
||||
.unwrap();
|
||||
for (query, hits) in [("vylet", 1), ("from:anna", 1), ("from:anna@example.com", 1), ("subject:sobot", 1), ("to:anna", 0), ("mapa", 1)] {
|
||||
for (query, hits) in [
|
||||
("vylet", 1),
|
||||
("from:anna", 1),
|
||||
("from:anna@example.com", 1),
|
||||
("subject:sobot", 1),
|
||||
("to:anna", 0),
|
||||
("mapa", 1),
|
||||
] {
|
||||
let expr = parse_query(query).fts_match().unwrap();
|
||||
let n: i64 = db.query_row("SELECT count(*) FROM s WHERE s MATCH ?1", [&expr], |r| r.get(0)).unwrap();
|
||||
assert_eq!(n, hits, "{query} -> {expr}");
|
||||
|
||||
Reference in New Issue
Block a user