eikendev / sectxt

A library & tool for probing, parsing, and validating security.txt files as specified in RFC 9116 :rocket::books:
https://crates.io/crates/sectxt
ISC License
13 stars 1 forks source link

Fully support obsolete syntax from RFC 5322 #13

Open eikendev opened 1 year ago

eikendev commented 1 year ago

See Section 4 for some background:

Earlier versions of this specification allowed for different (usually more liberal) syntax than is allowed in this version. Also, there have been syntactic elements used in messages on the Internet whose interpretations have never been documented. Though these syntactic forms MUST NOT be generated according to the grammar in section 3, they MUST be accepted and parsed by a conformant receiver.

The obsolete syntax leaves room for ambiguity, which nom cannot deal with. Instead, a more powerful parser would be required. Options are:

eikendev commented 1 year ago

Background: nom does not (fully) support backtracking, meaning a valid input might not be accepted.

use nom::{
    branch::alt,
    bytes::complete::tag,
    combinator::{all_consuming, recognize},
    multi::many0_count,
    sequence::terminated,
    IResult,
};
use regex::Regex;

fn main() {
    let input = "aaaaab";

    println!("{:?}", parser(input));

    let re = Regex::new(r"^(ab|a)*b$").unwrap();
    println!("{:?}", re.find(input));
}

fn parser(i: &str) -> IResult<&str, &str> {
    all_consuming(terminated(
        recognize(many0_count(alt((tag("ab"), tag("a"))))),
        tag("b"),
    ))(i)
}

This will print

$ cargo run
Err(Error(Error { input: "", code: Tag }))
Some(Match { text: "aaaaab", start: 0, end: 6 })

Of course, in this simple example there would be workarounds, such as swapping ab and a. However, this is not possible in the general case, leaving the problem unsolved for more complex parser combinators.

eikendev commented 1 year ago

For now, there are no plans to support the obsolete syntax.