Skip to content

Commit 2e8415e

Browse files
authored
Merge pull request #299 from oviboc-hybris/master
added toggable security check to only allow loading of Raml Urls whic…
2 parents ea47bc0 + 20cf063 commit 2e8415e

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

src/app/directives/raml-console-loader.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,25 @@
4040
$scope.vm.loaded = false;
4141
$scope.vm.error = void(0);
4242

43-
return ramlParser.loadPath($window.resolveUrl(url), null, $scope.options)
44-
.then(function (raml) {
45-
$scope.vm.raml = raml;
46-
})
47-
.catch(function (error) {
48-
$scope.vm.error = angular.extend(error, {
49-
/*jshint camelcase: false */
50-
buffer: (error.context_mark || error.problem_mark).buffer
51-
/*jshint camelcase: true */
52-
});
53-
})
54-
.finally(function () {
55-
$scope.vm.loaded = true;
56-
})
57-
;
43+
if(RAML.LoaderUtils.ramlOriginValidate(url, $scope.options)) {
44+
$scope.vm.error = {buffer : 'RAML origin check failed. Raml does not reside underneath the path:' + RAML.LoaderUtils.allowedRamlOrigin($scope.options)};
45+
} else {
46+
return ramlParser.loadPath($window.resolveUrl(url), null, $scope.options)
47+
.then(function (raml) {
48+
$scope.vm.raml = raml;
49+
})
50+
.catch(function (error) {
51+
$scope.vm.error = angular.extend(error, {
52+
/*jshint camelcase: false */
53+
buffer: (error.context_mark || error.problem_mark).buffer
54+
/*jshint camelcase: true */
55+
});
56+
})
57+
.finally(function () {
58+
$scope.vm.loaded = true;
59+
})
60+
;
61+
}
5862
}
5963
})
6064
;

src/app/directives/raml-initializer.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
restrict: 'E',
88
templateUrl: 'directives/raml-initializer.tpl.html',
99
replace: true,
10-
controller: 'RamlInitializerController'
10+
controller: 'RamlInitializerController',
11+
scope: {
12+
options: '='
13+
}
1114
};
1215
})
1316
.controller('RamlInitializerController', ['$scope', '$window', 'ramlParser', function RamlInitializerController(
@@ -44,7 +47,13 @@
4447

4548
function loadFromUrl(url) {
4649
$scope.vm.ramlUrl = url;
47-
return loadFromPromise(ramlParser.loadPath($window.resolveUrl(url)), {isLoadingFromUrl: true});
50+
51+
if(RAML.LoaderUtils.ramlOriginValidate(url, $scope.options)) {
52+
$scope.vm.isLoadedFromUrl = true;
53+
$scope.vm.error = {message : 'RAML origin check failed. Raml does not reside underneath the path:' + RAML.LoaderUtils.allowedRamlOrigin($scope.options)};
54+
} else {
55+
return loadFromPromise(ramlParser.loadPath($window.resolveUrl(url)), {isLoadingFromUrl: true});
56+
}
4857
}
4958

5059
function loadFromString(string) {

src/common/loader_utils.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(function() {
2+
'use strict';
3+
4+
RAML.LoaderUtils = {
5+
6+
allowedRamlOrigin : function(options) {
7+
var basepath='../';
8+
if(typeof options.ramlOriginCheck === 'string') {
9+
basepath = options.ramlOriginCheck;
10+
}
11+
return basepath;
12+
},
13+
14+
// prevent loading stuff from other hosts and/or services
15+
ramlOriginValidate: function (url, options) {
16+
var absolutePath = function(href) {
17+
var link = document.createElement('a');
18+
link.href = href;
19+
return link.href;
20+
};
21+
22+
var isSameBasePath = function(href, basepath) {
23+
var absoluteBasepath=absolutePath(basepath);
24+
var absoluteRamlPath=absolutePath(href);
25+
return absoluteRamlPath.indexOf(absoluteBasepath, 0) === 0;
26+
};
27+
28+
var decodedRamlUrl=decodeURIComponent(url);
29+
return options && options.ramlOriginCheck && !isSameBasePath(decodedRamlUrl, RAML.LoaderUtils.allowedRamlOrigin(options));
30+
}
31+
32+
33+
};
34+
})();

0 commit comments

Comments
 (0)