Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing "" as a service key #120

Merged
merged 3 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});