1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
<?php
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
* (c) 2015 Martin HasoĊ <martin.hason@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\CommonMark\Tests\Unit\Extension\Attributes\Util;
use League\CommonMark\Extension\Attributes\Util\AttributesHelper;
use League\CommonMark\Node\Block\AbstractBlock;
use League\CommonMark\Node\Inline\AbstractInline;
use League\CommonMark\Parser\Cursor;
use League\CommonMark\Tests\Unit\Environment\FakeBlock1;
use League\CommonMark\Tests\Unit\Environment\FakeInline1;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
final class AttributesHelperTest extends TestCase
{
/**
* @param array<string, mixed> $expectedResult
*/
#[DataProvider('dataForTestParseAttributes')]
public function testParseAttributes(Cursor $input, array $expectedResult, string $expectedRemainder = ''): void
{
$this->assertSame($expectedResult, AttributesHelper::parseAttributes($input));
$this->assertSame($expectedRemainder, $input->getRemainder());
}
/**
* @return iterable<Cursor|array<string, mixed>>
*/
public static function dataForTestParseAttributes(): iterable
{
yield [new Cursor(''), [], ''];
yield [new Cursor('{}'), [], '{}'];
yield [new Cursor('{ }'), [], '{ }'];
// Examples with colons
yield [new Cursor('{:title="My Title"}'), ['title' => 'My Title']];
yield [new Cursor('{: title="My Title"}'), ['title' => 'My Title']];
yield [new Cursor('{:title="My Title" }'), ['title' => 'My Title']];
yield [new Cursor('{: title="My Title" }'), ['title' => 'My Title']];
yield [new Cursor('{: title="My Title" }'), ['title' => 'My Title']];
yield [new Cursor('{: #custom-id }'), ['id' => 'custom-id']];
yield [new Cursor('{: #custom-id #another-id }'), ['id' => 'another-id']];
yield [new Cursor('{: .class1 .class2 }'), ['class' => 'class1 class2']];
yield [new Cursor('{: #custom-id .class1 .class2 title="My Title" disabled=true }'), ['id' => 'custom-id', 'class' => 'class1 class2', 'title' => 'My Title', 'disabled' => true]];
yield [new Cursor('{: #custom-id .class1 .class2 title="My Title" disabled="disabled" }'), ['id' => 'custom-id', 'class' => 'class1 class2', 'title' => 'My Title', 'disabled' => 'disabled']];
yield [new Cursor('{:target=_blank}'), ['target' => '_blank']];
yield [new Cursor('{: target=_blank}'), ['target' => '_blank']];
yield [new Cursor('{: target=_blank }'), ['target' => '_blank']];
yield [new Cursor('{: target=_blank }'), ['target' => '_blank']];
yield [new Cursor('{: disabled=disabled}'), ['disabled' => 'disabled']];
// Examples without colons
yield [new Cursor('{title="My Title"}'), ['title' => 'My Title']];
yield [new Cursor('{ title="My Title"}'), ['title' => 'My Title']];
yield [new Cursor('{title="My Title" }'), ['title' => 'My Title']];
yield [new Cursor('{ title="My Title" }'), ['title' => 'My Title']];
yield [new Cursor('{ title="My Title" }'), ['title' => 'My Title']];
yield [new Cursor('{ #custom-id }'), ['id' => 'custom-id']];
yield [new Cursor('{ #custom-id #another-id }'), ['id' => 'another-id']];
yield [new Cursor('{ .class1 .class2 }'), ['class' => 'class1 class2']];
yield [new Cursor('{ #custom-id .class1 .class2 title="My Title" disabled=true }'), ['id' => 'custom-id', 'class' => 'class1 class2', 'title' => 'My Title', 'disabled' => true]];
yield [new Cursor('{ #custom-id .class1 .class2 title="My Title" disabled="disabled" }'), ['id' => 'custom-id', 'class' => 'class1 class2', 'title' => 'My Title', 'disabled' => 'disabled']];
yield [new Cursor('{target=_blank}'), ['target' => '_blank']];
yield [new Cursor('{ target=_blank}'), ['target' => '_blank']];
yield [new Cursor('{target=_blank }'), ['target' => '_blank']];
yield [new Cursor('{ target=_blank }'), ['target' => '_blank']];
yield [new Cursor('{disabled=disabled}'), ['disabled' => 'disabled']];
// Stuff at the beginning
yield [new Cursor(' {: #custom-id }'), ['id' => 'custom-id']];
yield [new Cursor(' {: #custom-id }'), ['id' => 'custom-id']];
yield [new Cursor(' {: #custom-id }'), ['id' => 'custom-id']];
// Note that this method doesn't enforce indentation rules - that should be checked elsewhere
yield [new Cursor(' {: #custom-id }'), ['id' => 'custom-id']];
yield [new Cursor(' {: #custom-id }'), ['id' => 'custom-id']];
// Stuff on the end
yield [new Cursor('{: #custom-id } '), ['id' => 'custom-id'], ' '];
// Note that this method doesn't abort if non-attribute things are found at the end - that should be checked elsewhere
yield [new Cursor('{: #custom-id } foo'), ['id' => 'custom-id'], ' foo'];
yield [new Cursor('{: #custom-id }.'), ['id' => 'custom-id'], '.'];
// Missing curly brace on end
yield [new Cursor('{: #custom-id'), [], '{: #custom-id'];
// Two sets of attributes in one string - we stop after the first one
yield [new Cursor('{: #id1 } {: #id2 }'), ['id' => 'id1'], ' {: #id2 }'];
// Curly braces inside of values
yield [new Cursor('{: data-json="{1,2,3}" }'), ['data-json' => '{1,2,3}']];
yield [new Cursor('{data-json={1,2,3}} test'), ['data-json' => '{1,2,3}'], ' test'];
// Avoid mustache style templating language being parsed as attributes
yield [new Cursor('{{ foo }}'), [], '{{ foo }}'];
yield [new Cursor(' {{ foo }}'), [], ' {{ foo }}'];
yield [new Cursor('{ foo }}'), [], '{ foo }}'];
// Issue 1071
yield [new Cursor('{.display-4.mt-5.mx-auto}'), ['class' => 'display-4 mt-5 mx-auto']];
}
/**
* @param AbstractBlock|AbstractInline|array<string, mixed> $a1
* @param AbstractBlock|AbstractInline|array<string, mixed> $a2
* @param array<string, mixed> $expected
*/
#[DataProvider('dataForTestMergeAttributes')]
public function testMergeAttributes($a1, $a2, array $expected): void
{
$this->assertEquals($expected, AttributesHelper::mergeAttributes($a1, $a2));
}
/**
* @return iterable<AbstractBlock|AbstractInline|array<string, mixed>>
*/
public static function dataForTestMergeAttributes(): iterable
{
yield [
[],
[],
[],
];
// The second set of attributes overrides the first one (for matching keys)
yield [
['a' => '1', 'b' => 1],
['a' => '2', 'c' => 2],
['a' => '2', 'b' => 1, 'c' => 2],
];
// Special handling for the class attribute
yield [
['id' => 'foo', 'class' => 'foo'],
['id' => 'bar', 'class' => 'bar'],
['id' => 'bar', 'class' => 'foo bar'],
];
$block = new FakeBlock1();
$block->data->set('attributes', ['id' => 'block', 'class' => 'block']);
yield [
$block,
['id' => 'foo', 'class' => 'foo'],
['id' => 'foo', 'class' => 'block foo'],
];
yield [
['id' => 'foo', 'class' => 'foo'],
$block,
['id' => 'block', 'class' => 'foo block'],
];
$inline = new FakeInline1();
$inline->data->set('attributes', ['id' => 'inline', 'class' => 'inline']);
yield [
$inline,
['id' => 'foo', 'class' => 'foo'],
['id' => 'foo', 'class' => 'inline foo'],
];
yield [
['id' => 'foo', 'class' => 'foo'],
$inline,
['id' => 'inline', 'class' => 'foo inline'],
];
yield [
$block,
$inline,
['id' => 'inline', 'class' => 'block inline'],
];
yield [
$inline,
$block,
['id' => 'block', 'class' => 'inline block'],
];
}
/**
* @param array<string, mixed> $attributes
* @param list<string> $allowList
* @param array<string, mixed> $expected
*/
#[DataProvider('dataForTestFilterAttributes')]
public function testFilterAttributes(array $attributes, array $allowList, bool $allowUnsafeLinks, array $expected): void
{
$this->assertEquals($expected, AttributesHelper::filterAttributes($attributes, $allowList, $allowUnsafeLinks));
}
/**
* @return iterable<array<mixed>>
*/
public static function dataForTestFilterAttributes(): iterable
{
// No allow list; unsafe links disallowed (default behavior)
yield [
['id' => 'foo', 'class' => 'bar', 'onclick' => 'alert("XSS")', 'href' => 'javascript:alert("XSS")'],
[],
false,
['id' => 'foo', 'class' => 'bar'],
];
// No allow list; unsafe links allowed
yield [
['id' => 'foo', 'class' => 'bar', 'onclick' => 'alert("XSS")', 'href' => 'javascript:alert("XSS")'],
[],
true,
['id' => 'foo', 'class' => 'bar', 'href' => 'javascript:alert("XSS")'],
];
// Allow list; unsafe links disallowed
yield [
['id' => 'foo', 'class' => 'bar', 'onclick' => 'alert("XSS")', 'href' => 'javascript:alert("XSS")'],
['id', 'onclick', 'href'],
false,
['id' => 'foo', 'onclick' => 'alert("XSS")'],
];
// Allow list; unsafe links allowed
yield [
['id' => 'foo', 'class' => 'bar', 'onclick' => 'alert("XSS")', 'href' => 'javascript:alert("XSS")'],
['id', 'onclick', 'href'],
true,
['id' => 'foo', 'onclick' => 'alert("XSS")', 'href' => 'javascript:alert("XSS")'],
];
// Allow list blocks all
yield [
['id' => 'foo', 'class' => '<script>alert("XSS")</script>'],
['style'],
false,
[],
];
// Can't use weird casing to bypass allowlist or 'on*' restriction
yield [
['ID' => 'foo', 'oNcLiCk' => 'alert("XSS")'],
['id', 'class'],
false,
[],
];
}
}
|