php: validate and normalize metadata keys

pull/3790/head
Stanley Cheung 9 years ago
parent abaf47c3a5
commit b0cd08acab
  1. 25
      src/php/lib/Grpc/BaseStub.php
  2. 8
      src/php/tests/generated_code/AbstractGeneratedCodeTest.php

@ -114,7 +114,7 @@ class BaseStub {
return true; return true;
} }
if ($new_state == \Grpc\CHANNEL_FATAL_FAILURE) { if ($new_state == \Grpc\CHANNEL_FATAL_FAILURE) {
throw new Exception('Failed to connect to server'); throw new \Exception('Failed to connect to server');
} }
return false; return false;
} }
@ -153,6 +153,25 @@ class BaseStub {
return array($metadata_copy, $timeout); return array($metadata_copy, $timeout);
} }
/**
* validate and normalize the metadata array
* @param $metadata The metadata map
* @return $metadata Validated and key-normalized metadata map
* @throw InvalidArgumentException if key contains invalid characters
*/
private function _validate_and_normalize_metadata($metadata) {
$metadata_copy = array();
foreach ($metadata as $key => $value) {
if (!preg_match('/^[A-Za-z\d_-]+$/', $key)) {
throw new \InvalidArgumentException(
'Metadata keys must be nonempty strings containing only '.
'alphanumeric characters and hyphens');
}
$metadata_copy[strtolower($key)] = $value;
}
return $metadata_copy;
}
/* This class is intended to be subclassed by generated code, so all functions /* This class is intended to be subclassed by generated code, so all functions
begin with "_" to avoid name collisions. */ begin with "_" to avoid name collisions. */
@ -178,6 +197,7 @@ class BaseStub {
$actual_metadata, $actual_metadata,
$jwt_aud_uri); $jwt_aud_uri);
} }
$actual_metadata = $this->_validate_and_normalize_metadata($actual_metadata);
$call->start($argument, $actual_metadata, $options); $call->start($argument, $actual_metadata, $options);
return $call; return $call;
} }
@ -204,6 +224,7 @@ class BaseStub {
$actual_metadata, $actual_metadata,
$jwt_aud_uri); $jwt_aud_uri);
} }
$actual_metadata = $this->_validate_and_normalize_metadata($actual_metadata);
$call->start($actual_metadata); $call->start($actual_metadata);
return $call; return $call;
} }
@ -231,6 +252,7 @@ class BaseStub {
$actual_metadata, $actual_metadata,
$jwt_aud_uri); $jwt_aud_uri);
} }
$actual_metadata = $this->_validate_and_normalize_metadata($actual_metadata);
$call->start($argument, $actual_metadata, $options); $call->start($argument, $actual_metadata, $options);
return $call; return $call;
} }
@ -254,6 +276,7 @@ class BaseStub {
$actual_metadata, $actual_metadata,
$jwt_aud_uri); $jwt_aud_uri);
} }
$actual_metadata = $this->_validate_and_normalize_metadata($actual_metadata);
$call->start($actual_metadata); $call->start($actual_metadata);
return $call; return $call;
} }

@ -51,6 +51,14 @@ abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(is_string(self::$client->getTarget())); $this->assertTrue(is_string(self::$client->getTarget()));
} }
/**
* @expectedException InvalidArgumentException
*/
public function testInvalidMetadata() {
$div_arg = new math\DivArgs();
$call = self::$client->Div($div_arg, array(' ' => 'abc123'));
}
public function testWriteFlags() { public function testWriteFlags() {
$div_arg = new math\DivArgs(); $div_arg = new math\DivArgs();
$div_arg->setDividend(7); $div_arg->setDividend(7);

Loading…
Cancel
Save