Skip to content

Commit d7295a8

Browse files
committed
feat: Add blacklisting of endpoints with invalid media types
- Fixes #543 and #540
1 parent e16f547 commit d7295a8

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/JsonSchema/Uri/UriRetriever.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class UriRetriever implements BaseUriRetrieverInterface
3232
'|^https?://json-schema.org/draft-(0[34])/schema#?|' => 'package://dist/schema/json-schema-draft-$1.json'
3333
);
3434

35+
/**
36+
* @var array A blacklist for media type ceheck exclusion
37+
*/
38+
protected $mediaTypeBlacklist = array(
39+
'http://json-schema.org/',
40+
'https://json-schema.org/'
41+
);
42+
3543
/**
3644
* @var null|UriRetrieverInterface
3745
*/
@@ -44,6 +52,16 @@ class UriRetriever implements BaseUriRetrieverInterface
4452
*/
4553
private $schemaCache = array();
4654

55+
/**
56+
* Adds an endpoint to the media type validation blacklist
57+
*
58+
* @param string $endpoint
59+
*/
60+
public function addBlacklistedEndpoint($endpoint)
61+
{
62+
$this->mediaTypeBlacklist[] = $endpoint;
63+
}
64+
4765
/**
4866
* Guarantee the correct media type was encountered
4967
*
@@ -65,9 +83,10 @@ public function confirmMediaType($uriRetriever, $uri)
6583
return;
6684
}
6785

68-
if (substr($uri, 0, 23) == 'http://json-schema.org/') {
69-
//HACK; they deliver broken content types
70-
return true;
86+
for ($i = 0, $iMax = count($this->mediaTypeBlacklist); $i < $iMax; $i++) {
87+
if (stripos($uri, $this->mediaTypeBlacklist[$i]) === 0) {
88+
return true;
89+
}
7190
}
7291

7392
throw new InvalidSchemaMediaTypeException(sprintf('Media type %s expected', Validator::SCHEMA_MEDIA_TYPE));

tests/Uri/UriRetrieverTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public function testRetrieveSchemaFromPackage()
330330
$this->assertEquals('454f423bd7edddf0bc77af4130ed9161', md5(json_encode($schema)));
331331
}
332332

333-
public function testJsonSchemaOrgMediaTypeHack()
333+
public function testJsonSchemaOrgMediaTypeBlacklistDefault()
334334
{
335335
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
336336
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
@@ -339,6 +339,28 @@ public function testJsonSchemaOrgMediaTypeHack()
339339
$this->assertTrue($retriever->confirmMediaType($mock, 'http://json-schema.org/'));
340340
}
341341

342+
/**
343+
* @expectedException \JsonSchema\Exception\InvalidSchemaMediaTypeException
344+
*/
345+
public function testJsonSchemaOrgMediaTypeBlacklistUnknown()
346+
{
347+
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
348+
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
349+
$retriever = new UriRetriever();
350+
351+
$retriever->confirmMediaType($mock, 'http://iglucentral.com');
352+
}
353+
354+
public function testJsonSchemaOrgMediaTypeBlacklistAdded()
355+
{
356+
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
357+
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
358+
$retriever = new UriRetriever();
359+
$retriever->addBlacklistedEndpoint('http://iglucentral.com');
360+
361+
$retriever->confirmMediaType($mock, 'http://iglucentral.com');
362+
}
363+
342364
public function testSchemaCache()
343365
{
344366
$retriever = new UriRetriever();

0 commit comments

Comments
 (0)