Skip to content

Commit 14708fd

Browse files
committed
CredentialsProvider - use encodePassword(char[]) and decodePassword(char[]) - Fortify
1 parent 13e0a2d commit 14708fd

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

visualvm/jmx/nbproject/project.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<compile-dependency/>
2222
<run-dependency>
2323
<release-version>0</release-version>
24-
<specification-version>1.6</specification-version>
24+
<specification-version>1.7</specification-version>
2525
</run-dependency>
2626
</dependency>
2727
<dependency>

visualvm/jmx/src/org/graalvm/visualvm/jmx/CredentialsProvider.java

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
*/
5252
public abstract class CredentialsProvider extends EnvironmentProvider {
5353

54-
private static final String PROPERTY_USERNAME = "prop_credentials_username"; // NOI18N
55-
private static final String PROPERTY_PASSWORD = "prop_credentials_password"; // NOI18N
54+
private static final String PROPERTY_USER = "prop_credentials_user"; // NOI18N
55+
private static final String PROPERTY_PWORD = "prop_credentials_pword"; // NOI18N
5656

5757
private static Persistent PERSISTENT_PROVIDER;
5858

@@ -90,8 +90,8 @@ public String getId() {
9090
*/
9191
public static class Custom extends CredentialsProvider {
9292

93-
private final String username;
94-
private final char[] password;
93+
private final String user;
94+
private final char[] pword;
9595
private final boolean persistent;
9696

9797

@@ -103,32 +103,32 @@ public static class Custom extends CredentialsProvider {
103103
* @param persistent true if the credentials should be persisted for another VisualVM sessions, false otherwise
104104
*/
105105
public Custom(String username, char[] password, boolean persistent) {
106-
this.username = username;
107-
this.password = encodePassword(password);
106+
this.user = username;
107+
this.pword = encodePassword(password);
108108
this.persistent = persistent;
109109
}
110110

111111

112112
public Map<String, ?> getEnvironment(Application application, Storage storage) {
113-
return createMap(username, password != null ? new String(password) : null);
113+
return createMap(user, pword);
114114
}
115115

116116
public String getEnvironmentId(Storage storage) {
117-
if (username != null) return username;
117+
if (user != null) return user;
118118
return super.getEnvironmentId(storage);
119119
}
120120

121121
public void saveEnvironment(Storage storage) {
122122
if (!persistent) return;
123-
storage.setCustomProperty(PROPERTY_USERNAME, username);
124-
storage.setCustomProperty(PROPERTY_PASSWORD, new String(password));
123+
storage.setCustomProperty(PROPERTY_USER, user);
124+
storage.setCustomProperty(PROPERTY_PWORD, new String(pword));
125125
}
126126

127127

128-
String getUsername(Storage storage) { return username; }
128+
String getUsername(Storage storage) { return user; }
129129

130-
boolean hasPassword(Storage storage) { return password != null &&
131-
password.length > 0; }
130+
boolean hasPassword(Storage storage) { return pword != null &&
131+
pword.length > 0; }
132132

133133
boolean isPersistent(Storage storage) { return persistent; }
134134

@@ -144,26 +144,26 @@ boolean hasPassword(Storage storage) { return password != null &&
144144
public static class Persistent extends CredentialsProvider {
145145

146146
public Map<String, ?> getEnvironment(Application application, Storage storage) {
147-
String username = storage.getCustomProperty(PROPERTY_USERNAME);
148-
String password = storage.getCustomProperty(PROPERTY_PASSWORD);
149-
return createMap(username, password);
147+
String user = storage.getCustomProperty(PROPERTY_USER);
148+
char[] pword = storage.getCustomProperty(PROPERTY_PWORD).toCharArray();
149+
return createMap(user, pword);
150150
}
151151

152152
public String getEnvironmentId(Storage storage) {
153153
if (storage != null) {
154-
String username = storage.getCustomProperty(PROPERTY_USERNAME);
155-
if (username != null) return username;
154+
String user = storage.getCustomProperty(PROPERTY_USER);
155+
if (user != null) return user;
156156
}
157157
return super.getEnvironmentId(storage);
158158
}
159159

160160

161161
String getUsername(Storage storage) { return storage.getCustomProperty(
162-
PROPERTY_USERNAME); }
162+
PROPERTY_USER); }
163163

164164
boolean hasPassword(Storage storage) {
165-
String password = storage.getCustomProperty(PROPERTY_PASSWORD);
166-
return password != null && password.length() > 0;
165+
String pword = storage.getCustomProperty(PROPERTY_PWORD);
166+
return pword != null && pword.length() > 0;
167167
}
168168

169169
boolean isPersistent(Storage storage) {
@@ -175,24 +175,21 @@ boolean isPersistent(Storage storage) {
175175

176176
// --- Private implementation ----------------------------------------------
177177

178-
private static Map<String, ?> createMap(String username, String password) {
178+
private static Map<String, ?> createMap(String username, char[] pword) {
179179
Map map = new HashMap();
180180

181181
if (username != null && !username.isEmpty())
182-
map.put(JMXConnector.CREDENTIALS,
183-
new String[] { username, decodePassword(password) });
182+
map.put(JMXConnector.CREDENTIALS, new String[] { username, new String(decodePassword(pword)) });
184183

185184
return map;
186185
}
187186

188-
private static char[] encodePassword(char[] password) {
189-
if (password == null) return null;
190-
return Utils.encodePassword(new String(password)).toCharArray();
187+
private static char[] encodePassword(char[] pword) {
188+
return pword == null ? null : Utils.encodePassword(pword);
191189
}
192190

193-
private static String decodePassword(String password) {
194-
if (password == null) return null;
195-
return Utils.decodePassword(password);
191+
private static char[] decodePassword(char[] pword) {
192+
return pword == null ? null : Utils.decodePassword(pword);
196193
}
197194

198195
}

0 commit comments

Comments
 (0)