Skip to content

Escape a backslash in a quoted display name - #68

Open
pcbeingused333 wants to merge 1 commit into
Rails-Designer:mainfrom
pcbeingused333:fix/escape-backslash-in-display-name
Open

pcbeingused333 wants to merge 1 commit into
Rails-Designer:mainfrom
pcbeingused333:fix/escape-backslash-in-display-name

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Address.with_name quotes a display name when it contains a special character, but quote_escaped only escaped " inside the quoted string — never \. RFC 5322 requires both to be escaped as a quoted-pair.

Courrier::Email::Address.with_name("d@example.com", "Smith, John\\")
# => "\"Smith, John\\\" <d@example.com>"
#        the trailing \ escapes the closing " — the quoted string runs on
#        into the address and the header is corrupt

Courrier::Email::Address.with_name("d@example.com", "C:\\Users, Inc")
# => "\"C:\\Users, Inc\" <d@example.com>"
#        \U is a quoted-pair; a parser reads the name as "C:Users, Inc"

Fix

  • quote_escaped escapes \ as well as ".
  • \ joins the set in special_characters_in?, so a bare backslash in an otherwise plain name (Doe\) is quoted instead of going out as an unquoted atom, which is also illegal.

Tests

Two cases added to address_test.rb: a backslash mid-name and a trailing backslash. bundle exec rake test on Ruby 3.4: 161 runs, 304 assertions, 0 failures. rake standard clean.

🤖 Generated with Claude Code

`Address.with_name` quoted a display name that held a special character but
only escaped `"` inside it, never `\`. Two consequences, both producing an
invalid From/To header:

- A name ending in `\` ("Smith, John\") escaped the closing quote, so the
  quoted string ran on into the address.
- A `\` anywhere else ("C:\Users") was read by a parser as a quoted-pair and
  silently dropped the following character.

`\` is also added to the set that triggers quoting, so a bare backslash in an
otherwise plain name no longer goes out as an unquoted atom.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant