Skip to content

Commit eb6e05e

Browse files
authored
Merge pull request #575 from Nizernizer/fix/taint_max_value
Fix/taint max value
2 parents c5c7a47 + f065631 commit eb6e05e

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

dongtai-common/src/main/java/io/dongtai/iast/common/constants/PropertyConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ public class PropertyConstant {
3232
public static final String PROPERTY_UUID_PATH = "dongtai.uuid.path";
3333
public static final String PROPERTY_DISABLED_PLUGINS = "dongtai.disabled.plugins";
3434
public static final String PROPERTY_DISABLED_FEATURES = "dongtai.disabled.features";
35+
public static final String PROPERTY_TAINT_LENGTH = "dongtai.taint.length";
3536
}

dongtai-core/src/main/java/io/dongtai/iast/core/handler/hookpoint/models/MethodEvent.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.alibaba.fastjson2.JSONObject;
44
import io.dongtai.iast.core.handler.hookpoint.models.policy.TaintPosition;
55
import io.dongtai.iast.core.handler.hookpoint.models.taint.range.TaintRanges;
6+
import io.dongtai.iast.core.utils.PropertyUtils;
67
import io.dongtai.iast.core.utils.StringUtils;
78

89
import java.io.StringWriter;
@@ -75,7 +76,7 @@ public class MethodEvent {
7576
/**
7677
* method all parameters string value
7778
*/
78-
public List<Parameter> parameterValues = new ArrayList<Parameter>();
79+
public List<Parameter> parameterValues = new ArrayList<>();
7980

8081
/**
8182
* method return instance
@@ -87,13 +88,13 @@ public class MethodEvent {
8788
*/
8889
public String returnValue;
8990

90-
private final Set<Long> sourceHashes = new HashSet<Long>();
91+
private final Set<Long> sourceHashes = new HashSet<>();
9192

92-
private final Set<Long> targetHashes = new HashSet<Long>();
93+
private final Set<Long> targetHashes = new HashSet<>();
9394

94-
public List<MethodEventTargetRange> targetRanges = new ArrayList<MethodEventTargetRange>();
95+
public List<MethodEventTargetRange> targetRanges = new ArrayList<>();
9596

96-
public List<MethodEventTargetRange> sourceRanges = new ArrayList<MethodEventTargetRange>();
97+
public List<MethodEventTargetRange> sourceRanges = new ArrayList<>();
9798

9899
public List<MethodEventSourceType> sourceTypes;
99100

@@ -231,7 +232,7 @@ public void addParameterValue(int index, Object param, boolean hasTaint) {
231232
if (param == null) {
232233
return;
233234
}
234-
String indexString = "P" + String.valueOf(index + 1);
235+
String indexString = "P" + (index + 1);
235236
Parameter parameter = new Parameter(indexString, formatValue(param, hasTaint));
236237
this.parameterValues.add(parameter);
237238
}
@@ -246,7 +247,7 @@ public void setReturnValue(Object ret, boolean hasTaint) {
246247
private String formatValue(Object val, boolean hasTaint) {
247248
String str = obj2String(val);
248249
return "[" + StringUtils.normalize(str, MAX_VALUE_LENGTH) + "]"
249-
+ (hasTaint ? "*" : "") + String.valueOf(str.length());
250+
+ (hasTaint ? "*" : "") + str.length();
250251
}
251252

252253
public Set<Long> getSourceHashes() {
@@ -286,6 +287,7 @@ public void setCallStack(StackTraceElement callStack) {
286287
}
287288

288289
public String obj2String(Object value) {
290+
int taintValueLength = PropertyUtils.getInstance().getTaintValueLength();
289291
StringBuilder sb = new StringBuilder();
290292
if (null == value) {
291293
return "";
@@ -299,27 +301,37 @@ public String obj2String(Object value) {
299301
if (taint.getClass().isArray() && !taint.getClass().getComponentType().isPrimitive()) {
300302
Object[] subTaints = (Object[]) taint;
301303
for (Object subTaint : subTaints) {
302-
sb.append(subTaint.toString()).append(" ");
304+
appendWithMaxLength(sb, subTaint.toString() + " ", taintValueLength);
303305
}
304306
} else {
305-
sb.append(taint.toString()).append(" ");
307+
appendWithMaxLength(sb, taint.toString() + " ", taintValueLength);
306308
}
307309
}
308310
}
309311
} else if (value instanceof StringWriter) {
310-
sb.append(((StringWriter) value).getBuffer().toString());
312+
appendWithMaxLength(sb, ((StringWriter) value).getBuffer().toString(), taintValueLength);
311313
} else {
312-
sb.append(value.toString());
314+
appendWithMaxLength(sb, value.toString(), taintValueLength);
313315
}
314316
} catch (Throwable e) {
315317
// org.jruby.RubyBasicObject.hashCode() may cause NullPointerException when RubyBasicObject.metaClass is null
316-
sb.append(value.getClass().getName())
317-
.append("@")
318-
.append(Integer.toHexString(System.identityHashCode(value)));
318+
String typeName = value.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(value));
319+
appendWithMaxLength(sb, typeName, taintValueLength);
319320
}
320321
return sb.toString();
321322
}
322323

