Skip to content

[Mailer][Mime] Fix case-sensitive handling of header names #39954

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

Merged
merged 1 commit into from
Feb 2, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function addHeaderRule(string $headerName, int $rule): void
throw new InvalidArgumentException(sprintf('The "%d" rule is not supported.', $rule));
}

$this->headerRules[$headerName] = $rule;
$this->headerRules[strtolower($headerName)] = $rule;
}

public function onMessage(MessageEvent $event): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,17 @@ public function provideHeaders(): iterable
->add(new MailboxListHeader('bcc', [new Address('bcc-initial@example.com'), new Address('bcc-default@example.com'), new Address('bcc-default-1@example.com')]))
;
yield 'bcc, add another bcc (unique header)' => [$initialHeaders, $defaultHeaders, $expectedHeaders];

$initialHeaders = (new Headers())
->add(new UnstructuredHeader('foo', 'initial'))
;
$defaultHeaders = (new Headers())
->add(new UnstructuredHeader('foo', 'bar'))
->add(new UnstructuredHeader('bar', 'foo'))
;
$rules = [
'Foo' => MessageListener::HEADER_REPLACE,
];
yield 'Capitalized header rule (case-insensitive), replace if set' => [$initialHeaders, $defaultHeaders, $defaultHeaders, $rules];
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Mime/Header/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function addParameterizedHeader(string $name, string $value, array $param
*/
public function addHeader(string $name, $argument, array $more = []): self
{
$parts = explode('\\', self::HEADER_CLASS_MAP[$name] ?? UnstructuredHeader::class);
$parts = explode('\\', self::HEADER_CLASS_MAP[strtolower($name)] ?? UnstructuredHeader::class);
$method = 'add'.ucfirst(array_pop($parts));
if ('addUnstructuredHeader' === $method) {
$method = 'addTextHeader';
Expand Down Expand Up @@ -217,7 +217,7 @@ public function remove(string $name): void

public static function isUniqueHeader(string $name): bool
{
return \in_array($name, self::UNIQUE_HEADERS, true);
return \in_array(strtolower($name), self::UNIQUE_HEADERS, true);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Mime/Tests/Header/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,20 @@ public function testRemoveIsNotCaseSensitive()
$this->assertFalse($headers->has('Message-ID'));
}

public function testAddHeaderIsNotCaseSensitive()
{
$headers = new Headers();
$headers->addHeader('From', ['from@example.com']);

$this->assertInstanceOf(MailboxListHeader::class, $headers->get('from'));
$this->assertEquals([new Address('from@example.com')], $headers->get('from')->getBody());
}

public function testIsUniqueHeaderIsNotCaseSensitive()
{
$this->assertTrue(Headers::isUniqueHeader('From'));
}

public function testToStringJoinsHeadersTogether()
{
$headers = new Headers();
Expand Down