From ec1c66ec7d4e614ce224052e0d73d0e6d4081430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 28 Oct 2026 17:17:43 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20forms=20drop=20control?= =?UTF-8?q?=20characters;=20registered=20URIs=20and=20public=20URLs=20must?= =?UTF-8?q?=20be=20ASCII?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/im-core/src/names.rs | 42 ++++++++++++++++++++++++ crates/im-core/src/oauth.rs | 10 ++++-- crates/im-web/src/pages/admin/clients.rs | 20 ++++++----- crates/im-web/src/pages/admin/message.rs | 5 +-- crates/im-web/src/pages/admin/users.rs | 4 +-- crates/im-web/src/pages/login.rs | 4 +-- 6 files changed, 68 insertions(+), 17 deletions(-) diff --git a/crates/im-core/src/names.rs b/crates/im-core/src/names.rs index b68a06d..9149373 100644 --- a/crates/im-core/src/names.rs +++ b/crates/im-core/src/names.rs @@ -11,6 +11,8 @@ //! in mail and in other apps, so control characters and bidirectional overrides are refused: they //! can make one name read as another. +use serde::Deserialize; + use crate::error::{Error, Result}; /// Longest display name, in characters. The schema has the same limit. @@ -63,6 +65,40 @@ pub fn parse_display_name(input: &str) -> Result { Ok(name.to_owned()) } +/// Removes the control characters from a single-line field: every C0 control (U+0000 to U+001F, so NUL, +/// tab and line breaks too) and DEL (U+007F). Applied where form values are read, so a typed control +/// character is never used or echoed back into a page. +pub fn strip_line_controls(input: &str) -> String { + input.chars().filter(|&c| !is_c0_or_del(c)).collect() +} + +/// Removes the control characters from a multi-line field, keeping line feed and tab. A carriage return +/// is removed, so a browser's CRLF becomes LF. +pub fn strip_text_controls(input: &str) -> String { + input + .chars() + .filter(|&c| c == '\n' || c == '\t' || !is_c0_or_del(c)) + .collect() +} + +/// Serde adapter for a single-line form field: see [`strip_line_controls`]. +pub fn single_line<'de, D: serde::Deserializer<'de>>( + de: D, +) -> std::result::Result { + String::deserialize(de).map(|text| strip_line_controls(&text)) +} + +/// Serde adapter for a multi-line form field: see [`strip_text_controls`]. +pub fn multi_line<'de, D: serde::Deserializer<'de>>( + de: D, +) -> std::result::Result { + String::deserialize(de).map(|text| strip_text_controls(&text)) +} + +fn is_c0_or_del(c: char) -> bool { + c.is_ascii_control() +} + /// The bidirectional embedding, override and isolate controls that [`parse_display_name`] refuses. fn is_bidi_control(c: char) -> bool { matches!(c, '\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}') @@ -145,6 +181,12 @@ mod tests { } } + #[test] + fn form_text_loses_controls_and_multi_line_keeps_line_feed_and_tab() { + assert_eq!(strip_line_controls("a\u{0}b\u{7f}c\td\ne"), "abcde"); + assert_eq!(strip_text_controls("a\r\nb\tc\u{0}d"), "a\nb\tcd"); + } + #[test] fn attack_strings_are_refused() { let long_local = format!("{}@example.com", "a".repeat(MAX_LOCAL_LEN + 1)); diff --git a/crates/im-core/src/oauth.rs b/crates/im-core/src/oauth.rs index 7bc08a1..3453014 100644 --- a/crates/im-core/src/oauth.rs +++ b/crates/im-core/src/oauth.rs @@ -57,10 +57,13 @@ pub fn is_valid_verifier(verifier: &str) -> bool { } /// True for a registrable URI: `https`, or `http` to a loopback host (for development), absolute, with -/// no fragment, no userinfo (`@`), no whitespace or control characters and no backslash. Redirect URIs, -/// post-logout URIs and back-channel URIs all use this rule. +/// no fragment, no userinfo (`@`), no whitespace or control characters and no backslash. A registered URI +/// is ASCII only: a Unicode character, such as a bidi override, could show the URI differently from how it +/// is stored, so other bytes must be percent-encoded. Redirect URIs, post-logout URIs and back-channel URIs +/// all use this rule. pub fn is_valid_registered_uri(uri: &str) -> bool { if uri.len() > MAX_URI_BYTES + || !uri.is_ascii() || uri.bytes().any(|b| b <= 0x20 || b == 0x7f || b == b'\\') || uri.contains('#') { @@ -151,6 +154,9 @@ mod tests { "https://in.example.com\\evil", "in.example.com/auth/callback", "javascript://in.example.com/x", + "https://in.example.com/\u{202e}abc", + "https://in.example.com/caf\u{e9}", + "https://in.example.com/\u{0}", ] { assert!(!is_valid_registered_uri(bad), "accepted {bad:?}"); } diff --git a/crates/im-web/src/pages/admin/clients.rs b/crates/im-web/src/pages/admin/clients.rs index 1063dd4..d68715f 100644 --- a/crates/im-web/src/pages/admin/clients.rs +++ b/crates/im-web/src/pages/admin/clients.rs @@ -12,7 +12,7 @@ use im_core::Error as CoreError; use im_core::model::{AuditAction, Client, Service}; -use im_core::names::parse_display_name; +use im_core::names::{multi_line, parse_display_name, single_line}; use im_core::oauth::{is_client_family_key, is_supported_scope, is_valid_registered_uri}; use im_core::store::admin::ClientEdit; use im_core::store::clients::NewClient; @@ -46,23 +46,23 @@ const DEFAULT_SCOPES: &str = "openid profile email"; struct ClientForm { #[serde(default)] csrf: String, - #[serde(default)] + #[serde(default, deserialize_with = "single_line")] name: String, /// One URI per line. - #[serde(default)] + #[serde(default, deserialize_with = "multi_line")] redirect_uris: String, /// One URI per line. - #[serde(default)] + #[serde(default, deserialize_with = "multi_line")] post_logout_uris: String, - #[serde(default)] + #[serde(default, deserialize_with = "single_line")] backchannel_uri: String, /// Space-separated scopes. - #[serde(default)] + #[serde(default, deserialize_with = "single_line")] scopes: String, - #[serde(default)] + #[serde(default, deserialize_with = "single_line")] family_key: String, /// The origin of a family app, such as `https://in.example.com`. - #[serde(default)] + #[serde(default, deserialize_with = "single_line")] public_url: String, } @@ -150,13 +150,15 @@ fn uri_lines(text: &str) -> std::result::Result, &'static str> { Ok(uris) } -/// True for an origin: `http://` or `https://`, a host, and no path, space or query. +/// True for an origin: `http://` or `https://`, a host, and no path, space or query. ASCII only, like a +/// registered URI. pub(crate) fn is_origin(url: &str) -> bool { let rest = url .strip_prefix("https://") .or_else(|| url.strip_prefix("http://")); rest.is_some_and(|rest| { !rest.is_empty() + && url.is_ascii() && !rest.contains(['/', '?', '#', '@']) && !url.chars().any(char::is_whitespace) }) diff --git a/crates/im-web/src/pages/admin/message.rs b/crates/im-web/src/pages/admin/message.rs index 5f5e822..aa9456f 100644 --- a/crates/im-web/src/pages/admin/message.rs +++ b/crates/im-web/src/pages/admin/message.rs @@ -8,6 +8,7 @@ use std::time::Instant; use im_core::model::AuditAction; +use im_core::names::{multi_line, single_line}; use serde::Deserialize; use serde_json::json; use topcoat::Result; @@ -54,9 +55,9 @@ const THROTTLE_KEY: &str = "admin-message"; struct MessageForm { #[serde(default)] csrf: String, - #[serde(default)] + #[serde(default, deserialize_with = "single_line")] subject: String, - #[serde(default)] + #[serde(default, deserialize_with = "multi_line")] body: String, } diff --git a/crates/im-web/src/pages/admin/users.rs b/crates/im-web/src/pages/admin/users.rs index cea33d9..5690313 100644 --- a/crates/im-web/src/pages/admin/users.rs +++ b/crates/im-web/src/pages/admin/users.rs @@ -10,7 +10,7 @@ use im_core::Error as CoreError; use im_core::model::{AuditAction, User}; -use im_core::names::normalize_email; +use im_core::names::{normalize_email, single_line}; use im_core::store::admin::AdminChange; use im_core::store::backchannel::EndReason; use serde::Deserialize; @@ -39,7 +39,7 @@ use crate::views::{ViewerInfo, page_lang, shell}; /// The query of the account list: the search, the cursor of the next page, and the flash codes. #[derive(Deserialize, Default)] struct UsersQuery { - #[serde(default)] + #[serde(default, deserialize_with = "single_line")] q: String, /// `created_at|id` of the last account of the previous page. #[serde(default)] diff --git a/crates/im-web/src/pages/login.rs b/crates/im-web/src/pages/login.rs index 3adae1b..609f5c1 100644 --- a/crates/im-web/src/pages/login.rs +++ b/crates/im-web/src/pages/login.rs @@ -9,7 +9,7 @@ use std::sync::OnceLock; use std::time::{Duration, Instant}; use im_core::model::{AuditAction, User, amr}; -use im_core::names::normalize_email; +use im_core::names::{normalize_email, single_line}; use im_core::password::{hash_password_async, verify_password_async}; use im_core::store::sessions::SignInState; use im_core::time::stamp_after; @@ -43,7 +43,7 @@ use crate::views::{alert, field, page_lang, shell}; struct LoginForm { #[serde(default)] precsrf: String, - #[serde(default)] + #[serde(default, deserialize_with = "single_line")] email: String, #[serde(default)] password: String,