Skip to content

Commit

Permalink
Fix url validation in patch import-map, switch to strict-ssl fetch (#177
Browse files Browse the repository at this point in the history
)
  • Loading branch information
joeldenning authored Nov 10, 2024
1 parent 14935c8 commit 870c8d7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 320 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"lodash": "^4.17.21",
"minimist": "^1.2.8",
"morgan": "^1.10.0",
"request": "^2.88.2",
"rwlock": "^5.0.0"
},
"devDependencies": {
Expand Down
38 changes: 17 additions & 21 deletions src/modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,23 @@ exports.modifyImportMap = function (env, newValues) {
: integrity;

// either imports or scopes have to be defined
if (newImports || newScopes || newIntegrity) {
return modifyLock(env, (json) => {
if (newImports) {
const imports = getMapFromManifest(json);
Object.assign(imports, newImports);
}
if (newScopes) {
json.scopes = json.scopes ?? {};
const scopes = getScopesFromManifest(json);
Object.assign(scopes, newScopes);
}
if (newIntegrity) {
json.integrity = json.integrity ?? {};
const integrity = getIntegrityFromManifest(json);
Object.assign(integrity, newIntegrity);
}
return json;
});
} else {
return Promise.resolve();
}
return modifyLock(env, (json) => {
if (newImports) {
const imports = getMapFromManifest(json);
Object.assign(imports, newImports);
}
if (newScopes) {
json.scopes = json.scopes ?? {};
const scopes = getScopesFromManifest(json);
Object.assign(scopes, newScopes);
}
if (newIntegrity) {
json.integrity = json.integrity ?? {};
const integrity = getIntegrityFromManifest(json);
Object.assign(integrity, newIntegrity);
}
return json;
});
};

exports.modifyService = function (
Expand Down
15 changes: 3 additions & 12 deletions src/verify-valid-url.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const util = require("util");
const request = require("request");
const requestAsPromise = util.promisify(request);

async function verifyValidUrl(req, url) {
if (req.query.skip_url_check === "true" || req.query.skip_url_check === "") {
// ?skip_url_check
Expand All @@ -10,15 +6,10 @@ async function verifyValidUrl(req, url) {
} else {
// ?skip_url_check=false
// ?<no param>
try {
const resp = await requestAsPromise({ url, strictSSL: false });
if (resp.statusCode < 200 || resp.statusCode >= 400) {
throw Error(resp.statusCode);
}
return true;
} catch (err) {
const r = await fetch(url);
if (!r.ok) {
throw Error(
`The following url in the request body is not reachable: ${url}`
`The following url in the request body is not reachable: ${url} ${r.status} ${r.statusText}`
);
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/web-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ app.patch("/import-map.json", (req, res) => {
}

// Import map validation
let validImportUrlPromises = Promise.resolve();
let validImportUrlPromises = [];
if (req.body.imports) {
const importUrlsToValidate = findUrlsToValidateInServices(req.body.imports);
const unsafeUrls = importUrlsToValidate.map(checkUrlUnsafe).filter(Boolean);
Expand All @@ -181,7 +181,7 @@ app.patch("/import-map.json", (req, res) => {
}

// Scope validation
let validScopeUrlPromises = Promise.resolve();
let validScopeUrlPromises = [];
if (req.body.scopes) {
const scopeUrlsToValidate = findUrlsToValidateInScopes(req.body.scopes);
const unsafeUrls = scopeUrlsToValidate.map(checkUrlUnsafe).filter(Boolean);
Expand All @@ -199,7 +199,7 @@ app.patch("/import-map.json", (req, res) => {
}
}

let validIntegrityUrlPromises = Promise.resolve();
let validIntegrityUrlPromises = [];
if (req.body.integrity) {
const integrityUrlsToValidate = findUrlsToValidateInIntegrity(
req.body.integrity
Expand All @@ -222,9 +222,9 @@ app.patch("/import-map.json", (req, res) => {
}

return Promise.all([
validImportUrlPromises,
validScopeUrlPromises,
validIntegrityUrlPromises,
...validImportUrlPromises,
...validScopeUrlPromises,
...validIntegrityUrlPromises,
])
.then(() => {
modify
Expand All @@ -234,7 +234,6 @@ app.patch("/import-map.json", (req, res) => {
integrity: req.body.integrity,
})
.then((newImportMap) => {
console.log(`Patched import map. New import map`, newImportMap);
res.status(200).send(newImportMap);
})
.catch((err) => {
Expand Down
Loading

0 comments on commit 870c8d7

Please sign in to comment.