Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f8ce7d0

Browse files
committed
bug #40209 [WebLink] Escape double quotes in attributes values (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [WebLink] Escape double quotes in attributes values | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - If the attribute value contains a double quote, the serialized value is invalid: `</foo>; rel="alternate"; title="foo " bar"`. Ideally we would use `addcslashes` but we can't because users that already pass escaped values would then be impacted. Commits ------- 7946be2 [WebLink] Escape double quotes in attributes values
2 parents 9230f69 + 7946be2 commit f8ce7d0

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/Symfony/Component/WebLink/HttpHeaderSerializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public function serialize(iterable $links): ?string
3939
foreach ($link->getAttributes() as $key => $value) {
4040
if (\is_array($value)) {
4141
foreach ($value as $v) {
42-
$attributesParts[] = sprintf('%s="%s"', $key, $v);
42+
$attributesParts[] = sprintf('%s="%s"', $key, preg_replace('/(?<!\\\\)"/', '\"', $v));
4343
}
4444

4545
continue;
4646
}
4747

4848
if (!\is_bool($value)) {
49-
$attributesParts[] = sprintf('%s="%s"', $key, $value);
49+
$attributesParts[] = sprintf('%s="%s"', $key, preg_replace('/(?<!\\\\)"/', '\"', $value));
5050

5151
continue;
5252
}

src/Symfony/Component/WebLink/Tests/HttpHeaderSerializerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ public function testSerializeEmpty()
4444
{
4545
$this->assertNull($this->serializer->serialize([]));
4646
}
47+
48+
public function testSerializeDoubleQuotesInAttributeValue()
49+
{
50+
$this->assertSame('</foo>; rel="alternate"; title="\"escape me\" \"already escaped\" \"\"\""', $this->serializer->serialize([
51+
(new Link('alternate', '/foo'))
52+
->withAttribute('title', '"escape me" \"already escaped\" ""\"'),
53+
]));
54+
}
4755
}

0 commit comments

Comments
 (0)