jenkins-bot has submitted this change and it was merged.
Change subject: Validate adherence to 'enum' constraint in server-side logging
......................................................................
Validate adherence to 'enum' constraint in server-side logging
This patch ensures enum constraints are enforced when validating server-side
events in PHP.
Bug: 49096
Change-Id: I700ec58de1be6de01093cc57b5ec6f4b23429f3d
---
M includes/JsonSchema.i18n.php
M includes/JsonSchema.php
M tests/EventLoggingExtensionFunctionsTest.php
3 files changed, 24 insertions(+), 3 deletions(-)
Approvals:
Spage: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/JsonSchema.i18n.php b/includes/JsonSchema.i18n.php
index a9bff44..582cd6f 100644
--- a/includes/JsonSchema.i18n.php
+++ b/includes/JsonSchema.i18n.php
@@ -21,6 +21,7 @@
'jsonschema-invalidempty' => 'Empty data structure not valid with this
schema',
'jsonschema-invalidnode' => 'Invalid node: expecting "$1", got "$2".
Path: "$3"',
'jsonschema-invalid-missingfield' => 'Missing required field "$1"',
+ 'jsonschema-invalid-notinenum' => 'Value "$1" not in enum for property
$2',
);
/** Message documentation (Message documentation)
@@ -33,6 +34,7 @@
'jsonschema-invalidempty' => 'JSON Schema validation error, shown when
attempting to validate empty object against a schema that does not allow empty
objects.',
'jsonschema-invalidnode' => 'JSON Schema validation error, shown when
object node does not match expected type.',
'jsonschema-invalid-missingfield' => 'JSON Schema validation error,
shown when a required field is missing.',
+ 'jsonschema-invalid-notinenum' => 'JSON Schema validation error, shown
when a value is not in the set of permitted values for a field.',
);
/** Asturian (asturianu)
diff --git a/includes/JsonSchema.php b/includes/JsonSchema.php
index e9f6186..1b2a1f3 100644
--- a/includes/JsonSchema.php
+++ b/includes/JsonSchema.php
@@ -397,6 +397,13 @@
* Return true on success, and throw a JsonSchemaException on failure.
*/
public function validate() {
+ if( array_key_exists( 'enum', $this->schemaref->node ) &&
+ !in_array( $this->node, $this->schemaref->node['enum']
) ) {
+ $msg = JsonUtil::uiMessage(
'jsonschema-invalid-notinenum', $this->node, $this->getDataPathTitles() );
+ $e = new JsonSchemaException( $msg );
+ $e->subtype = "validate-fail";
+ throw( $e );
+ }
$datatype = JsonUtil::getType( $this->node );
$schematype = $this->getType();
if ( $datatype == 'array' && $schematype == 'object' ) {
diff --git a/tests/EventLoggingExtensionFunctionsTest.php
b/tests/EventLoggingExtensionFunctionsTest.php
index f329452..120eb9c 100644
--- a/tests/EventLoggingExtensionFunctionsTest.php
+++ b/tests/EventLoggingExtensionFunctionsTest.php
@@ -21,15 +21,27 @@
'valid' => array(
'type' => 'boolean',
'required' => true,
- )
+ ),
+ 'action' => array(
+ 'type' => 'string',
+ 'enum' => array(
+ 'delete',
+ 'edit',
+ 'history',
+ 'protect',
+ 'purge',
+ 'submit',
+ 'view',
+ ),
+ ),
)
);
/** @var array: conforms to $validSchema. **/
- static $validObject = array( 'valid' => true );
+ static $validObject = array( 'valid' => true, 'action' => 'history' );
/** @var array: does not conform to $validSchema. **/
- static $invalidObject = array( 'invalid' => 'yes' );
+ static $invalidObject = array( 'valid' => true, 'action' => 'cache' );
const UGLY_JSON = '{"nested":{"value":"{}"}}';
--
To view, visit https://gerrit.wikimedia.org/r/70062
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I700ec58de1be6de01093cc57b5ec6f4b23429f3d
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>
Gerrit-Reviewer: Mattflaschen <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: Spage <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits