Skip to content

Commit

Permalink
chore: fix url check (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
steffstefferson authored Nov 15, 2024
2 parents bca572e + e423fad commit e4204e5
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/deps/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ $ = function (f) {

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

var url = "../model/openapi/api.yaml";
if(window.location.search !="?url="+url) {
window.location.search = "?url="+url;
}
var url = new URL(window.location).searchParams.get('url')

if (!url) {
alert('Please specify the API to display using the "url" query parameter.\nE.g. ' + location.origin + location.pathname + '?url=/src/openapi/api.yaml');
return;
}

var fallbackUrl = "../model/openapi/api.yaml";
if(!isValidUrl(url)) {
window.location.search = "?url="+fallbackUrl;
}

if (!window.fetch) {
alert('Please use a Browser.\nIt should at least support "fetch".');
return;
Expand Down Expand Up @@ -112,6 +116,22 @@ $ = function (f) {
)
}

function isValidUrl(referencedUrl){
// must be relative url
if(!referencedUrl.startsWith('../')){
return false;
}
// only allow referencing max two parent directories
var matches = referencedUrl.match(/\.\.\//g);
if(matches && matches.length > 2){
return false;
}

// additional check
var referencedAbsoluteUrl = getAbsoluteUrl(referencedUrl)
return new URL(referencedAbsoluteUrl).origin == new URL(location.href).origin
}

function isLocalSchema(models, schema) {
return _.any(models, function (m) {
return normalize(schema.extra.filename) === normalize(m);
Expand Down

0 comments on commit e4204e5

Please sign in to comment.