@@ -352,70 +352,69 @@ public List<Object> getMany(final int count) {
352
352
return responses ;
353
353
}
354
354
355
+ /**
356
+ * Check if the client name libname, libver, characters are legal
357
+ * @param info the name
358
+ * @return Returns true if legal, false throws exception
359
+ * @throws JedisException if characters illegal
360
+ */
361
+ private static boolean validateClientInfo (String info ) {
362
+ for (int i = 0 ; i < info .length (); i ++) {
363
+ char c = info .charAt (i );
364
+ if (c < '!' || c > '~' ) {
365
+ throw new JedisException ("client info cannot contain spaces, "
366
+ + "newlines or special characters." );
367
+ }
368
+ }
369
+ return true ;
370
+ }
371
+
355
372
private void initializeFromClientConfig (JedisClientConfig config ) {
356
373
try {
357
374
connect ();
358
375
359
376
Supplier <RedisCredentials > credentialsProvider = config .getCredentialsProvider ();
360
377
if (credentialsProvider instanceof RedisCredentialsProvider ) {
361
- ((RedisCredentialsProvider ) credentialsProvider ).prepare ();
362
- auth (credentialsProvider );
363
- ((RedisCredentialsProvider ) credentialsProvider ).cleanUp ();
378
+ try {
379
+ ((RedisCredentialsProvider ) credentialsProvider ).prepare ();
380
+ auth (credentialsProvider );
381
+ } finally {
382
+ ((RedisCredentialsProvider ) credentialsProvider ).cleanUp ();
383
+ }
364
384
} else {
365
385
auth (credentialsProvider );
366
386
}
367
387
368
- List <CommandArguments > fireAndForgetMsg = new ArrayList <>();
369
-
370
388
int dbIndex = config .getDatabase ();
371
389
if (dbIndex > 0 ) {
372
- fireAndForgetMsg . add ( new CommandArguments ( Command . SELECT ). add ( Protocol . toByteArray ( dbIndex )) );
390
+ select ( dbIndex );
373
391
}
374
392
393
+ List <CommandArguments > fireAndForgetMsg = new ArrayList <>();
394
+
375
395
String clientName = config .getClientName ();
376
- if (clientName != null ) {
396
+ if (clientName != null && validateClientInfo ( clientName ) ) {
377
397
fireAndForgetMsg .add (new CommandArguments (Command .CLIENT ).add (Keyword .SETNAME ).add (clientName ));
378
398
}
379
399
380
400
String libName = JedisMetaInfo .getArtifactId ();
381
- if (libName != null ) {
401
+ if (libName != null && validateClientInfo ( libName ) ) {
382
402
fireAndForgetMsg .add (new CommandArguments (Command .CLIENT ).add (Keyword .SETINFO )
383
403
.add (ClientAttributeOption .LIB_NAME .getRaw ()).add (libName ));
384
404
}
385
405
386
406
String libVersion = JedisMetaInfo .getVersion ();
387
- if (libVersion != null ) {
407
+ if (libVersion != null && validateClientInfo ( libVersion ) ) {
388
408
fireAndForgetMsg .add (new CommandArguments (Command .CLIENT ).add (Keyword .SETINFO )
389
409
.add (ClientAttributeOption .LIB_VER .getRaw ()).add (libVersion ));
390
410
}
391
411
392
412
for (CommandArguments arg : fireAndForgetMsg ) {
393
413
sendCommand (arg );
394
414
}
395
-
396
- List <Object > objects = getMany (fireAndForgetMsg .size ());
397
- for (Object obj : objects ) {
398
- if (obj instanceof JedisDataException ) {
399
- JedisDataException e = (JedisDataException )obj ;
400
- String errorMsg = e .getMessage ().toUpperCase ();
401
- /**
402
- * 1. Redis 4.0 and before, we need to ignore `Syntax error`.
403
- * 2. Redis 5.0 and later, we need to ignore `Unknown subcommand error`.
404
- * 3. Because Jedis allows Jedis jedis = new Jedis() in advance, and jedis.auth(password) later,
405
- * we need to ignore `NOAUTH errors`.
406
- */
407
- if (errorMsg .contains ("SYNTAX" ) ||
408
- errorMsg .contains ("UNKNOWN" ) ||
409
- errorMsg .contains ("NOAUTH" )) { // TODO: not filter out NOAUTH
410
- // ignore
411
- } else {
412
- throw e ;
413
- }
414
- }
415
- }
415
+ getMany (fireAndForgetMsg .size ());
416
416
} catch (JedisException je ) {
417
417
try {
418
- setBroken ();
419
418
disconnect ();
420
419
} catch (Exception e ) {
421
420
// the first exception 'je' will be thrown
@@ -447,7 +446,6 @@ private void auth(final Supplier<RedisCredentials> credentialsProvider) {
447
446
getStatusCodeReply (); // OK
448
447
}
449
448
450
- @ Deprecated
451
449
public String select (final int index ) {
452
450
sendCommand (Protocol .Command .SELECT , Protocol .toByteArray (index ));
453
451
return getStatusCodeReply ();
0 commit comments