Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to set more strict quoting of strings #7

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion lib/Mail/AuthenticationResults/Header/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ sub stringify {
my ( $self, $value ) = @_;
my $string = $value;
$string = q{} if ! defined $string; #5.8;
my $strict_quotes = $self->strict_quotes;

if ( $string =~ /[\s\t \(\);=]/ ) {
if ( ( $strict_quotes && $string =~ /[\s\t \(\);=<>@,:\\\/\[\]\?]/ )
|| ( !$strict_quotes && $string =~ /[\s\t \(\);=]/ ) ) {
$string = '"' . $string . '"';
}

Expand Down Expand Up @@ -369,6 +371,41 @@ sub ancestor {
return ( $eldest, $depth );
}

=method strict_quotes()

Return the current value of strict quotes flag for this header or for its
ancestor if not set locally

If true, we are stricter about which characters result in a quoted string

=cut

sub strict_quotes {
my ( $self ) = @_;

return $self->{ 'strict_quotes' } if defined $self->{ 'strict_quotes' };

my ( $eldest, $depth ) = $self->ancestor();
return 0 if $depth == 0;
return $eldest->strict_quotes;
}

=method set_strict_quotes( $value )

Set the value of strict quotes

If true, we are stricter about which characters result in a quoted string

Default false

=cut

sub set_strict_quotes {
my ( $self, $value ) = @_;
$self->{ 'strict_quotes' } = $value ? 1 : 0;
return $self;
}

=method as_string_prefix( $header )

Add the prefix to as_string for this object when calledas a child
Expand Down
40 changes: 35 additions & 5 deletions t/05-as_string-styles.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ my $Input = [
'x-google-dkim=none (no signatures found)',
'dmarc=fail (p=none,d=none) header.from=marcbradshaw.net',
'dmarc=fail (p=reject,d=reject) header.from=goestheweasel.com',
'dmarc=none (p=none,d=none) header.from=example.com'
'dmarc=none (p=none,d=none) header.from=example.com',
'x-url=http://example.com'
];

my $InputARHeader = join( ";\n", 'test.example.com', @$Input );

my $Parser = Mail::AuthenticationResults::Parser->new( $InputARHeader );
my $Parsed = $Parser->parsed();

my $None = 'test.example.com; iprev=fail policy.iprev=123.123.123.123 (NOT FOUND); x-ptr=fail x-ptr-helo=bad.name.google.com x-ptr-lookup=""; spf=fail [email protected] smtp.helo=bad.name.google.com; dkim=none (no signatures found); x-google-dkim=none (no signatures found); dmarc=fail (p=none,d=none) header.from=marcbradshaw.net; dmarc=fail (p=reject,d=reject) header.from=goestheweasel.com; dmarc=none (p=none,d=none) header.from=example.com';
my $None = 'test.example.com; iprev=fail policy.iprev=123.123.123.123 (NOT FOUND); x-ptr=fail x-ptr-helo=bad.name.google.com x-ptr-lookup=""; spf=fail [email protected] smtp.helo=bad.name.google.com; dkim=none (no signatures found); x-google-dkim=none (no signatures found); dmarc=fail (p=none,d=none) header.from=marcbradshaw.net; dmarc=fail (p=reject,d=reject) header.from=goestheweasel.com; dmarc=none (p=none,d=none) header.from=example.com; x-url=http://example.com';

my $Entry = 'test.example.com;
iprev=fail policy.iprev=123.123.123.123 (NOT FOUND);
Expand All @@ -35,7 +36,8 @@ my $Entry = 'test.example.com;
x-google-dkim=none (no signatures found);
dmarc=fail (p=none,d=none) header.from=marcbradshaw.net;
dmarc=fail (p=reject,d=reject) header.from=goestheweasel.com;
dmarc=none (p=none,d=none) header.from=example.com';
dmarc=none (p=none,d=none) header.from=example.com;
x-url=http://example.com';

my $SubEntry = 'test.example.com;
iprev=fail
Expand All @@ -53,7 +55,8 @@ my $SubEntry = 'test.example.com;
dmarc=fail (p=reject,d=reject)
header.from=goestheweasel.com;
dmarc=none (p=none,d=none)
header.from=example.com';
header.from=example.com;
x-url=http://example.com';

my $Full = 'test.example.com;
iprev=fail
Expand All @@ -77,13 +80,40 @@ my $Full = 'test.example.com;
header.from=goestheweasel.com;
dmarc=none
(p=none,d=none)
header.from=example.com';
header.from=example.com;
x-url=http://example.com';

my $FullStrict = 'test.example.com;
iprev=fail
policy.iprev=123.123.123.123
(NOT FOUND);
x-ptr=fail
x-ptr-helo=bad.name.google.com
x-ptr-lookup="";
spf=fail
smtp.mailfrom="[email protected]"
smtp.helo=bad.name.google.com;
dkim=none
(no signatures found);
x-google-dkim=none
(no signatures found);
dmarc=fail
(p=none,d=none)
header.from=marcbradshaw.net;
dmarc=fail
(p=reject,d=reject)
header.from=goestheweasel.com;
dmarc=none
(p=none,d=none)
header.from=example.com;
x-url="http://example.com"';

is( $Parsed->set_indent_style( 'none' )->as_string(), $None, 'None stringifies correctly' );
is( $Parsed->set_indent_style( 'entry' )->as_string(), $Entry, 'Entry stringifies correctly' );
is( $Parsed->set_indent_style( 'subentry' )->as_string(), $SubEntry, 'SubEntry stringifies correctly' );
is( $Parsed->set_indent_style( 'full' )->as_string(), $Full, 'Full stringifies correctly' );
is( $Parsed->set_indent_style( 'full' )->set_strict_quotes(1)->as_string(), $FullStrict, 'Full Strict stringifies correctly' );

dies_ok( sub{ $Parsed->set_indent_style( 'bogus_indent_style' ); }, 'invalid style dies' );

done_testing();
Expand Down