Skip to content
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

Validate encoding #405

Merged
merged 2 commits into from
Jan 16, 2020
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi

## [Unreleased][unreleased]

### Added

- Added check to ensure Markdown input is valid UTF-8 (#401, #405)

## [1.2.2] - 2019-01-15

This release contains the same changes as 1.1.3:
Expand Down
4 changes: 4 additions & 0 deletions src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function __construct(DocParserInterface $docParser, ElementRendererInterf
*
* @param string $commonMark
*
* @throws \RuntimeException
*
* @return string
*
* @api
Expand All @@ -68,6 +70,8 @@ public function convertToHtml(string $commonMark): string
*
* @param string $commonMark
*
* @throws \RuntimeException
*
* @return string
*/
public function __invoke(string $commonMark): string
Expand Down
11 changes: 11 additions & 0 deletions src/DocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use League\CommonMark\Block\Element\Paragraph;
use League\CommonMark\Block\Element\StringContainerInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Exception\UnexpectedEncodingException;

final class DocParser implements DocParserInterface
{
Expand Down Expand Up @@ -71,13 +72,16 @@ private function preProcessInput(string $input): array
/**
* @param string $input
*
* @throws \RuntimeException
*
* @return Document
*/
public function parse(string $input): Document
{
$document = new Document();
$context = new Context($document, $this->environment);

$this->assertValidUTF8($input);
$lines = $this->preProcessInput($input);
foreach ($lines as $line) {
$context->setNextLine($line);
Expand Down Expand Up @@ -248,4 +252,11 @@ private function setAndPropagateLastLineBlank(ContextInterface $context, Cursor
$container = $container->parent();
}
}

private function assertValidUTF8(string $input)
{
if (!\mb_check_encoding($input, 'UTF-8')) {
throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected');
}
}
}
2 changes: 2 additions & 0 deletions src/DocParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface DocParserInterface
/**
* @param string $input
*
* @throws \RuntimeException
*
* @return Document
*/
public function parse(string $input): Document;
Expand Down
16 changes: 16 additions & 0 deletions src/Exception/UnexpectedEncodingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\CommonMark\Exception;

final class UnexpectedEncodingException extends \RuntimeException
{
}
9 changes: 9 additions & 0 deletions tests/unit/CommonMarkConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ public function testEnvironmentAndConfigConstructor()

$this->assertSame($mockEnvironment, $environment);
}

/**
* @expectedException \League\CommonMark\Exception\UnexpectedEncodingException
*/
public function testConvertingInvalidUTF8()
{
$converter = new CommonMarkConverter();
$converter->convertToHtml("\x09\xca\xca");
}
}
30 changes: 30 additions & 0 deletions tests/unit/DocParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\CommonMark\Tests\Unit;

use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use PHPUnit\Framework\TestCase;

class DocParserTest extends TestCase
{
/**
* @expectedException \League\CommonMark\Exception\UnexpectedEncodingException
*/
public function testParsingWithInvalidUTF8()
{
$environment = Environment::createCommonMarkEnvironment();
$docParser = new DocParser($environment);

$docParser->parse("\x09\xca\xca");
}
}