1616 * specific language governing permissions and limitations
1717 * under the License.
1818 */
19- import { Server , ServerRoute } from '@hapi/hapi' ;
19+ import { Server } from '@hapi/hapi' ;
2020import HapiStaticFiles from '@hapi/inert' ;
2121import url from 'url' ;
2222import uuid from 'uuid' ;
@@ -167,21 +167,28 @@ export class HttpServer {
167167 for ( const router of this . registeredRouters ) {
168168 for ( const route of router . getRoutes ( ) ) {
169169 this . log . debug ( `registering route handler for [${ route . path } ]` ) ;
170+ // Hapi does not allow payload validation to be specified for 'head' or 'get' requests
171+ const validate = isSafeMethod ( route . method ) ? undefined : { payload : true } ;
170172 const { authRequired, tags, body = { } , timeout } = route . options ;
171173 const { accepts : allow , maxBytes, output, parse } = body ;
172174
173175 const kibanaRouteOptions : KibanaRouteOptions = {
174176 xsrfRequired : route . options . xsrfRequired ?? ! isSafeMethod ( route . method ) ,
175177 } ;
176178
177- const routeOpts : ServerRoute = {
179+ this . server . route ( {
178180 handler : route . handler ,
179181 method : route . method ,
180182 path : route . path ,
181183 options : {
182184 auth : this . getAuthOption ( authRequired ) ,
183185 app : kibanaRouteOptions ,
184186 tags : tags ? Array . from ( tags ) : undefined ,
187+ // TODO: This 'validate' section can be removed once the legacy platform is completely removed.
188+ // We are telling Hapi that NP routes can accept any payload, so that it can bypass the default
189+ // validation applied in ./http_tools#getServerOptions
190+ // (All NP routes are already required to specify their own validation in order to access the payload)
191+ validate,
185192 // @ts -expect-error Types are outdated and doesn't allow `payload.multipart` to be `true`
186193 payload : [ allow , maxBytes , output , parse , timeout ?. payload ] . some ( ( x ) => x !== undefined )
187194 ? {
@@ -197,22 +204,7 @@ export class HttpServer {
197204 socket : timeout ?. idleSocket ?? this . config ! . socketTimeout ,
198205 } ,
199206 } ,
200- } ;
201-
202- // Hapi does not allow payload validation to be specified for 'head' or 'get' requests
203- if ( ! isSafeMethod ( route . method ) ) {
204- // TODO: This 'validate' section can be removed once the legacy platform is completely removed.
205- // We are telling Hapi that NP routes can accept any payload, so that it can bypass the default
206- // validation applied in ./http_tools#getServerOptions
207- // (All NP routes are already required to specify their own validation in order to access the payload)
208- // TODO: Move the setting of the validate option back up to being set at `routeOpts` creation-time once
209- // https://github.com/hapijs/hoek/pull/365 is merged and released in @hapi/hoek v9.1.1. At that point I
210- // imagine the ts-error below will go away as well.
211- // @ts -expect-error "Property 'validate' does not exist on type 'RouteOptions'" <-- ehh?!? yes it does!
212- routeOpts . options ! . validate = { payload : true } ;
213- }
214-
215- this . server . route ( routeOpts ) ;
207+ } ) ;
216208 }
217209 }
218210
0 commit comments