Skip to content

Commit

Permalink
Safeguard metadata configuration value against bad filters (#4487)
Browse files Browse the repository at this point in the history
* Improve validation errors for transformer configuration

* Safeguard metadata configuration value against bad filters
  • Loading branch information
schlessera authored and westonruter committed Mar 31, 2020
1 parent 2f2ff28 commit 23a7dfa
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
Expand Up @@ -58,14 +58,14 @@ protected function validate($key, $value)
switch ($key) {
case self::VERSION:
if (! is_string($value)) {
throw InvalidConfigurationValue::forInvalidSubValueType(AmpRuntimeCss::class, self::VERSION, 'string', gettype($value));
throw InvalidConfigurationValue::forInvalidSubValueType(self::class, self::VERSION, 'string', gettype($value));
}
$value = trim($value);
break;

case self::CANARY:
if (! is_bool($value)) {
throw InvalidConfigurationValue::forInvalidSubValueType(AmpRuntimeCss::class, self::CANARY, 'boolean', gettype($value));
throw InvalidConfigurationValue::forInvalidSubValueType(self::class, self::CANARY, 'boolean', gettype($value));
}
break;
}
Expand Down
Expand Up @@ -36,7 +36,7 @@ public function __construct($configuration = [])

foreach ($configuration as $key => $value) {
if (! array_key_exists($key, $this->allowedKeys)) {
throw InvalidConfigurationKey::fromTransformerKey(self::class, $key);
throw InvalidConfigurationKey::fromTransformerKey(static::class, $key);
}
$this->$key = $this->validate($key, $value);
}
Expand All @@ -55,7 +55,7 @@ public function __construct($configuration = [])
public function get($key)
{
if (! array_key_exists($key, $this->allowedKeys)) {
throw UnknownConfigurationKey::fromTransformerKey(self::class, $key);
throw UnknownConfigurationKey::fromTransformerKey(static::class, $key);
}

// At this point, the configuration should either have received this value or filled it with a default.
Expand Down
Expand Up @@ -48,7 +48,7 @@ protected function validate($key, $value)
switch ($key) {
case self::VERSION:
if (! is_int($value)) {
throw InvalidConfigurationValue::forInvalidSubValueType(TransformedIdentifier::class, self::VERSION, 'integer', gettype($value));
throw InvalidConfigurationValue::forInvalidSubValueType(self::class, self::VERSION, 'integer', gettype($value));
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Transformer/AmpSchemaOrgMetadataConfiguration.php
Expand Up @@ -36,7 +36,7 @@ final class AmpSchemaOrgMetadataConfiguration extends BaseTransformerConfigurati
*/
protected function getAllowedKeys() {
return [
self::METADATA => amp_get_schemaorg_metadata(),
self::METADATA => (array) amp_get_schemaorg_metadata(),
];
}

Expand All @@ -52,7 +52,7 @@ protected function validate( $key, $value ) {
switch ( $key ) {
case self::METADATA:
if ( ! is_array( $value ) ) {
throw InvalidConfigurationValue::forInvalidSubValueType( TransformedIdentifier::class, self::METADATA, 'array', gettype( $value ) );
throw InvalidConfigurationValue::forInvalidSubValueType( self::class, self::METADATA, 'array', gettype( $value ) );
}
break;
}
Expand Down

0 comments on commit 23a7dfa

Please sign in to comment.