Skip to content

Commit e4204e5

Browse files
chore: fix url check (#220)
allow also other urls like: ../complete-api.yaml this fixes our techsys apis: (https://github.com/TechSysApi/vsi-process-control-stream-api-dist/tree/master?tab=readme-ov-file) which are broken since broken since [v7.7.1](https://techsysapi.github.io/vsi-process-control-stream-api-dist/v7.7.1/ui/?url=../complete-api.yaml) due to the commit: (07bf790)
2 parents bca572e + e423fad commit e4204e5

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/deps/helper.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ $ = function (f) {
2929

3030
Handlebars.templates.signature = Handlebars.compile('{{sanitize signature}}');
3131

32-
var url = "../model/openapi/api.yaml";
33-
if(window.location.search !="?url="+url) {
34-
window.location.search = "?url="+url;
35-
}
32+
var url = new URL(window.location).searchParams.get('url')
33+
3634
if (!url) {
3735
alert('Please specify the API to display using the "url" query parameter.\nE.g. ' + location.origin + location.pathname + '?url=/src/openapi/api.yaml');
3836
return;
3937
}
38+
39+
var fallbackUrl = "../model/openapi/api.yaml";
40+
if(!isValidUrl(url)) {
41+
window.location.search = "?url="+fallbackUrl;
42+
}
43+
4044
if (!window.fetch) {
4145
alert('Please use a Browser.\nIt should at least support "fetch".');
4246
return;
@@ -112,6 +116,22 @@ $ = function (f) {
112116
)
113117
}
114118

119+
function isValidUrl(referencedUrl){
120+
// must be relative url
121+
if(!referencedUrl.startsWith('../')){
122+
return false;
123+
}
124+
// only allow referencing max two parent directories
125+
var matches = referencedUrl.match(/\.\.\//g);
126+
if(matches && matches.length > 2){
127+
return false;
128+
}
129+
130+
// additional check
131+
var referencedAbsoluteUrl = getAbsoluteUrl(referencedUrl)
132+
return new URL(referencedAbsoluteUrl).origin == new URL(location.href).origin
133+
}
134+
115135
function isLocalSchema(models, schema) {
116136
return _.any(models, function (m) {
117137
return normalize(schema.extra.filename) === normalize(m);

0 commit comments

Comments
 (0)