-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathAttributeTest.java
More file actions
225 lines (176 loc) · 6.64 KB
/
AttributeTest.java
File metadata and controls
225 lines (176 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package io.jenkins.plugins.casc;
import hudson.util.Secret;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.For;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.LoggerRule;
import org.kohsuke.stapler.DataBoundConstructor;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@For(Attribute.class)
public class AttributeTest {
@Rule
public LoggerRule logging = new LoggerRule();
@Before
public void tearUp() {
logging.record(Logger.getLogger(Attribute.class.getName()), Level.FINEST).capture(2048);
}
@After
public void tearDown() {
for (String entry : logging.getMessages()) {
System.out.println(entry);
}
}
@Test
@Issue("SECURITY-1279")
public void checkCommonSecretPatterns() {
assertFieldIsSecret(WellDefinedField.class, "secretField");
assertFieldIsSecret(SecretFromGetter.class, "secretField");
assertFieldIsSecret(SecretFromPublicField.class, "secretField");
assertFieldIsSecret(SecretFromPrivateField.class, "secretField");
assertFieldIsSecret(SecretFromImpliedAttributeName.class, "password");
assertFieldIsSecret(SecretRenamedFieldFithSecretConstructor.class, "mySecretValueField");
}
@Test
@Issue("SECURITY-1279")
public void checkStaticResolution() {
// Field is not secret, but the attribute is secret
assertFieldIsNotSecret(SecretRenamedFieldFithSecretConstructor.class, "mySecretValue");
}
@Test
@Issue("SECURITY-1279")
public void checkCommonSecretPatternsForOverrides() {
assertFieldIsSecret(WellDefinedField2.class, "secret");
assertFieldIsSecret(SecretFromGetter2.class, "secretField");
// Fields
assertFieldIsSecret(SecretFromPublicField2.class, "secretField");
assertFieldIsSecret(SecretFromPrivateField2.class, "secretField");
assertFieldIsSecret(SecretFromPrivateField3.class, "secretField");
assertFieldIsSecret(SecretFromImpliedAttributeName2.class, "password");
}
@Test
@Issue("SECURITY-1279")
public void checkNonSecretPatterns() {
assertFieldIsNotSecret(NonSecretField.class, "passwordPath");
}
@Test
public void shouldConsiderSuffixes() {
assertFieldIsSecret(null, "secretKey");
assertFieldIsSecret(null, "mySecretKey");
assertFieldIsSecret(null, "myPwd");
assertFieldIsSecret(null, "superSecretPassword");
// Examples of false positives
assertFieldIsSecret(null, "pathToSecretKey");
assertFieldIsSecret(null, "foretoken"); // https://en.wiktionary.org/wiki/foretoken
}
@Test
public void shouldNotConsiderSuffixesInTheMiddle() {
assertFieldIsNotSecret(null, "passwordPath");
assertFieldIsNotSecret(null, "passwordFile");
assertFieldIsNotSecret(null, "tokenSource");
}
public static void assertFieldIsSecret(Class<?> clazz, String fieldName) {
String displayName = clazz != null ? (clazz.getName() + "#" + fieldName) : fieldName;
assertTrue("Field is not secret: " + displayName,
Attribute.calculateIfSecret(clazz, fieldName));
}
public static void assertFieldIsNotSecret(Class<?> clazz, String fieldName) {
String displayName = clazz != null ? (clazz.getName() + "#" + fieldName) : fieldName;
assertFalse("Field is a secret while it should not be considered as one: " + displayName,
Attribute.calculateIfSecret(clazz, fieldName));
}
public static class WellDefinedField {
Secret secretField;
@DataBoundConstructor
public WellDefinedField(Secret secretField) {
this.secretField = secretField;
}
public Secret getSecret() {
return secretField;
}
}
public static class WellDefinedField2 extends WellDefinedField {
public WellDefinedField2(Secret secret) {
super(secret);
}
}
public static class SecretFromGetter {
Secret secretField;
@DataBoundConstructor
public SecretFromGetter(String secretField) {
this.secretField = Secret.fromString(secretField);
}
public Secret getSecret() {
return secretField;
}
}
public static class SecretFromGetter2 extends SecretFromGetter {
public SecretFromGetter2(String secret) {
super(secret);
}
}
public static class SecretFromPublicField {
public Secret secretField;
@DataBoundConstructor
public SecretFromPublicField(String secretField) {
this.secretField = Secret.fromString(secretField);
}
}
public static class SecretFromPublicField2 extends SecretFromPublicField {
public SecretFromPublicField2(String secret) {
super(secret);
}
}
public static class SecretFromPrivateField {
private Secret secretField;
@DataBoundConstructor
public SecretFromPrivateField(String secretField) {
this.secretField = Secret.fromString(secretField);
}
}
public static class SecretFromPrivateField2 extends SecretFromPrivateField {
public SecretFromPrivateField2(String secret) {
super(secret);
}
}
public static class SecretFromPrivateField3 extends SecretFromPrivateField2 {
public SecretFromPrivateField3(String secret) {
super(secret);
}
}
public static class SecretFromImpliedAttributeName {
public String password;
@DataBoundConstructor
public SecretFromImpliedAttributeName(String password) {
this.password = password;
}
}
public static class SecretFromImpliedAttributeName2 extends SecretFromImpliedAttributeName {
@DataBoundConstructor
public SecretFromImpliedAttributeName2(String password) {
super(password);
}
}
public static class NonSecretField {
public String passwordPath;
@DataBoundConstructor
public NonSecretField(String passwordPath) {
this.passwordPath = passwordPath;
}
}
public static class SecretRenamedFieldFithSecretConstructor {
Secret mySecretValueField;
@DataBoundConstructor
public SecretRenamedFieldFithSecretConstructor(Secret mySecretValue) {
this.mySecretValueField = mySecretValue;
}
public String getMySecretValue() {
return mySecretValueField.getPlainText();
}
}
}