Skip to content

Commit b8d46e5

Browse files
committed
[WebLink] Escape double quotes in attributes values
1 parent 8cb6c71 commit b8d46e5

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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
}

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)