@@ -7,6 +7,7 @@ const express = require('express');
77const path = require ( 'path' ) ;
88const fs = require ( 'fs' ) ;
99const uuid = require ( 'uuid' ) . v4 ;
10+ const escape = require ( 'lodash.escape' ) ;
1011
1112// https://github.tools.sap/sgs/SAP-Global-Trust-List/blob/master/approved.pem
1213const certs = fs . readFileSync ( 'certs.pem' , 'utf8' ) ;
@@ -47,7 +48,7 @@ export const makeHandleRequest = () => {
4748 id : req . id ,
4849 method : req . method ,
4950 url : req . url ,
50- apiServerAddress : req . headers [ 'x-cluster-url' ] ,
51+ apiServerAddress : escape ( req . headers [ 'x-cluster-url' ] ) ,
5152 code : req . code ,
5253 stack : req . stack ,
5354 type : req . type ,
@@ -63,6 +64,7 @@ export const makeHandleRequest = () => {
6364 headersData = extractHeadersData ( req ) ;
6465 } catch ( e ) {
6566 req . log . error ( 'Headers error:' + e . message ) ;
67+ res . contentType ( 'text/plain' ) ;
6668 res . status ( 400 ) . send ( 'Headers are missing or in a wrong format.' ) ;
6769 return ;
6870 }
@@ -71,7 +73,8 @@ export const makeHandleRequest = () => {
7173 filters . forEach ( filter => filter ( req , headersData ) ) ;
7274 } catch ( e ) {
7375 req . log . error ( 'Filters rejected the request: ' + e . message ) ;
74- res . status ( 400 ) . send ( 'Request ID: ' + req . id ) ;
76+ res . contentType ( 'text/plain' ) ;
77+ res . status ( 400 ) . send ( 'Request ID: ' + escape ( req . id ) ) ;
7578 return ;
7679 }
7780
@@ -120,7 +123,10 @@ export const makeHandleRequest = () => {
120123
121124 function throwInternalServerError ( originalError ) {
122125 req . log . warn ( originalError ) ;
123- res . status ( 502 ) . send ( 'Request ID: ' + req . id ) ;
126+ res . contentType ( 'text/plain' ) ;
127+ res
128+ . status ( 502 )
129+ . send ( 'Internal server error. Request ID: ' + escape ( req . id ) ) ;
124130 }
125131 } ;
126132} ;
@@ -143,11 +149,19 @@ function extractHeadersData(req) {
143149 const clientKeyDataHeader = 'x-client-key-data' ;
144150 const authorizationHeader = 'x-k8s-authorization' ;
145151 let targetApiServer ;
152+
146153 if ( req . headers [ urlHeader ] ) {
147- targetApiServer = handleDockerDesktopSubsitution (
148- new URL ( req . headers [ urlHeader ] ) ,
149- ) ;
154+ try {
155+ targetApiServer = handleDockerDesktopSubsitution (
156+ new URL ( req . headers [ urlHeader ] ) ,
157+ ) ;
158+ } catch ( e ) {
159+ throw new Error ( 'Invalid cluster URL provided.' ) ;
160+ }
161+ } else {
162+ throw new Error ( 'Missing required cluster URL.' ) ;
150163 }
164+
151165 const ca = decodeHeaderToBuffer ( req . headers [ caHeader ] ) || certs ;
152166 const cert = decodeHeaderToBuffer ( req . headers [ clientCAHeader ] ) ;
153167 const key = decodeHeaderToBuffer ( req . headers [ clientKeyDataHeader ] ) ;
0 commit comments