fix codes, validate header len

This commit is contained in:
yggverse 2025-03-19 03:12:43 +02:00
parent 376473660f
commit b62f990bf2
2 changed files with 16 additions and 4 deletions

View file

@ -1,9 +1,9 @@
pub mod error; pub mod error;
pub use error::Error; pub use error::Error;
const REQUIRED: (u8, &str) = (10, "Certificate required"); const REQUIRED: (u8, &str) = (60, "Certificate required");
const NOT_AUTHORIZED: (u8, &str) = (11, "Certificate not authorized"); const NOT_AUTHORIZED: (u8, &str) = (61, "Certificate not authorized");
const NOT_VALID: (u8, &str) = (11, "Certificate not valid"); const NOT_VALID: (u8, &str) = (62, "Certificate not valid");
/// 6* status code group /// 6* status code group
/// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates /// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates
@ -85,6 +85,10 @@ impl std::fmt::Display for Certificate {
impl std::str::FromStr for Certificate { impl std::str::FromStr for Certificate {
type Err = Error; type Err = Error;
fn from_str(header: &str) -> Result<Self, Self::Err> { fn from_str(header: &str) -> Result<Self, Self::Err> {
let len = header.len();
if len > super::HEADER_LEN {
return Err(Error::HeaderLen(len));
}
if let Some(postfix) = header.strip_prefix("60") { if let Some(postfix) = header.strip_prefix("60") {
return Ok(Self::Required { return Ok(Self::Required {
header: header.to_string(), header: header.to_string(),

View file

@ -6,6 +6,7 @@ use std::{
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
Code, Code,
HeaderLen(usize),
Utf8Error(Utf8Error), Utf8Error(Utf8Error),
} }
@ -13,7 +14,14 @@ impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> Result { fn fmt(&self, f: &mut Formatter) -> Result {
match self { match self {
Self::Code => { Self::Code => {
write!(f, "Status code error") write!(f, "Unexpected status code")
}
Self::HeaderLen(l) => {
write!(
f,
"Header length reached protocol limit ({l} of {} bytes max)",
super::super::HEADER_LEN
)
} }
Self::Utf8Error(e) => { Self::Utf8Error(e) => {
write!(f, "UTF-8 error: {e}") write!(f, "UTF-8 error: {e}")