Skip to content

Commit 26e8d8c

Browse files
committed
Fix wrong combined paths when traversing upwards. Fixes #557
1 parent 4c74da5 commit 26e8d8c

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/JsonSchema/Uri/UriResolver.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,37 @@ public function resolve($uri, $baseUri = null)
125125
public static function combineRelativePathWithBasePath($relativePath, $basePath)
126126
{
127127
$relativePath = self::normalizePath($relativePath);
128-
if ($relativePath == '') {
128+
if (!$relativePath) {
129129
return $basePath;
130130
}
131-
if ($relativePath[0] == '/') {
131+
if ($relativePath[0] === '/') {
132132
return $relativePath;
133133
}
134-
135-
$basePathSegments = explode('/', $basePath);
136-
137-
preg_match('|^/?(\.\./(?:\./)*)*|', $relativePath, $match);
138-
$numLevelUp = strlen($match[0]) /3 + 1;
139-
if ($numLevelUp >= count($basePathSegments)) {
134+
if (!$basePath) {
140135
throw new UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath));
141136
}
142137

143-
$basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp);
144-
$path = preg_replace('|^/?(\.\./(\./)*)*|', '', $relativePath);
138+
$dirname = $basePath[strlen($basePath) - 1] === '/' ? $basePath : dirname($basePath);
139+
$combined = rtrim($dirname, '/') . '/' . ltrim($relativePath, '/');
140+
$combinedSegments = explode('/', $combined);
141+
$collapsedSegments = array();
142+
while ($combinedSegments) {
143+
$segment = array_shift($combinedSegments);
144+
if ($segment === '..') {
145+
if (count($collapsedSegments) <= 1) {
146+
// Do not remove the top level (domain)
147+
// This is not ideal - the domain should not be part of the path here. parse() and generate()
148+
// should handle the "domain" separately, like the schema.
149+
// Then the if-condition here would be `if (!$collapsedSegments) {`.
150+
throw new UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath));
151+
}
152+
array_pop($collapsedSegments);
153+
} else {
154+
$collapsedSegments[] = $segment;
155+
}
156+
}
145157

146-
return implode('/', $basePathSegments) . '/' . $path;
158+
return implode('/', $collapsedSegments);
147159
}
148160

149161
/**

tests/Uri/UriResolverTest.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class UriResolverTest extends TestCase
99
{
10+
/**
11+
* @var UriResolver
12+
*/
13+
private $resolver;
14+
1015
public function setUp()
1116
{
1217
$this->resolver = new UriResolver();
@@ -83,6 +88,21 @@ public function testCombineRelativePathWithBasePathNoPath()
8388
);
8489
}
8590

91+
/**
92+
* Covers https://github.com/justinrainbow/json-schema/issues/557
93+
* Relative paths yield wrong result.
94+
*/
95+
public function testCombineRelativePathWithBasePathTraversingUp()
96+
{
97+
$this->assertEquals(
98+
'/var/packages/schema/UuidSchema.json',
99+
UriResolver::combineRelativePathWithBasePath(
100+
'../../../schema/UuidSchema.json',
101+
'/var/packages/foo/tests/UnitTests/DemoData/../../../schema/Foo/FooSchema_latest.json'
102+
)
103+
);
104+
}
105+
86106
public function testResolveAbsoluteUri()
87107
{
88108
$this->assertEquals(
@@ -99,12 +119,9 @@ public function testResolveAbsoluteUri()
99119
*/
100120
public function testResolveRelativeUriNoBase()
101121
{
102-
$this->assertEquals(
103-
'http://example.org/foo/bar.json',
104-
$this->resolver->resolve(
105-
'bar.json',
106-
null
107-
)
122+
$this->resolver->resolve(
123+
'bar.json',
124+
null
108125
);
109126
}
110127

0 commit comments

Comments
 (0)