Skip to content

Commit 21a4a8a

Browse files
committed
SDK now properly adding a peer with an invalid URL
Before this change, the SDK aborted when adding a peer with an invalid URL. This change correctly handles the case when the URL does not begin with 'grpc' or 'grpcs' and returns an 'InvalidProtocol' error message Fixes FAB-256 Change-Id: I7791acc49b4c9b05511155d399d882b884128c4c Signed-off-by: Gari Singh <[email protected]>
1 parent 84e7cc1 commit 21a4a8a

File tree

3 files changed

+98
-8
lines changed

3 files changed

+98
-8
lines changed

sdk/node/src/hfc.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -2205,15 +2205,24 @@ class Endpoint {
22052205

22062206
constructor(url:string, pem?:string) {
22072207
let purl = parseUrl(url);
2208-
let protocol = purl.protocol.toLowerCase();
2208+
var protocol;
2209+
if (purl.protocol) {
2210+
protocol = purl.protocol.toLowerCase().slice(0,-1);
2211+
}
22092212
if (protocol === 'grpc') {
22102213
this.addr = purl.host;
22112214
this.creds = grpc.credentials.createInsecure();
2212-
} else if (protocol === 'grpcs') {
2215+
}
2216+
else if (protocol === 'grpcs') {
22132217
this.addr = purl.host;
22142218
this.creds = grpc.credentials.createSsl(new Buffer(pem));
2215-
} else {
2216-
throw Error("invalid protocol: " + protocol);
2219+
}
2220+
else {
2221+
var error = new Error();
2222+
error.name = "InvalidProtocol";
2223+
error.message = "Invalid protocol: " + protocol +
2224+
". URLs must begin with grpc:// or grpcs://"
2225+
throw error;
22172226
}
22182227
}
22192228
}
@@ -2628,10 +2637,6 @@ function isFunction(fcn:any):boolean {
26282637
function parseUrl(url:string):any {
26292638
// TODO: find ambient definition for url
26302639
var purl = urlParser.parse(url, true);
2631-
var protocol = purl.protocol;
2632-
if (endsWith(protocol, ":")) {
2633-
purl.protocol = protocol.slice(0, -1);
2634-
}
26352640
return purl;
26362641
}
26372642

sdk/node/test/fixtures/tlsca.cert

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBwTCCAUegAwIBAgIBATAKBggqhkjOPQQDAzApMQswCQYDVQQGEwJVUzEMMAoG
3+
A1UEChMDSUJNMQwwCgYDVQQDEwNPQkMwHhcNMTYwMTIxMjI0OTUxWhcNMTYwNDIw
4+
MjI0OTUxWjApMQswCQYDVQQGEwJVUzEMMAoGA1UEChMDSUJNMQwwCgYDVQQDEwNP
5+
QkMwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAR6YAoPOwMzIVi+P83V79I6BeIyJeaM
6+
meqWbmwQsTRlKD6g0L0YvczQO2vp+DbxRN11okGq3O/ctcPzvPXvm7Mcbb3whgXW
7+
RjbsX6wn25tF2/hU6fQsyQLPiJuNj/yxknSjQzBBMA4GA1UdDwEB/wQEAwIChDAP
8+
BgNVHRMBAf8EBTADAQH/MA0GA1UdDgQGBAQBAgMEMA8GA1UdIwQIMAaABAECAwQw
9+
CgYIKoZIzj0EAwMDaAAwZQIxAITGmq+x5N7Q1jrLt3QFRtTKsuNIosnlV4LR54l3
10+
yyDo17Ts0YLyC0pZQFd+GURSOQIwP/XAwoMcbJJtOVeW/UL2EOqmKA2ygmWX5kte
11+
9Lngf550S6gPEWuDQOcY95B+x3eH
12+
-----END CERTIFICATE-----

sdk/node/test/unit/chain-tests.js

+73
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,79 @@ function fail(t, msg, err) {
128128
t.end(err);
129129
}
130130

131+
//
132+
// Test adding an invalid peer (omit protocol or use invalid protocol in URL)
133+
//
134+
test('Add invalid peer URLs to the chain', function (t) {
135+
136+
//list of valid protocol prefixes to test
137+
var prefixes = [
138+
"",
139+
"grpcio://",
140+
"http://",
141+
" "
142+
];
143+
144+
t.plan(prefixes.length);
145+
146+
var chain_test;
147+
148+
//loop through the prefixes
149+
prefixes.forEach(function (prefix, index) {
150+
151+
chain_test = hfc.newChain("chain_test" + index);
152+
153+
try {
154+
chain_test.addPeer(prefix + "://localhost:7051", new Buffer(32));
155+
t.fail("Should not be able to add peer with URL starting with " + prefix);
156+
}
157+
catch (err) {
158+
if (err.name === 'InvalidProtocol') {
159+
t.pass("Returned 'InvalidProtocol' error for URL starting with " + prefix);
160+
}
161+
else {
162+
t.fail("Failed to return 'InvalidProtocol' error for URL starting with " + prefix);
163+
}
164+
}
165+
})
166+
167+
});
168+
169+
170+
//
171+
// Test adding a valid peer (URL must start with grpc or grpcs)
172+
//
173+
test('Add valid peer URLs to the chain', function (t) {
174+
175+
//list of valid protocol prefixes to test
176+
var prefixes = [
177+
"grpc",
178+
"GRPC",
179+
"grpcs",
180+
"GRPCS"
181+
];
182+
183+
t.plan(prefixes.length);
184+
185+
var chain_test2;
186+
187+
//loop through the prefixes
188+
prefixes.forEach(function (prefix, index) {
189+
190+
chain_test2 = hfc.newChain("chain_test2" + index);
191+
192+
try {
193+
chain_test2.addPeer(prefix + "://localhost:7051",
194+
fs.readFileSync(__dirname + "/../fixtures/tlsca.cert"));
195+
t.pass("Successfully added peer with URL starting with " + prefix + "://");
196+
}
197+
catch (err) {
198+
t.fail("Could not add peer to the chain with URL starting with " + prefix + "://");
199+
}
200+
})
201+
202+
});
203+
131204
//
132205
// Set Invalid security level and hash algorithm.
133206
//

0 commit comments

Comments
 (0)