METAR parsing infinite loop bugfix #599 (#600)

METAR parsing infinite loop bugfix #599
This commit is contained in:
Nabeel S 2020-02-28 22:50:41 -05:00 committed by GitHub
parent b0f122a301
commit ea9ee985e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 3 deletions

View File

@ -4,6 +4,8 @@ namespace App\Services\Metar;
use App\Contracts\Metar; use App\Contracts\Metar;
use App\Support\HttpClient; use App\Support\HttpClient;
use function count;
use Exception;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
/** /**
@ -44,15 +46,31 @@ class AviationWeather extends Metar
try { try {
$res = $this->httpClient->get($url, []); $res = $this->httpClient->get($url, []);
$xml = simplexml_load_string($res); $xml = simplexml_load_string($res);
if (\count($xml->data->METAR->raw_text) === 0) {
$attrs = $xml->data->attributes();
if (!isset($attrs['num_results'])) {
return '';
}
$num_results = $attrs['num_results'];
if (empty($num_results)) {
return '';
}
$num_results = (int) $num_results;
if ($num_results === 0) {
return '';
}
if (count($xml->data->METAR->raw_text) === 0) {
return ''; return '';
} }
return $xml->data->METAR->raw_text->__toString(); return $xml->data->METAR->raw_text->__toString();
} catch (\Exception $e) { } catch (Exception $e) {
Log::error('Error reading METAR: '.$e->getMessage()); Log::error('Error reading METAR: '.$e->getMessage());
throw $e; return '';
} }
} }
} }

View File

@ -19,6 +19,7 @@ class Weather extends Widget
*/ */
public function run() public function run()
{ {
/** @var \App\Services\AirportService $airportSvc */
$airportSvc = app(AirportService::class); $airportSvc = app(AirportService::class);
$metar = $airportSvc->getMetar($this->config['icao']); $metar = $airportSvc->getMetar($this->config['icao']);

View File

@ -185,4 +185,15 @@ class MetarTest extends TestCase
$this->assertNull($airportSvc->getMetar('idk')); $this->assertNull($airportSvc->getMetar('idk'));
} }
public function testHttpCallUnknown()
{
$this->mockXmlResponse('aviationweather/unknown.xml');
/** @var AirportService $airportSvc */
$airportSvc = app(AirportService::class);
$metar = $airportSvc->getMetar('7AK4');
$this->assertNull($metar);
}
} }

View File