-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an API spec validator to report parameter differences.
- Loading branch information
Showing
6 changed files
with
121 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
// api_specvalidator.php -- HotCRP API spec validator | ||
// Copyright (c) 2008-2024 Eddie Kohler; see LICENSE. | ||
|
||
class SpecValidator_API { | ||
const F_REQUIRED = 1; | ||
const F_BODY = 2; | ||
|
||
static function run($uf, Qrequest $qreq) { | ||
$known = []; | ||
foreach ($uf->parameters ?? [] as $p) { | ||
$flags = self::F_REQUIRED; | ||
for ($i = 0; $i !== strlen($p); ++$i) { | ||
if ($p[$i] === "?") { | ||
$flags &= ~self::F_REQUIRED; | ||
} else if ($p[$i] === "=") { | ||
$flags |= self::F_BODY; | ||
} else { | ||
break; | ||
} | ||
} | ||
$n = substr($p, $i); | ||
$known[$n] = $flags; | ||
if (($flags & self::F_REQUIRED) !== 0 | ||
&& !$qreq->has($n)) { | ||
self::error($qreq, "required parameter `{$n}` missing"); | ||
} | ||
} | ||
$param = []; | ||
foreach (array_keys($_GET) as $n) { | ||
if (!isset($known[$n])) { | ||
if (!in_array($n, ["post", "base", "fn", "_"]) | ||
&& ($n !== "p" || !($uf->paper ?? false)) | ||
&& !isset($known["*"])) { | ||
self::error($qreq, "query param `{$n}` unknown"); | ||
} | ||
} else if (($known[$n] & self::F_BODY) !== 0) { | ||
self::error($qreq, "query param `{$n}` should be in body"); | ||
} | ||
} | ||
foreach (array_keys($_POST) as $n) { | ||
if (!isset($known[$n])) { | ||
if (!isset($known["*"])) { | ||
self::error($qreq, "post param `{$n}` unknown"); | ||
} | ||
} else if (!isset($_GET[$n]) | ||
&& ($known[$n] & self::F_BODY) === 0) { | ||
self::error($qreq, "post param `{$n}` should be in query"); | ||
} | ||
} | ||
} | ||
|
||
static function error(Qrequest $qreq, $error) { | ||
$nav = $qreq->navigation(); | ||
$url = substr($nav->self(), 0, 100); | ||
$out = $qreq->conf()->opt("validateApiSpec"); | ||
if (is_string($out)) { | ||
@file_put_contents($out, "{$url}: {$error}\n", FILE_APPEND); | ||
} else { | ||
error_log("{$url}: {$error}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters