Skip to content

Commit

Permalink
Account for non-xml submission objects
Browse files Browse the repository at this point in the history
When given a non-xml submission file, catch the exception of the bad
submission and only reject the submission of the Validate setting is enabled.

Fixes: #2662
  • Loading branch information
josephsnyder committed Jan 15, 2025
1 parent 9fd9dcc commit 0f93331
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
31 changes: 21 additions & 10 deletions app/Http/Controllers/SubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,28 @@ private function submitProcess(): Response

// Figure out what type of XML file this is.
$stored_filename = 'inbox/' . $filename;
$xml_info = SubmissionUtils::get_xml_type(fopen(Storage::path($stored_filename), 'r'), $stored_filename);

// If validation is enabled and if this file has a corresponding schema, validate it
$validation_errors = $xml_info['xml_handler']::validate(storage_path('app/' . $stored_filename));
if (count($validation_errors) > 0) {
$error_string = implode(PHP_EOL, $validation_errors);

// We always log validation failures, but we only send messages back to the client if configured to do so
Log::warning("Submission validation failed for file '$filename':" . PHP_EOL);
$xml_info = [];
try {
$xml_info = SubmissionUtils::get_xml_type(fopen(Storage::path($stored_filename), 'r'), $stored_filename);
} catch (BadSubmissionException $e) {
$xml_info['xml_handler'] = '';
$message = "Could not determine submission file type for: '{$stored_filename}'";
Log::warning($message . PHP_EOL);
if ((bool) config('cdash.validate_xml_submissions') === true) {
abort(400, "XML validation failed: rejected file $filename:" . PHP_EOL . $error_string);
abort(400, $message);
}
}
if ($xml_info['xml_handler'] !== '') {
// If validation is enabled and if this file has a corresponding schema, validate it
$validation_errors = $xml_info['xml_handler']::validate(storage_path('app/' . $stored_filename));
if (count($validation_errors) > 0) {
$error_string = implode(PHP_EOL, $validation_errors);

// We always log validation failures, but we only send messages back to the client if configured to do so
Log::warning("Submission validation failed for file '$filename':" . PHP_EOL);
if ((bool) config('cdash.validate_xml_submissions') === true) {
abort(400, "XML validation failed: rejected file $filename:" . PHP_EOL . $error_string);
}
}
}

Expand Down
3 changes: 0 additions & 3 deletions app/Jobs/ProcessSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use AbstractSubmissionHandler;
use ActionableBuildInterface;
use App\Exceptions\BadSubmissionException;
use App\Models\SuccessfulJob;
use App\Utils\UnparsedSubmissionProcessor;
use BuildPropertiesJSONHandler;
Expand Down Expand Up @@ -134,7 +133,6 @@ private function requeueSubmissionFile($buildid): bool
*
* @return void
*
* @throws BadSubmissionException
*/
public function handle()
{
Expand Down Expand Up @@ -199,7 +197,6 @@ public function failed(Throwable $exception): void
* This method could be running on a worker that is either remote or local, so it accepts
* a file handle or a filename that it can query the CDash API for.
*
* @throws BadSubmissionException
**/
private function doSubmit($filename, $projectid, $buildid = null, $expected_md5 = ''): AbstractSubmissionHandler|UnparsedSubmissionProcessor|false
{
Expand Down
17 changes: 13 additions & 4 deletions app/cdash/include/ctestparser.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,6 @@ function parse_put_submission($filehandler, $projectid, $expected_md5, ?int $bui

/**
* Main function to parse the incoming xml from ctest
*
* @throws BadSubmissionException
*/
function ctest_parse($filehandle, string $filename, $projectid, $expected_md5 = '', ?int $buildid = null): AbstractSubmissionHandler|false
{
Expand All @@ -185,9 +183,20 @@ function ctest_parse($filehandle, string $filename, $projectid, $expected_md5 =

$Project = new Project();
$Project->Id = $projectid;

$xml_info = [];
// Figure out what type of XML file this is.
$xml_info = SubmissionUtils::get_xml_type($filehandle, $filename);
try {
$xml_info = SubmissionUtils::get_xml_type($filehandle, $filename);
} catch (BadSubmissionException $e) {
$xml_info['file_handle'] = $filehandle;
$xml_info['xml_handler'] = null;
$xml_info['xml_type'] = '';
$message = "Could not determine submission file type for: '{$filename}'";
Log::warning($message . PHP_EOL);
if ((bool) config('cdash.validate_xml_submissions') === true) {
abort(400, $message);
}
}
$filehandle = $xml_info['file_handle'];
$handler_ref = $xml_info['xml_handler'];
$file = $xml_info['xml_type'];
Expand Down
15 changes: 0 additions & 15 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14002,21 +14002,6 @@ parameters:
count: 1
path: app/cdash/include/ctestparser.php

-
message: "#^Parameter \\#1 \\$stream of function feof expects resource, mixed given\\.$#"
count: 1
path: app/cdash/include/ctestparser.php

-
message: "#^Parameter \\#1 \\$stream of function fread expects resource, mixed given\\.$#"
count: 2
path: app/cdash/include/ctestparser.php

-
message: "#^Parameter \\#1 \\$stream of function rewind expects resource, mixed given\\.$#"
count: 1
path: app/cdash/include/ctestparser.php

-
message: "#^Parameter \\#2 \\$data of function xml_parse expects string, string\\|false given\\.$#"
count: 2
Expand Down

0 comments on commit 0f93331

Please sign in to comment.