Move the bad passwords code into the server module since the relay doesn't need it

main
_ 2020-11-26 23:41:32 +00:00
parent 7c2ce65864
commit c4108f6f2f
5 changed files with 41 additions and 43 deletions

View File

@ -26,19 +26,5 @@ pub fn prefix_match <'a> (prefix: &str, hay: &'a str) -> Option <&'a str>
} }
} }
// Thanks to https://github.com/robsheldon/bad-passwords-index
const BAD_PASSWORDS: &[u8] = include_bytes! ("bad_passwords.txt");
pub fn password_is_bad (mut password: String) -> bool {
password.make_ascii_lowercase ();
let ac = aho_corasick::AhoCorasick::new (&[
password
]);
ac.find (BAD_PASSWORDS).is_some ()
}
#[cfg (test)] #[cfg (test)]
mod tests; mod tests;

View File

@ -842,7 +842,7 @@ mod tests {
("/files/?", InvalidQuery), ("/files/?", InvalidQuery),
("/files/src", Redirect ("src/".to_string ())), ("/files/src", Redirect ("src/".to_string ())),
("/files/src/?", InvalidQuery), ("/files/src/?", InvalidQuery),
("/files/src/bad_passwords.txt", ServeFile (ServeFileParams { ("/files/src/server/bad_passwords.txt", ServeFile (ServeFileParams {
send_body: true, send_body: true,
range: 0..1_048_576, range: 0..1_048_576,
range_requested: false, range_requested: false,
@ -870,7 +870,7 @@ mod tests {
let resp = internal_serve_all ( let resp = internal_serve_all (
&file_server_root, &file_server_root,
Method::Get, Method::Get,
"/files/src/bad_passwords.txt", "/files/src/server/bad_passwords.txt",
&hashmap! { &hashmap! {
"range".into () => b"bytes=0-2000000".to_vec (), "range".into () => b"bytes=0-2000000".to_vec (),
}, },
@ -882,7 +882,7 @@ mod tests {
let resp = internal_serve_all ( let resp = internal_serve_all (
&file_server_root, &file_server_root,
Method::Head, Method::Head,
"/files/src/bad_passwords.txt", "/files/src/server/bad_passwords.txt",
&headers, &headers,
None None
).await; ).await;

View File

@ -24,6 +24,20 @@ use crate::{
pub mod file_server; pub mod file_server;
// Thanks to https://github.com/robsheldon/bad-passwords-index
const BAD_PASSWORDS: &[u8] = include_bytes! ("bad_passwords.txt");
pub fn password_is_bad (mut password: String) -> bool {
password.make_ascii_lowercase ();
let ac = aho_corasick::AhoCorasick::new (&[
password
]);
ac.find (BAD_PASSWORDS).is_some ()
}
struct ServerState { struct ServerState {
config: Config, config: Config,
handlebars: Handlebars <'static>, handlebars: Handlebars <'static>,
@ -143,7 +157,7 @@ pub async fn run_server (
use std::convert::TryInto; use std::convert::TryInto;
if crate::password_is_bad (config_file.api_key.clone ()) { if password_is_bad (config_file.api_key.clone ()) {
panic! ("API key is too weak, server can't use it"); panic! ("API key is too weak, server can't use it");
} }
@ -275,4 +289,27 @@ mod tests {
assert_eq! (config.tripcode (), "A9rPwZyY89Ag4TJjMoyYA2NeGOm99Je6rq1s0rg8PfY=".to_string ()); assert_eq! (config.tripcode (), "A9rPwZyY89Ag4TJjMoyYA2NeGOm99Je6rq1s0rg8PfY=".to_string ());
} }
#[test]
fn check_bad_passwords () {
for pw in &[
"",
" ",
"user",
"password",
"pAsSwOrD",
"secret",
"123123",
] {
assert! (password_is_bad (pw.to_string ()));
}
use rand::prelude::*;
let mut entropy = [0u8; 32];
thread_rng ().fill_bytes (&mut entropy);
let good_password = base64::encode (entropy);
assert! (! password_is_bad (good_password));
}
} }

View File

@ -18,31 +18,6 @@ use super::{
server, server,
}; };
#[test]
fn check_bad_passwords () {
use crate::password_is_bad;
for pw in &[
"",
" ",
"user",
"password",
"pAsSwOrD",
"secret",
"123123",
] {
assert! (password_is_bad (pw.to_string ()));
}
use rand::prelude::*;
let mut entropy = [0u8; 32];
thread_rng ().fill_bytes (&mut entropy);
let good_password = base64::encode (entropy);
assert! (! password_is_bad (good_password));
}
#[test] #[test]
fn end_to_end () { fn end_to_end () {
use maplit::*; use maplit::*;