From bdf876ddcef7b7dca37c4031a81f40da120870d6 Mon Sep 17 00:00:00 2001 From: derberg Date: Fri, 13 Nov 2020 09:42:14 +0100 Subject: [PATCH] fix: channels with name / fail on validation --- lib/utils.js | 4 +- test/customValidators_test.js | 76 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 6da472f53..0c001ec11 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -268,10 +268,8 @@ utils.parseUrlVariables = str => { */ utils.parseUrlQueryParameters = str => { if (typeof str !== 'string') return; - const channelName = str; - const url = new URL(`http://${channelName}`); - return url.search; + return str.match(/\?((.*=.*)(&?))/g); }; /** diff --git a/test/customValidators_test.js b/test/customValidators_test.js index 42ea238a9..fee213807 100644 --- a/test/customValidators_test.js +++ b/test/customValidators_test.js @@ -343,6 +343,18 @@ describe('validateChannel()', function () { expect(validateChannels(parsedInput, inputString, input)).to.equal(true); }); + it('should successfully validate channel name is just a single slash (/)', async function () { + const inputString = `{ + "channels": { + "/": { + } + } + }`; + const parsedInput = JSON.parse(inputString); + + expect(validateChannels(parsedInput, inputString, input)).to.equal(true); + }); + it('should throw error that the provided channel name is invalid', async function () { const inputString = `{ "channels": { @@ -375,6 +387,70 @@ describe('validateChannel()', function () { } }); + it('should throw error that the provided channel name is invalid when channel name is just a single slash (/)', async function () { + const inputString = `{ + "channels": { + "/?foo=1": { + } + } + }`; + const parsedInput = JSON.parse(inputString); + + try { + validateChannels(parsedInput, inputString, input); + } catch (e) { + expect(e.type).to.equal('https://github.com/asyncapi/parser-js/validation-errors'); + expect(e.title).to.equal('Channel validation failed'); + expect(e.parsedJSON).to.deep.equal(parsedInput); + expect(e.validationErrors).to.deep.equal([ + { + title: '/?foo=1 channel contains invalid name with url query parameters: ?foo=1', + location: { + endColumn: 11, + endLine: 4, + endOffset: 52, + jsonPointer: '/channels/~1?foo=1', + startColumn: 21, + startLine: 3, + startOffset: 41 + } + } + ]); + } + }); + + it('should throw error that channel has invalid name with two query params', async function () { + const inputString = `{ + "channels": { + "/user/signedup?foo=1&bar=0": { + } + } + }`; + const parsedInput = JSON.parse(inputString); + + try { + validateChannels(parsedInput, inputString, input); + } catch (e) { + expect(e.type).to.equal('https://github.com/asyncapi/parser-js/validation-errors'); + expect(e.title).to.equal('Channel validation failed'); + expect(e.parsedJSON).to.deep.equal(parsedInput); + expect(e.validationErrors).to.deep.equal([ + { + title: '/user/signedup?foo=1&bar=0 channel contains invalid name with url query parameters: ?foo=1&bar=0', + location: { + endColumn: 9, + endLine: 4, + endOffset: 65, + jsonPointer: '/channels/~1user~1signedup?foo=1&bar=0', + startColumn: 38, + startLine: 3, + startOffset: 56 + } + } + ]); + } + }); + it('should throw error that one of the provided channel name is invalid', async function () { const inputString = `{ "channels": {