Skip to content

[HtmlSanitizer] Fix force_attributes not replacing existing attribute in initial data #58547

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

Open
wants to merge 1 commit into
base: 6.4
Choose a base branch
from
Open
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
[HtmlSanitizer] Fix force_attributes not replacing existing attribute…
… in initial data
  • Loading branch information
tgalopin committed Oct 12, 2024
commit 926985bac7c96e5ec915d9db567cbba573d60180
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,17 @@ public function testForceAttribute()
{
$config = (new HtmlSanitizerConfig())
->allowElement('div')
->allowElement('img', '*')
->allowElement('a', ['href'])
->forceAttribute('a', 'rel', 'noopener noreferrer')
->forceAttribute('img', 'loading', 'lazy')
;

$this->assertSame(
'<img title="My image" src="https://example.com/image.png" loading="lazy" />',
$this->sanitize($config, '<img title="My image" src="https://example.com/image.png" loading="eager" onerror="alert(\'1234\')" />')
);

$this->assertSame(
'<a rel="noopener noreferrer">Hello</a> world',
$this->sanitize($config, '<a>Hello</a> world')
Expand All @@ -250,6 +257,11 @@ public function testForceAttribute()
'<div>Hello</div> world',
$this->sanitize($config, '<div style="width: 100px">Hello</div> world')
);

$this->assertSame(
'<a href="https://symfony.com" rel="noopener noreferrer">Hello</a> world',
$this->sanitize($config, '<a href="https://symfony.com" rel="noopener">Hello</a> world')
);
}

public function testForceHttps()
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HtmlSanitizer/Visitor/DomVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private function enterNode(string $domNodeName, \DOMNode $domNode, Cursor $curso

// Force configured attributes
foreach ($this->forcedAttributes[$domNodeName] ?? [] as $attribute => $value) {
$node->setAttribute($attribute, $value);
$node->setAttribute($attribute, $value, true);
}

$cursor->node->addChild($node);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HtmlSanitizer/Visitor/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function getAttribute(string $name): ?string
return $this->attributes[$name] ?? null;
}

public function setAttribute(string $name, ?string $value): void
public function setAttribute(string $name, ?string $value, bool $override = false): void
{
// Always use only the first declaration (ease sanitization)
if (!\array_key_exists($name, $this->attributes)) {
if ($override || !\array_key_exists($name, $this->attributes)) {
$this->attributes[$name] = $value;
}
}
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy