Skip to content

Commit

Permalink
Fix regex injection (#1079)
Browse files Browse the repository at this point in the history
Closes #1078
  • Loading branch information
gseric committed Feb 27, 2021
1 parent 183961d commit b57f4c6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Definition/Value/FixtureMatchReferenceValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ public function __construct(string $pattern)
*/
public static function createWildcardReference(string $reference): self
{
return new self(sprintf('/^%s.*/', $reference));
return new self(sprintf('/^%s.*/', preg_quote($reference, '/')));
}

public function match(string $value): bool
{
return 1 === preg_match($this->pattern, $value);
}

public function getValue(): string
{
return $this->pattern;
}

public function __toString(): string
{
return sprintf('@(regex: %s)', $this->pattern);
Expand Down
6 changes: 6 additions & 0 deletions tests/Definition/Value/FixtureMatchReferenceValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ public function testCanBeCastedIntoAString(): void
$value = FixtureMatchReferenceValue::createWildcardReference('dummy');
static::assertEquals('@(regex: /^dummy.*/)', (string) $value);
}

public function testReferenceIsRegexEscaped(): void
{
$value = FixtureMatchReferenceValue::createWildcardReference('du/m*m+y.ref[ere]n(c)e');
static::assertEquals('/^du\\/m\\*m\\+y\\.ref\\[ere\\]n\\(c\\)e.*/', $value->getValue());
}
}

0 comments on commit b57f4c6

Please sign in to comment.