Skip to content

Commit 9bb7db0

Browse files
committedFeb 7, 2024
Avoid leaking more than one character when showing position of a file that is clearly not json
1 parent 76d449a commit 9bb7db0

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed
 

‎src/Seld/JsonLint/Lexer.php

+4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ public function setInput($input)
140140
*/
141141
public function showPosition()
142142
{
143+
if ($this->yylineno === 0 && $this->offset === 1 && $this->match !== '{') {
Has conversations. Original line has conversations.
144+
return $this->match.'...' . "\n^";
145+
}
146+
143147
$pre = str_replace("\n", '', $this->getPastInput());
144148
$c = str_repeat('-', max(0, \strlen($pre) - 1)); // new Array(pre.length + 1).join("-");
145149

‎tests/JsonParserTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ public function testErrorAtBeginning()
177177
}
178178
}
179179

180+
public function testAvoidLeakingInfoForInvalidFiles()
181+
{
182+
$parser = new JsonParser();
183+
try {
184+
$parser->parse('ABCD');
185+
$this->fail('Empty string should be invalid');
186+
} catch (ParsingException $e) {
187+
$this->assertContains("Parse error on line 1:\nA...\n^", $e->getMessage());
188+
}
189+
}
190+
180191
public function testParsesMultiInARow()
181192
{
182193
$parser = new JsonParser();

0 commit comments

Comments
 (0)
Please sign in to comment.