Skip to content

Commit

Permalink
SafeUrl is not used for attribute parts
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 1, 2022
1 parent e7b06f4 commit 38d7db8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/Latte/Compiler/Escaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function enterHtmlAttribute(?string $name = null, string $quote = ''): vo
{
$this->state = self::HtmlAttribute;
$this->quote = $quote;
$this->subType = '';

if ($this->contentType === ContentType::Html && is_string($name)) {
$name = strtolower($name);
Expand Down
15 changes: 13 additions & 2 deletions src/Latte/Compiler/Nodes/Html/QuotedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Latte\Compiler\Nodes\Html;

use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\FragmentNode;
use Latte\Compiler\Position;
use Latte\Compiler\PrintContext;

Expand All @@ -27,8 +28,18 @@ public function __construct(
public function print(PrintContext $context): string
{
$res = 'echo ' . var_export($this->quote, true) . ';';
$context->beginEscape()->enterHtmlAttributeQuote($this->quote);
$res .= $this->value->print($context);
$escaper = $context->beginEscape();
$escaper->enterHtmlAttributeQuote($this->quote);

if ($this->value instanceof FragmentNode && $escaper->export() === 'html/attr/url') {
foreach ($this->value->children as $child) {
$res .= $child->print($context);
$escaper->enterHtmlAttribute(null, $this->quote);
}
} else {
$res .= $this->value->print($context);
}

$res .= 'echo ' . var_export($this->quote, true) . ';';
$context->restoreEscape();
return $res;
Expand Down
18 changes: 2 additions & 16 deletions tests/common/Safe.url.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Assert::match('
<a href="" src="" action="" formaction="" title="javascript:alert(1)"></a>
<a href=""></a>
<a href="javascript:alert(1)"></a>
<a href="http://nette.org?val=ok"></a>
<a href="http://nette.org?val=javascript:alert(1)"></a>
<a data="javascript:alert(1)"></a>
<OBJECT DATA=""></OBJECT>
<a HREF=""></a>
Expand All @@ -44,7 +44,7 @@ Assert::match('
<a href={$url1} src="{$url1}" action={$url1} formaction={$url1} title={$url1}></a>
<a {if true}href={$url1}{/if}></a>
<a href={$url1|nocheck}></a>
<a href="http://nette.org?val={$url4}"></a>
<a href="http://nette.org?val={$url1}"></a>
<a data={$url1}></a>
<OBJECT DATA={$url1}></object>
<a HREF={$url2}></a>
Expand Down Expand Up @@ -77,17 +77,3 @@ Assert::contains(
'LR\Filters::escapeHtmlAttr(LR\Filters::safeUrl(($this->filters->upper)($url1)))',
$latte->compile('<a href="{$url1|upper}"></a>'),
);


// former |safeurl & |nosafeurl
Assert::exception(
fn() => $latte->renderToString('<a href={$url1|nosafeurl}></a>', $params),
LogicException::class,
"Filter 'nosafeurl' is not defined.",
);

Assert::exception(
fn() => $latte->renderToString('<a href={$url4|dataStream|safeURL}></a>', $params),
LogicException::class,
"Filter 'safeURL' is not defined.",
);

0 comments on commit 38d7db8

Please sign in to comment.