Skip to content

Commit

Permalink
fix(core-api): handle null url and malformed JSON payloads (#2797)
Browse files Browse the repository at this point in the history
  • Loading branch information
spkjp authored and faustbrian committed Jul 11, 2019
1 parent 009e815 commit 9ea7fed
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
14 changes: 4 additions & 10 deletions packages/core-api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ export class Server {
const options = {
host: this.config.host,
port: this.config.port,
routes: {
validate: {
async failAction(request, h, err) {
throw err;
},
},
},
};

if (this.config.enabled) {
Expand Down Expand Up @@ -140,9 +133,10 @@ export class Server {

// @TODO: remove this with the release of 3.0 - adds support for /api and /api/v2
server.ext("onRequest", (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
const path: string = request.url.pathname.replace("/v2", "");

request.setUrl(request.url.search ? `${path}${request.url.search}` : path);
if (request.url) {
const path: string = request.url.pathname.replace("/v2", "");
request.setUrl(request.url.search ? `${path}${request.url.search}` : path);
}

return h.continue;
});
Expand Down
7 changes: 0 additions & 7 deletions packages/core-elasticsearch/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ export const startServer = async config => {
const server = await createServer({
host: config.host,
port: config.port,
routes: {
validate: {
async failAction(request, h, err) {
throw err;
},
},
},
});

server.route([
Expand Down
1 change: 1 addition & 0 deletions packages/core-http-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@hapi/good-console": "^8.1.0",
"@hapi/good-squeeze": "^5.2.0",
"@hapi/hapi": "^18.3.1",
"deepmerge": "^3.3.0",
"expand-home-dir": "^0.0.3",
"hapi-trailing-slash": "^3.1.0",
"nanomatch": "^1.2.13"
Expand Down
20 changes: 20 additions & 0 deletions packages/core-http-utils/src/server/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Boom from "@hapi/boom";
import Hapi from "@hapi/hapi";
import deepmerge from "deepmerge";
import expandHomeDir from "expand-home-dir";
import { readFileSync } from "fs";
import { monitorServer } from "./monitor";
Expand All @@ -9,6 +11,24 @@ export const createServer = async (options, callback?: any, plugins?: any[]) =>
options.tls.cert = readFileSync(expandHomeDir(options.tls.cert));
}

options = deepmerge(
{
routes: {
payload: {
async failAction(request, h, err) {
return Boom.badData(err.message);
},
},
validate: {
async failAction(request, h, err) {
return Boom.badData(err.message);
},
},
},
},
options,
);

const server = new Hapi.Server(options);

if (Array.isArray(plugins)) {
Expand Down
5 changes: 0 additions & 5 deletions packages/core-webhooks/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ export const startServer = async config => {
port: config.port,
routes: {
cors: true,
validate: {
async failAction(request, h, err) {
throw err;
},
},
},
});

Expand Down

0 comments on commit 9ea7fed

Please sign in to comment.