Skip to content

Commit 25a6731

Browse files
kristoffermollerhojphil-davis
authored andcommitted
Improve error check in Mimedir parser
1 parent 8b28880 commit 25a6731

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/Parser/MimeDir.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
*/
2727
class MimeDir extends Parser
2828
{
29+
public const TOKEN_PROPNAME = 1;
30+
public const TOKEN_PROPVALUE = 2;
31+
public const TOKEN_PARAMNAME = 3;
32+
public const TOKEN_PARAMVALUE = 4;
33+
2934
/**
3035
* The input stream.
3136
*
@@ -360,6 +365,12 @@ protected function readProperty(string $line)
360365
'value' => null,
361366
];
362367

368+
/*
369+
* Keep track on the last token we parsed in order to do
370+
* better error checking
371+
*/
372+
$lastToken = null;
373+
363374
$lastParam = null;
364375

365376
/*
@@ -385,10 +396,16 @@ protected function readProperty(string $line)
385396
// option is set to ignore invalid lines, we ignore this line
386397
// This can happen when servers provide faulty data as iCloud
387398
// frequently does with X-APPLE-STRUCTURED-LOCATION
399+
$lastToken = self::TOKEN_PARAMVALUE;
388400
continue;
389401
}
390402
throw new ParseException('Invalid Mimedir file. Line starting at '.$this->startLine.' did not follow iCalendar/vCard conventions');
391403
}
404+
405+
if ('=' == $match[0][0] && self::TOKEN_PARAMNAME != $lastToken) {
406+
throw new ParseException('Invalid Mimedir file. Line starting at '.$this->startLine.': Missing parameter name for parameter value "'.$match['paramValue'].'"');
407+
}
408+
392409
if (\is_null($property['parameters'][$lastParam])) {
393410
$property['parameters'][$lastParam] = $value;
394411
} elseif (is_array($property['parameters'][$lastParam])) {
@@ -402,21 +419,25 @@ protected function readProperty(string $line)
402419
$value,
403420
];
404421
}
422+
$lastToken = self::TOKEN_PARAMVALUE;
405423
continue;
406424
}
407425
if (isset($match['paramName'])) {
408426
$lastParam = strtoupper($match['paramName']);
409427
if (!isset($property['parameters'][$lastParam])) {
410428
$property['parameters'][$lastParam] = null;
411429
}
430+
$lastToken = self::TOKEN_PARAMNAME;
412431
continue;
413432
}
414433
if (isset($match['propValue'])) {
415434
$property['value'] = $match['propValue'];
435+
$lastToken = self::TOKEN_PROPVALUE;
416436
continue;
417437
}
418438
if (isset($match['name']) && 0 < strlen($match['name'])) {
419439
$property['name'] = strtoupper($match['name']);
440+
$lastToken = self::TOKEN_PROPNAME;
420441
continue;
421442
}
422443

tests/VObject/Parser/MimeDirTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,21 @@ public function testPropertyName0(): void
276276
$vevent = $mimeDir->parse($iCal);
277277
self::assertEquals('test', $vevent->VEVENT->{0}->getValue());
278278
}
279+
280+
public function testInvalidParameter(): void
281+
{
282+
$this->expectException(ParseException::class);
283+
$this->expectExceptionMessage('Invalid Mimedir file. Line starting at 3: Missing parameter name for parameter value "value2"');
284+
$vcard = <<<EOF
285+
BEGIN:VCARD
286+
VERSION:4.0
287+
FN;P1=value1; P2=value2:value
288+
UID:1234
289+
END:VCARD
290+
EOF;
291+
$mimeDir = new MimeDir();
292+
$vcard = $mimeDir->parse($vcard);
293+
294+
echo $vcard->serialize();
295+
}
279296
}

0 commit comments

Comments
 (0)