The rule lives on the control, and punctuation is not a rule
Browse files at this commit78c0728ec06724051ca96a95cb6a60bedd12dee5Parent: 2258902- test
- the rule lives on the control, and punctuation is not a rule
4 changed files +60 -2
- crates/izlek-core/src/auth.rs +8 -1
- crates/izlek-web/src/pages.rs +2 -0
- crates/izlek-web/src/settings.rs +1 -1
- crates/izlek-web/tests/http.rs +49 -0
crates/izlek-core/src/auth.rsmodified+8 -1
| @@ -25,6 +25,13 @@ pub const ARGON2_PARALLELISM: u32 = 1; | |||
| 25 | /// How many bytes of randomness every token carries. | 25 | /// How many bytes of randomness every token carries. |
| 26 | pub const TOKEN_BYTES: usize = 16; | 26 | pub const TOKEN_BYTES: usize = 16; |
| 27 | 27 | ||
| 28 | +/// The shortest password this app accepts. | ||
| 29 | +/// | ||
| 30 | +/// Named because the form that takes a new password carries it too — as | ||
| 31 | +/// `minlength`, so the browser enforces the rule where it is being broken | ||
| 32 | +/// rather than the server explaining it afterwards. Two places, one number. | ||
| 33 | +pub const MIN_PASSWORD_CHARS: usize = 10; | ||
| 34 | + | ||
| 28 | /// A password rule the person's choice broke. The wording is the wording on the | 35 | /// A password rule the person's choice broke. The wording is the wording on the |
| 29 | /// first-sign-in screen. | 36 | /// first-sign-in screen. |
| 30 | #[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)] | 37 | #[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)] |
| @@ -155,7 +162,7 @@ pub fn check_password( | |||
| 155 | ) -> Result<(), PasswordProblem> { | 162 | ) -> Result<(), PasswordProblem> { |
| 156 | // Counted in characters, not bytes: a ten-character password is ten | 163 | // Counted in characters, not bytes: a ten-character password is ten |
| 157 | // characters whatever alphabet it is in. | 164 | // characters whatever alphabet it is in. |
| 158 | - if password.chars().count() < 10 { | 165 | + if password.chars().count() < MIN_PASSWORD_CHARS { |
| 159 | return Err(PasswordProblem::TooShort); | 166 | return Err(PasswordProblem::TooShort); |
| 160 | } | 167 | } |
| 161 | 168 | ||
crates/izlek-web/src/pages.rsmodified+2 -0
| @@ -117,6 +117,7 @@ async fn setup_card(cx: &Cx) -> Result { | |||
| 117 | type="password" | 117 | type="password" |
| 118 | name="password" | 118 | name="password" |
| 119 | autocomplete="new-password" | 119 | autocomplete="new-password" |
| 120 | + minlength=(izlek_core::auth::MIN_PASSWORD_CHARS.to_string()) | ||
| 120 | required="" | 121 | required="" |
| 121 | > | 122 | > |
| 122 | </label> | 123 | </label> |
| @@ -222,6 +223,7 @@ async fn join_card(cx: &Cx, token: &str, person: Invited) -> Result { | |||
| 222 | type="password" | 223 | type="password" |
| 223 | name="password" | 224 | name="password" |
| 224 | autocomplete="new-password" | 225 | autocomplete="new-password" |
| 226 | + minlength=(izlek_core::auth::MIN_PASSWORD_CHARS.to_string()) | ||
| 225 | required="" | 227 | required="" |
| 226 | > | 228 | > |
| 227 | </label> | 229 | </label> |
crates/izlek-web/src/settings.rsmodified+1 -1
| @@ -1311,7 +1311,7 @@ async fn settings_page(cx: &Cx) -> Result { | |||
| 1311 | </label> | 1311 | </label> |
| 1312 | <label class="field"> | 1312 | <label class="field"> |
| 1313 | <span class="field-label">(t(lang, Key::NewPasswordLabel))</span> | 1313 | <span class="field-label">(t(lang, Key::NewPasswordLabel))</span> |
| 1314 | - <input class="field-input" type="password" name="new" autocomplete="new-password" required=""> | 1314 | + <input class="field-input" type="password" name="new" autocomplete="new-password" minlength=(izlek_core::auth::MIN_PASSWORD_CHARS.to_string()) required=""> |
| 1315 | </label> | 1315 | </label> |
| 1316 | <div class="panel-foot"> | 1316 | <div class="panel-foot"> |
| 1317 | if let Some(refusal) = &password_refusal { | 1317 | if let Some(refusal) = &password_refusal { |
crates/izlek-web/tests/http.rsmodified+49 -0
| @@ -536,6 +536,55 @@ async fn queued_join_token(app: &App, email: &str) -> String { | |||
| 536 | } | 536 | } |
| 537 | 537 | ||
| 538 | /// Invites someone in the given role and signs them in, returning their cookie. | 538 | /// Invites someone in the given role and signs them in, returning their cookie. |
| 539 | +#[tokio::test] | ||
| 540 | +async fn a_password_may_be_made_of_punctuation() { | ||
| 541 | + // An invited member reported "it would not accept the *". Nothing in the | ||
| 542 | + // rules mentions characters, so if one is refused it is the wire eating | ||
| 543 | + // it, not the policy: a form body is url-encoded, where `+` means space | ||
| 544 | + // and `%` starts an escape, and a password that survives the round trip | ||
| 545 | + // in a test is a password that survives it in a browser. | ||
| 546 | + let app = App::open().await; | ||
| 547 | + let admin_cookie = admin(&app).await; | ||
| 548 | + let email = "punct@izlek.sh"; | ||
| 549 | + let answer = app | ||
| 550 | + .post( | ||
| 551 | + "/api/invite_member", | ||
| 552 | + Some(&admin_cookie), | ||
| 553 | + &[("email", email), ("display_name", "Pat"), ("role", "member")], | ||
| 554 | + ) | ||
| 555 | + .await; | ||
| 556 | + assert_eq!(answer.status, StatusCode::OK, "{}", answer.body); | ||
| 557 | + let token = queued_join_token(&app, email).await; | ||
| 558 | + | ||
| 559 | + let password = "*+&%= ?#/\\ tulip 42"; | ||
| 560 | + let answer = app | ||
| 561 | + .post( | ||
| 562 | + "/api/redeem_link", | ||
| 563 | + None, | ||
| 564 | + &[("token", &token), ("password", password)], | ||
| 565 | + ) | ||
| 566 | + .await; | ||
| 567 | + assert_eq!(answer.status, StatusCode::SEE_OTHER, "{}", answer.body); | ||
| 568 | + assert_eq!(answer.body, "null", "the punctuation password was refused"); | ||
| 569 | + | ||
| 570 | + // And it is the same password on the way back in — an encoding that ate a | ||
| 571 | + // character on the way in would let them set one thing and sign in with | ||
| 572 | + // another. | ||
| 573 | + let back = app | ||
| 574 | + .post( | ||
| 575 | + "/api/sign_in", | ||
| 576 | + None, | ||
| 577 | + &[("email", email), ("password", password)], | ||
| 578 | + ) | ||
| 579 | + .await; | ||
| 580 | + assert_eq!(back.status, StatusCode::SEE_OTHER, "{}", back.body); | ||
| 581 | + assert!( | ||
| 582 | + back.session.is_some(), | ||
| 583 | + "the password that was set does not sign in: {}", | ||
| 584 | + back.body | ||
| 585 | + ); | ||
| 586 | +} | ||
| 587 | + | ||
| 539 | async fn invited(app: &App, admin: &str, email: &str, name: &str, role: Role) -> String { | 588 | async fn invited(app: &App, admin: &str, email: &str, name: &str, role: Role) -> String { |
| 540 | let role = match role { | 589 | let role = match role { |
| 541 | Role::Admin => "admin", | 590 | Role::Admin => "admin", |