From c43b614cab9abca8a3a6449f7d025d5476749430 Mon Sep 17 00:00:00 2001 From: Narasimha1997 Date: Sun, 29 Aug 2021 19:46:27 +0530 Subject: [PATCH] http2: set origin name correctly when servername is empty Fixes: https://github.com/nodejs/node/issues/39919 --- lib/internal/http2/core.js | 2 +- ...t-http2-https-origin-string-correctness.js | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/internet/test-http2-https-origin-string-correctness.js diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 473c1244f75da7..c6d0b9a420f4d6 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -3098,7 +3098,7 @@ function initializeTLSOptions(options, servername) { options.ALPNProtocols = ['h2']; if (options.allowHTTP1 === true) ArrayPrototypePush(options.ALPNProtocols, 'http/1.1'); - if (servername !== undefined && options.servername === undefined) + if (servername !== undefined && !options.servername) options.servername = servername; return options; } diff --git a/test/internet/test-http2-https-origin-string-correctness.js b/test/internet/test-http2-https-origin-string-correctness.js new file mode 100644 index 00000000000000..89310353479b0a --- /dev/null +++ b/test/internet/test-http2-https-origin-string-correctness.js @@ -0,0 +1,42 @@ +'use strict'; + +// Ref: https://github.com/nodejs/node/issues/39919 + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const http2 = require('http2'); + +function _verifyOriginSet(session, originString) { + session.once('remoteSettings', () => { + assert.strictEqual(typeof session.originSet, 'object'); + assert.strictEqual(session.originSet.length, 1); + assert.strictEqual(session.originSet[0], originString); + session.close(); + }); + session.once('error', (error) => { + assert.strictEqual(error.code, 'ECONNREFUSED'); + session.close(); + }); +} + +function withServerName() { + const session = http2.connect('https://1.1.1.1', { servername: 'cloudflare-dns.com' }); + _verifyOriginSet(session, 'https://cloudflare-dns.com'); +} + +function withEmptyServerName() { + const session = http2.connect('https://1.1.1.1', { servername: '' }); + _verifyOriginSet(session, 'https://1.1.1.1'); +} + +function withoutServerName() { + const session = http2.connect('https://1.1.1.1'); + _verifyOriginSet(session, 'https://1.1.1.1'); +} + +withServerName(); +withEmptyServerName(); +withoutServerName();