Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls: use the most recently added matching SecureContext in default SNICallback. #34638

Closed
wants to merge 365 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
365 commits
Select commit Hold shift + click to select a range
c143266
test: add a few uncovered url tests from wpt
watilde Oct 14, 2020
8a93b37
build: do not install jq in workflows
targos Oct 14, 2020
cfbbeea
build: use GITHUB_ENV file to set env variables
targos Oct 14, 2020
6f34498
net: add support for resolving DNS CAA records
lxdicted Oct 2, 2020
2c2c87e
doc: fix stability indicator in webcrypto doc
Trott Oct 16, 2020
7f25fe8
fs: remove unused assignment
Trott Oct 14, 2020
9e5a27a
doc: fix EventEmitter examples
shaw91 May 22, 2020
eeb6b47
src: error reporting on CPUUsage
yashLadha Aug 13, 2020
30fb4a0
test: add common.mustSucceed
tniessen Sep 6, 2020
aaf225a
module: add setter for module.parent
aduh95 Oct 6, 2020
a373130
test: refactor test-https-host-headers
himself65 Apr 13, 2020
35a6946
tools: update gyp-next to v0.6.2
targos Oct 17, 2020
bb2945e
build: add GitHub Action for code coverage
bcoe Oct 15, 2020
9ce5a03
test: add test for listen callback runtime binding
adinhtdsibm Oct 15, 2020
ee85eb9
doc: importModuleDynamically gets Script, not Module
SimenB Oct 11, 2020
89defff
doc: correct order of metadata for deprecation
Trott Oct 16, 2020
fbe33aa
tools: bump remark-lint-preset-node to 1.17.1
Trott Oct 16, 2020
5b9593f
deps: upgrade npm to 7.0.2
MylesBorins Oct 16, 2020
089d654
test: fix addons/dlopen-ping-pong for npm 7.0.1
MylesBorins Oct 16, 2020
a1d639b
deps: update V8 to 8.6.395
targos Oct 15, 2020
d701247
build: reset embedder string to "-node.0"
targos Oct 15, 2020
b1e8e0e
src: update NODE_MODULE_VERSION to 88
targos Sep 29, 2020
a8cd91c
deps: V8: un-cherry-pick bd019bd
refack Mar 27, 2019
53e55b6
deps: patch V8 to run on older XCode versions
ryzokuken Sep 14, 2019
046c6fb
deps: V8: patch register-arm64.h
refack May 22, 2019
ebbb038
deps: V8: forward declaration of `Rtl*FunctionTable`
refack May 22, 2019
338ba94
deps: make v8.h compatible with VS2015
joaocgreis Nov 1, 2019
3937dff
deps: V8: silence irrelevant warnings
targos Mar 27, 2019
a86521a
deps: patch V8 to run on Xcode 8
mmarchini Mar 10, 2020
410f501
deps: update V8 postmortem metadata script
cjihrig Jul 4, 2020
47cb9f1
deps: update V8 postmortem metadata script
cjihrig Sep 15, 2020
b803b3f
deps: fix platform-embedded-file-writer-win for ARM64
targos Oct 14, 2020
3c23af4
deps: fix V8 build issue with inline methods
gengjiawen Oct 14, 2020
b006fa8
deps: V8: cherry-pick 6be2f6e26e8d
targos Sep 29, 2020
efee834
deps: V8: cherry-pick 717543bbf0ef
targos Sep 29, 2020
9378070
deps: V8: cherry-pick d76abfed3512
targos Oct 14, 2020
70cdf10
deps: V8: cherry-pick 3176bfd447a9
addaleax Oct 12, 2020
faeb960
tools: update V8 gypfiles for 8.5
ryzokuken May 12, 2020
94dd7b9
test: update tests after increasing typed array size to 4GB
ktran Jun 18, 2020
ca8f3ef
wasi: drop --experimental-wasm-bigint requirement
cjihrig Jul 22, 2020
80e8aec
tools: update V8 gypfiles for 8.6
ryzokuken Jul 4, 2020
b65e5ae
src: implement NodePlatform::PostJob
backes Jul 21, 2020
6cfba9f
process: update v8 fast api calls usage
MayaLekova Jul 23, 2020
c55f661
tools: disable x86 safe exception handlers in V8
targos Oct 14, 2020
7afe3af
url: fix file url reparse
watilde Oct 16, 2020
ce03a18
doc: clarify experimental API elements in vm.md
Trott Oct 11, 2020
7798e59
doc: upgrade stability status of report API
gireeshpunathil Oct 15, 2020
1103b15
doc: fix YAML lint error on master
Trott Oct 19, 2020
84a7880
src: minor cleanup and simplification of crypto::Hash
jasnell Oct 14, 2020
b18651b
build: do not pass mode option to test-v8 command
targos Oct 19, 2020
2b3acc4
src: large pages support in illumos/solaris systems
devnexen Jul 12, 2020
1428db8
lib: refactor Socket._getpeername and Socket._getsockname
himself65 Apr 21, 2020
5b2c263
deps: fix typo in zlib.gyp that break arm-fpu-neon build
lucasg Oct 15, 2020
11f1ad9
module: only try to enrich CJS syntax errors
targos Oct 17, 2020
50103bf
doc: add a statement in the documentation.
PoojaDurgad Oct 10, 2020
3d9310d
doc: use kbd element in os doc
Trott Oct 15, 2020
9e227d5
test: refactor test-crypto-pbkdf2
tniessen Oct 17, 2020
4f296d2
path: add `path/posix` and `path/win32` alias modules
ExE-Boss Aug 28, 2020
57613f8
2020-10-20, Version 15.0.0 (Current)
BethGriggs Sep 1, 2020
6b6bbfe
doc: add release key for Danielle Adams
danielleadams Oct 7, 2020
c2ceb15
util: add `util/types` alias module
ExE-Boss Jun 27, 2020
4cbcfae
crypto: fix regression on randomFillSync
jasnell Oct 20, 2020
6c4239b
doc: use kbd element in readline doc
Trott Oct 18, 2020
a987a32
build: use make functions instead of echo
aduh95 Oct 19, 2020
610c68c
src: mark/pop OpenSSL errors in NewRootCertStore
danbev Oct 6, 2020
66ab2bd
deps: V8: set correct V8 version patch number
targos Oct 21, 2020
d5088d8
deps: upgrade npm to 7.0.3
ruyadorno Oct 20, 2020
d2a3078
src: add --heapsnapshot-near-heap-limit option
joyeecheung Oct 21, 2020
4d16554
2020-10-21, Version 15.0.1 (Current)
BethGriggs Oct 21, 2020
db4ce85
deps: V8: backport 4263f8a5e8e0
bdougie Oct 14, 2020
7657f62
test: add windows and C++ coverage
bcoe Oct 17, 2020
fc5636e
v8: implement v8.takeCoverage()
joyeecheung Jun 9, 2020
7945a2c
v8: implement v8.stopCoverage()
joyeecheung Jun 9, 2020
8f47e69
build: add license-builder GitHub Action
bnb Oct 19, 2020
1ba932b
doc: add Installing Node.js header in BUILDING.md
PoojaDurgad Oct 19, 2020
3f4ec9c
doc,esm: document experimental warning removal
aduh95 Oct 22, 2020
4fb7803
doc,src,test: revise C++ code for linter update
Trott Oct 20, 2020
d9ed73c
tools: bump cpplint to 1.5.0
Trott Oct 20, 2020
d233f10
tools: refloat 7 Node.js patches to cpplint.py
Trott Oct 9, 2020
e6b1d74
doc: update package.json field definitions
MylesBorins Oct 21, 2020
fdaf9cb
deps: update to cjs-module-lexer@0.4.3
guybedford Oct 21, 2020
4d54449
stream: fix duplicate logic in stream destroy
yashLadha Oct 21, 2020
ab62fc1
doc: this prints replaced with this is printed
PoojaDurgad Oct 6, 2020
6bf2139
doc: improve text for breakOnSigint
Trott Oct 17, 2020
4ab6628
doc: add missing link in Node.js 14 Changelog
aduh95 Oct 23, 2020
440edaa
doc: update console.error example
iyabong Aug 29, 2020
c5b9b5b
test: add additional deprecation warning tests for rmdir recursive
iansu Oct 16, 2020
7083425
http2: remove unsupported %.* specifier
mmomtchev Oct 17, 2020
3e10ce3
dns: add setLocalAddress to Resolver
daguej Aug 18, 2020
1d5b91d
doc: document changes for `*/promises` alias modules
ExE-Boss Jun 21, 2020
629e1ab
http2: add updateSettings to both http2 servers
Sep 28, 2020
75202d9
http2: reinject data received before http2 is attached
mmomtchev Oct 16, 2020
1f703e1
http2: fix reinjection check
mmomtchev Oct 18, 2020
fc3f41b
doc: add a example code to API doc property
PoojaDurgad Oct 21, 2020
20a6ddc
timers: correct explanation in comment
wjabbour Oct 1, 2020
364ac78
stream: remove redundant context from comments
yashLadha Oct 21, 2020
9437e2f
doc: mark optional parameters in timers.md
vsemozhetbyt Oct 22, 2020
e54c8b7
doc: remove stability warning in v8 module doc
Trott Oct 23, 2020
bae7dde
doc: fix header level in fs.md
ax1 Oct 23, 2020
ba907ff
test: refactor coverage logic
bcoe Oct 23, 2020
e2d2206
doc: fix a typo in CHANGELOG_V12
iShibi Oct 24, 2020
dd43715
doc: use kbd element in readline doc prose
Trott Oct 21, 2020
4ace92f
build: turn off Codecov comments
bcoe Oct 25, 2020
a8b8bc8
doc: modified memory set statement set size
PoojaDurgad Oct 6, 2020
48d2aca
doc: add require statement in the example
PoojaDurgad Oct 8, 2020
f44029e
Revert "src: ignore GCC -Wcast-function-type for v8.h"
danbev Oct 22, 2020
ba465c5
src: use MaybeLocal.ToLocal instead of IsEmpty
danbev Oct 20, 2020
9ff25b1
src: remove ignore GCC -Wcast-function-type for v8
danbev Oct 23, 2020
ed5e539
doc: fix typo in BUILDING.md
RaisinTen Oct 26, 2020
16b2bc0
deps: update acorn to v8.0.4
targos Oct 24, 2020
8f1e4d3
doc: add a subsystems header in pull-requests.md
PoojaDurgad Oct 20, 2020
7c3ce9d
doc: fix _construct example params order
a0viedo Oct 24, 2020
0d47432
http: lazy create IncomingMessage.headers
ronag Sep 21, 2020
04d1664
worker: add eventLoopUtilization()
trevnorris Oct 14, 2020
0b45382
n-api: revert change to finalization
mhdawson Oct 23, 2020
34810e0
doc: consolidate and clarify breakOnSigInt text
Trott Oct 24, 2020
245ec6f
doc,test: update v8 method doc and comment
Trott Oct 25, 2020
f9c9c21
test: add upstream test cases to urlsearchparam
watilde Oct 25, 2020
62ead4e
2020-10-27, Version 10.23.0 'Dubnium' (LTS)
richardlau Oct 7, 2020
0ca8617
2020-10-27, Version 14.15.0 'Fermium' (LTS)
richardlau Oct 26, 2020
cb62f16
doc: use case-sensitive in the example
PoojaDurgad Oct 13, 2020
923f76d
src: remove ERR prefix in WebCryptoKeyExportStatus
danbev Oct 14, 2020
3df5afb
child_process: add ChildProcess 'spawn' event
zenflow Sep 27, 2020
ab660a5
doc: update MessagePort documentation for EventTarget inheritance
addaleax Oct 27, 2020
761c1b0
tls: allow reading data into a static buffer
puzpuzpuz Oct 22, 2020
937b70d
doc: make small improvements to REPL doc
Trott Oct 26, 2020
81ba3ae
events: make eventTarget.removeAllListeners() return this
lpinca Oct 26, 2020
f20a783
events: make abort_controller event trusted
benjamingr Oct 26, 2020
bec918f
test: add upstream test cases to encoding
watilde Oct 25, 2020
a3cc239
doc: add conditional example for setBreakpoint()
copperwall Oct 26, 2020
4bd1fba
tools: add update-npm script
MylesBorins Oct 26, 2020
acdfc16
doc: stabilize packages features
MylesBorins Oct 21, 2020
802c98d
test: add onerror test cases to policy
watilde Oct 25, 2020
b92d2e4
deps: upgrade to cjs-module-lexer@0.5.0
guybedford Oct 29, 2020
48ed72d
tools: add msvc /P output to .gitignore
gengjiawen Oct 21, 2020
5947862
doc: fix reference to googletest test fixture
tniessen Oct 26, 2020
e8fe38f
crypto: fix webcrypto ECDH JWK import
panva Oct 28, 2020
0c61436
build,tools: add lint-js-doc target
aduh95 Oct 19, 2020
b90e135
build: add vcbuilt test-doc target
aduh95 Oct 19, 2020
30e9fab
doc: recommend test-doc instead of lint-md
aduh95 Oct 19, 2020
05bb1b3
crypto: fixup scrypt regressions
jasnell Oct 26, 2020
1da6729
lib: use primordials when calling methods of Error
aduh95 Oct 27, 2020
876d816
build,tools: gitHub Actions: use Node.js Fermium
aduh95 Oct 27, 2020
dc9f108
doc: move ronkorving to emeritus
Trott Oct 27, 2020
02e60a6
doc: fix a typo in CHANGELOG_V15
tnir Oct 26, 2020
87a6b60
test: fix reference to WPT testharness.js
tniessen Oct 26, 2020
0ddd69e
assert,repl: enable ecmaVersion 2021 in acorn parser
targos Oct 27, 2020
c9acb9e
module: runtime deprecate subpath folder mappings
guybedford Oct 22, 2020
5da2512
test: mark test-http2-respond-file-error-pipe-offset flaky
MylesBorins Oct 30, 2020
3a7537d
n-api: napi_make_callback emit async init with resource of async_context
legendecas Oct 6, 2020
c0af8bd
zlib: test BrotliCompress throws invalid arg value
RaisinTen Oct 27, 2020
915a94c
test: mark test-worker-eventlooputil flaky
MylesBorins Oct 30, 2020
1139a4a
doc: add on statement in the APIs docs
PoojaDurgad Oct 12, 2020
65c9d67
crypto: rename check to createJob
danbev Oct 29, 2020
5dd344a
test: add test to fs/promises setImmediate
tyankatsu0105 Oct 28, 2020
c6c36c3
lib: create diagnostics_channel module
Qard Aug 21, 2020
7231b51
http: report request start and end with diagnostics_channel
Qard Sep 8, 2020
868d81e
tools: update ESLint to 7.12.0
cjihrig Oct 25, 2020
7ebae13
tools: update ESLint to 7.12.1
cjihrig Oct 27, 2020
2351e2c
test: integrate abort_controller tests from wpt
watilde Oct 30, 2020
5735525
deps: update to cjs-module-lexer@0.5.2
guybedford Oct 31, 2020
44571d1
lib: let abort_controller target be EventTarget
watilde Oct 30, 2020
c0f6ff2
doc: move bnoordhuis to emeritus
bnoordhuis Oct 3, 2020
09af8c8
doc: update crypto.createSecretKey history
ben-turner Oct 29, 2020
dfc288e
src: clean up embedder API
addaleax Oct 31, 2020
c61306d
tools: bump cpplint to 1.5.1
Trott Oct 29, 2020
b600b9a
tools: refloat 7 Node.js patches to cpplint.py
Trott Oct 9, 2020
a5f99b8
deps: upgrade to cjs-module-lexer@1.0.0
guybedford Nov 2, 2020
dc05c70
deps: upgrade npm to 7.0.7
lpinca Nov 1, 2020
bf6883d
build: fix lint-js-fix target
aduh95 Nov 2, 2020
26fcdb6
errors: do not call resolve on URLs with schemes
bcoe Oct 31, 2020
dbdd234
doc: adjust table alignment for remark v13
nschonni Oct 14, 2020
07bb0c4
doc: disable no-undefined-references workarounds
nschonni Oct 14, 2020
1a29c09
doc: temporarily disable list-item-bullet-indent
nschonni Oct 14, 2020
adfcade
doc: update tables in README files for linting changes
Trott Nov 3, 2020
1bc1f84
tools: bump remark-lint-preset-node to 2.0.0
Trott Nov 1, 2020
ee749ba
events: add a few tests
benjamingr Oct 26, 2020
fc8e3e2
src: remove redundant OpenSSLBuffer
jasnell Oct 15, 2020
38c1569
benchmark: add startup benchmark for loading public modules
joyeecheung Oct 26, 2020
dbcadf0
benchmark: make the benchmark tool work with Node 10
joyeecheung Oct 26, 2020
58280ff
deps: upgrade npm to 7.0.8
MylesBorins Nov 4, 2020
9dbde1d
test: correct test-worker-eventlooputil
Flarna Oct 31, 2020
72ce5dc
test: add missing ref comments to parallel.status
Trott Oct 31, 2020
5a3f43b
stream: fix regression on duplex end
mmomtchev Nov 3, 2020
9aafda5
crypto: pass empty passphrases to OpenSSL properly
tniessen Nov 1, 2020
8caa6a9
2020-11-04, Version 15.1.0 (Current)
targos Nov 3, 2020
d4d6dfd
doc: fix incorrect heading level
700software Nov 4, 2020
3fab511
doc: fix release-schedule link in backport guide
watilde Nov 2, 2020
1387f4b
src: remove ERR prefix in crypto status enums
danbev Oct 29, 2020
c822ba7
n-api: unlink reference during its destructor
Nov 2, 2020
540b796
benchmark: remove modules that require intl
richardlau Nov 4, 2020
eb24573
doc: option --prof documentation help added
Krank2me Aug 30, 2020
2b985a2
doc: add new wordings to the API description
PoojaDurgad Oct 10, 2020
088dfce
test: fix error in test/internet/test-dns.js
Trott Nov 4, 2020
c575aec
doc: update benchmark CI test indicator in README
Trott Nov 3, 2020
c9e3848
crypto: fix comment in ByteSource
tniessen Nov 4, 2020
1f6ca18
console: use more primordials
aduh95 Oct 21, 2020
c723e4c
lib: refactor to use more primordials
aduh95 Oct 29, 2020
e6e6070
errors: refactor to use more primordials
aduh95 Nov 3, 2020
b5a136c
fs: add support for AbortSignal in readFile
benjamingr Nov 1, 2020
2a1273c
events: port some wpt tests
benjamingr May 28, 2020
c5477be
events: support emit on nodeeventtarget
benjamingr Oct 28, 2020
456b4e1
doc: update core-validate-commit link in guide
watilde Nov 3, 2020
ec400f8
doc: revise v8.getHeapSnapshot()
Trott Oct 28, 2020
be17886
test: update old comment style test_util.cc
RaisinTen Oct 30, 2020
1fd7d8e
http2: fix error stream write followed by destroy
davedoesdev Nov 4, 2020
8fa9035
module: refactor to use iterable-weak-map
bcoe Nov 3, 2020
0ad4f70
domain: refactor to use more primordials
aduh95 Oct 30, 2020
3c9f072
tools,doc: enable ecmaVersion 2021 in acorn parser
aduh95 Nov 6, 2020
85b42af
benchmark: ignore build artifacts for napi addons
richardlau Nov 4, 2020
1d525f5
fs: remove unused assignment
Trott Oct 30, 2020
cef1444
tools: add new ESLint rule: prefer-primordials
Leko Sep 29, 2020
115722b
tools: don't print gold linker warning w/o flag
MylesBorins Nov 4, 2020
bfc9847
win, build: fix build time on Windows
bzoz Nov 2, 2020
8e87efb
events: define event handler as enumerable
benjamingr Nov 2, 2020
b80d847
events: support event handlers on prototypes
benjamingr Nov 2, 2020
1c99cde
events: define abort on prototype
benjamingr Nov 2, 2020
d396900
events: fire handlers in correct oder
benjamingr Nov 6, 2020
f06f3c0
fs: remove unnecessary Function#bind() in fs/promises
bnoordhuis Sep 15, 2020
1f69aa0
doc,crypto: added sign/verify method changes about dsaEncoding
panva Oct 12, 2020
133fdd4
doc: fix crypto doc linter errors
aduh95 Nov 8, 2020
6abb7e5
http2: move events to the JSStreamSocket
mmomtchev Oct 23, 2020
adae822
http2: centralise socket event binding in Http2Session
mmomtchev Oct 24, 2020
ad98cf0
stream: remove isPromise utility function
aduh95 Nov 2, 2020
1a6d4dc
events: getEventListeners static
benjamingr Nov 6, 2020
aa1eb1f
timers: cleanup abort listener on awaitable timers
jasnell Nov 6, 2020
c925e24
lib: add %TypedArray% abstract constructor to primordials
ExE-Boss Nov 7, 2020
1bba8c8
deps: cherry-pick 9a49b22 from V8 upstream
danbev Nov 3, 2020
642174a
dns: fix trace_events name for resolveCaa()
Trott Nov 5, 2020
8f95438
tools,lib: tighten prefer-primordials rules for Error statics
aduh95 Nov 7, 2020
6c3326c
doc,fs: document value of stats.isDirectory on symbolic links
coderaiser Apr 25, 2019
888900b
doc: advise users to import the full set of trusted release keys
haqer1 Apr 4, 2020
5bbc4f4
doc,net: document socket.timeout
kobelb Jul 28, 2020
da3626a
doc: add note regarding file structure in src/README.md
lundibundi Aug 31, 2020
0d2e198
http2: add has method to proxySocketHandler
masx200 Sep 14, 2020
caa5a05
doc,stream: write(chunk, encoding, cb) encoding can be null
dev-script Sep 27, 2020
5b95f01
test: improve error message for policy failures
bmeck Oct 13, 2020
b589128
bootstrap: refactor to use more primordials
aduh95 Nov 6, 2020
36fbbe0
test: use global.EventTarget instead of internals
aduh95 Nov 6, 2020
a8b9ede
fs: replace finally with PromisePrototypeFinally
baruchiro Nov 6, 2020
2c77fe1
module: refactor to use more primordials
aduh95 Nov 7, 2020
831f4c7
tools,lib: recommend using safe primordials
aduh95 Nov 7, 2020
ab587ca
build: fix zlib inlining for IA-32
RaisinTen Oct 16, 2020
da53a3c
crypto: refactor to use more primordials
aduh95 Nov 6, 2020
dd0f8f1
stream: writableNeedDrain
ronag Sep 25, 2020
322abb4
build, tools: look for local installation of NASM
richardlau Nov 7, 2020
6ce5bd8
tls: use the most recently added matching SecureContext in default SN…
mkrawczuk Aug 5, 2020
ec8d2bd
fixup! do not reverse in place, add a test for it.
mkrawczuk Aug 6, 2020
60ab351
Merge branch '34110' of github.com:mkrawczuk/node into 34110
mkrawczuk Nov 10, 2020
acccc68
Revert "Merge branch '34110' of github.com:mkrawczuk/node into 34110"
mkrawczuk Nov 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/api/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ added: v0.5.3
The `server.addContext()` method adds a secure context that will be used if
the client request's SNI name matches the supplied `hostname` (or wildcard).