324+
private void appendWithMaxLength(StringBuilder sb, String content, int maxLength) {
325+
if (sb.length() + content.length() > maxLength) {
326+
int remainingSpace = maxLength - sb.length();
327+
if (remainingSpace > 0) {
328+
sb.append(content, 0, remainingSpace);
329+
}
330+
} else {
331+
sb.append(content);
332+
}
333+
}
334+
323335
public List<Object> getStacks() {
324336
return stacks;
325337
}

dongtai-core/src/main/java/io/dongtai/iast/core/utils/PropertyUtils.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@ public class PropertyUtils {
2323
private String iastDumpPath;
2424
private Long heartBeatInterval = -1L;
2525
private String serverUrl;
26-
private String serverMode;
2726
private String proxyEnableStatus;
2827
private String proxyHost;
2928
private int proxyPort = -1;
30-
private String debugFlag;
3129
private Integer responseLength;
3230
private String policyPath;
3331
private static List<String> disabledFeatureList;
3432
private static Boolean isDisabledCustomModel;
3533

3634
private final String propertiesFilePath;
3735

36+
private int taintValueLength = -1;
37+
38+
3839
public static PropertyUtils getInstance(String propertiesFilePath) {
3940
if (null == instance) {
4041
instance = new PropertyUtils(propertiesFilePath);
@@ -180,13 +181,6 @@ public int getProxyPort() {
180181
return proxyPort;
181182
}
182183

183-
private String getDebugFlag() {
184-
if (debugFlag == null) {
185-
debugFlag = System.getProperty(PropertyConstant.PROPERTY_DEBUG, "false");
186-
}
187-
return debugFlag;
188-
}
189-
190184
public Integer getResponseLength() {
191185
if (responseLength == null) {
192186
responseLength = Integer.parseInt(System.getProperty(PropertyConstant.PROPERTY_RESPONSE_LENGTH,
@@ -229,4 +223,13 @@ public static Boolean isDisabledCustomModel() {
229223
public static Boolean validatedSink() {
230224
return ConfigBuilder.getInstance().get(ConfigKey.VALIDATED_SINK);
231225
}
226+
227+
public int getTaintValueLength() {
228+
if (-1 == taintValueLength) {
229+
taintValueLength = Integer
230+
.parseInt(System.getProperty(PropertyConstant.PROPERTY_TAINT_LENGTH,
231+
cfg.getProperty(PropertyConstant.PROPERTY_TAINT_LENGTH, "1024")));
232+
}
233+
return taintValueLength;
234+
}
232235
}

0 commit comments

Comments
 (0)