Skip to content

Commit

Permalink
Avoid leaking more than one character when showing position of a file…
Browse files Browse the repository at this point in the history
… that is clearly not json
  • Loading branch information
Seldaek committed Feb 7, 2024
1 parent 76d449a commit 9bb7db0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Seld/JsonLint/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public function setInput($input)
*/
public function showPosition()
{
if ($this->yylineno === 0 && $this->offset === 1 && $this->match !== '{') {

This comment has been minimized.

Copy link
@stof

stof Feb 7, 2024

Contributor

not all JSON files start with a { though

This comment has been minimized.

Copy link
@Seldaek

Seldaek Feb 7, 2024

Author Owner

No but only files starting with a { really need the linting, and in those the linting errors are usually not on the first character, so I think this is fine really.

return $this->match.'...' . "\n^";
}

$pre = str_replace("\n", '', $this->getPastInput());
$c = str_repeat('-', max(0, \strlen($pre) - 1)); // new Array(pre.length + 1).join("-");

Expand Down
11 changes: 11 additions & 0 deletions tests/JsonParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ public function testErrorAtBeginning()
}
}

public function testAvoidLeakingInfoForInvalidFiles()
{
$parser = new JsonParser();
try {
$parser->parse('ABCD');
$this->fail('Empty string should be invalid');
} catch (ParsingException $e) {
$this->assertContains("Parse error on line 1:\nA...\n^", $e->getMessage());
}
}

public function testParsesMultiInARow()
{
$parser = new JsonParser();
Expand Down

0 comments on commit 9bb7db0

Please sign in to comment.