Skip to content

Commit

Permalink
Merge pull request #120 from single-spa/preventing-falsy-keys
Browse files Browse the repository at this point in the history
Preventing "" as a service key
  • Loading branch information
TheMcMurder authored Jun 25, 2021
2 parents 19a48ab + 8988264 commit 3053a4b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/verify-valid-input-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ exports.verifyInputFormatForServices = function (services) {
errorsToReport.push(
`Invalid import map in request body -- module with name '${moduleName}' does not have a string url`
);
} else if (!moduleName || moduleName.trim().length === 0) {
// catch times where the name evaluates to false, such as "".
errorsToReport.push(
`Invalid module name -- module name '${moduleName}' is invalid`
);
} else {
if (moduleName.endsWith("/") && !services[moduleName].endsWith("/")) {
errorsToReport.push(
Expand Down
5 changes: 5 additions & 0 deletions src/web-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ app.patch("/services", function (req, res) {
} else {
return res.status(400).send("service key is missing");
}
if (!service || service.trim().length === 0) {
return res
.status(400)
.send(`Invalid service key - "${service}" is an invalid service key`);
}
if (req.body != undefined && req.body.hasOwnProperty("url")) {
url = req.body.url;
} else {
Expand Down
29 changes: 29 additions & 0 deletions test/import-map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,33 @@ describe(`/import-map.json`, () => {
.set("accept", "json")
.expect(404);
});

it(`returns a 400 when you attempt to patch the import map with an invalid name`, async () => {
await request(app)
.patch("/import-map.json")
.query({
skip_url_check: true,
})
.set("accept", "json")
.send({
imports: {
"": "/something.js",
},
})
.expect(400);
});

it(`returns a 400 when you attempt to patch a service with an invalid name`, async () => {
await request(app)
.patch("/services")
.query({
skip_url_check: true,
})
.set("accept", "json")
.send({
service: "",
url: "/a-1-updated.mjs",
})
.expect(400);
});
});

0 comments on commit 3053a4b

Please sign in to comment.