From 34ec83b25ccacad5c523e6b0ad2e156d2107c6e6 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Mon, 22 Mar 2021 21:18:31 +0100 Subject: [PATCH] Show "400 Missing slash" when needed #238 --- lib/cors-anywhere.js | 8 ++++++++ test/test.js | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/cors-anywhere.js b/lib/cors-anywhere.js index f064ddee..eb4f47c5 100644 --- a/lib/cors-anywhere.js +++ b/lib/cors-anywhere.js @@ -316,6 +316,14 @@ function getHandler(options, proxy) { } if (!location) { + // Special case http:/notenoughslashes, because new users of the library frequently make the + // mistake of putting this application behind a server/router that normalizes the URL. + // See https://github.com/Rob--W/cors-anywhere/issues/238#issuecomment-629638853 + if (/^\/https?:\/[^/]/i.test(req.url)) { + res.writeHead(400, 'Missing slash', cors_headers); + res.end('The URL is invalid: two slashes are needed after the http(s):.'); + return; + } // Invalid API call. Show how to correctly use the API showUsage(corsAnywhere.helpFile, cors_headers, res); return; diff --git a/test/test.js b/test/test.js index b08bd225..613978ce 100644 --- a/test/test.js +++ b/test/test.js @@ -141,7 +141,7 @@ describe('Basic functionality', function() { request(cors_anywhere) .get('/http:/notenoughslashes') .expect('Access-Control-Allow-Origin', '*') - .expect(200, helpText, done); + .expect(400, 'The URL is invalid: two slashes are needed after the http(s):.', done); });