Move the bad passwords code into the server module since the relay doesn't need it
parent
7c2ce65864
commit
c4108f6f2f
14
src/lib.rs
14
src/lib.rs
|
@ -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)]
|
||||
mod tests;
|
||||
|
|
|
@ -842,7 +842,7 @@ mod tests {
|
|||
("/files/?", InvalidQuery),
|
||||
("/files/src", Redirect ("src/".to_string ())),
|
||||
("/files/src/?", InvalidQuery),
|
||||
("/files/src/bad_passwords.txt", ServeFile (ServeFileParams {
|
||||
("/files/src/server/bad_passwords.txt", ServeFile (ServeFileParams {
|
||||
send_body: true,
|
||||
range: 0..1_048_576,
|
||||
range_requested: false,
|
||||
|
@ -870,7 +870,7 @@ mod tests {
|
|||
let resp = internal_serve_all (
|
||||
&file_server_root,
|
||||
Method::Get,
|
||||
"/files/src/bad_passwords.txt",
|
||||
"/files/src/server/bad_passwords.txt",
|
||||
&hashmap! {
|
||||
"range".into () => b"bytes=0-2000000".to_vec (),
|
||||
},
|
||||
|
@ -882,7 +882,7 @@ mod tests {
|
|||
let resp = internal_serve_all (
|
||||
&file_server_root,
|
||||
Method::Head,
|
||||
"/files/src/bad_passwords.txt",
|
||||
"/files/src/server/bad_passwords.txt",
|
||||
&headers,
|
||||
None
|
||||
).await;
|
||||
|
|
|
@ -24,6 +24,20 @@ use crate::{
|
|||
|
||||
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 {
|
||||
config: Config,
|
||||
handlebars: Handlebars <'static>,
|
||||
|
@ -143,7 +157,7 @@ pub async fn run_server (
|
|||
|
||||
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");
|
||||
}
|
||||
|
||||
|
@ -275,4 +289,27 @@ mod tests {
|
|||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
25
src/tests.rs
25
src/tests.rs
|
@ -18,31 +18,6 @@ use super::{
|
|||
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]
|
||||
fn end_to_end () {
|
||||
use maplit::*;
|
||||
|
|
Loading…
Reference in New Issue