When there are multiple matching contexts, the most recently added one will be
used.

### `server.address()`
<!-- YAML
added: v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -1435,8 +1435,8 @@ Server.prototype[EE.captureRejectionSymbol] = function(

function SNICallback(servername, callback) {
const contexts = this.server._contexts;

for (const elem of contexts) {
for (let i = contexts.length - 1; i >= 0; --i) {
const elem = contexts[i];
if (elem[0].test(servername)) {
callback(null, elem[1]);
return;
Expand Down
99 changes: 99 additions & 0 deletions test/parallel/test-tls-secure-context-usage-order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');

if (!common.hasCrypto)
common.skip('missing crypto');

// This test ensures that when a TLS connection is estabilished, the server
// selects the most recently added SecureContext that matches the servername.

const assert = require('assert');
const tls = require('tls');

function loadPEM(n) {
return fixtures.readKey(`${n}.pem`);
}

const serverOptions = {
key: loadPEM('agent2-key'),
cert: loadPEM('agent2-cert'),
requestCert: true,
rejectUnauthorized: false,
};

const badSecureContext = {
key: loadPEM('agent1-key'),
cert: loadPEM('agent1-cert'),
ca: [ loadPEM('ca2-cert') ]
};

const goodSecureContext = {
key: loadPEM('agent1-key'),
cert: loadPEM('agent1-cert'),
ca: [ loadPEM('ca1-cert') ]
};

const server = tls.createServer(serverOptions, (c) => {
// The 'a' and 'b' subdomains are used to distinguish between client
// connections.
// Connection to subdomain 'a' is made when the 'bad' secure context is
// the only one in use.
if ('a.example.com' === c.servername) {
assert.strictEqual(c.authorized, false);
}
// Connection to subdomain 'b' is made after the 'good' context has been
// added.
if ('b.example.com' === c.servername) {
assert.strictEqual(c.authorized, true);
}
});

// 1. Add the 'bad' secure context. A connection using this context will not be
// authorized.
server.addContext('*.example.com', badSecureContext);

server.listen(0, () => {
const options = {
port: server.address().port,
key: loadPEM('agent1-key'),
cert: loadPEM('agent1-cert'),
ca: [loadPEM('ca1-cert')],
servername: 'a.example.com',
rejectUnauthorized: false,
};

// 2. Make a connection using servername 'a.example.com'. Since a 'bad'
// secure context is used, this connection should not be authorized.
const client = tls.connect(options, () => {
client.end();
});

client.on('close', common.mustCall(() => {
// 3. Add a 'good' secure context.
server.addContext('*.example.com', goodSecureContext);

options.servername = 'b.example.com';
// 4. Make a connection using servername 'b.example.com'. This connection
// should be authorized because the 'good' secure context is the most
// recently added matching context.

const other = tls.connect(options, () => {
other.end();
});

other.on('close', common.mustCall(() => {
// 5. Make another connection using servername 'b.example.com' to ensure
// that the array of secure contexts is not reversed in place with each
// SNICallback call, as someone might be tempted to refactor this piece of
// code by using Array.prototype.reverse() method.
const onemore = tls.connect(options, () => {
onemore.end();
});

onemore.on('close', common.mustCall(() => {
server.close();
}));
}));
}));
});