Skip to content

Commit dfdbc1b

Browse files
author
TLS-Attacker Developer
committed
Fix Alert(null,null) logging in AlertMessage toCompactString()
Previously, AlertMessage.toCompactString() would return "Alert(null,null)" when the level and description fields were not set. This happened because the method only checked the config field for description but not for level. This commit: - Updates toCompactString() to use config as fallback for both level and description - Returns "not configured" instead of "null" when no values are available - Adds comprehensive unit tests to verify the behavior This provides better logging output before the message is prepared by the AlertPreparator, resolving issue #212.
1 parent b481012 commit dfdbc1b

File tree

2 files changed

+107
-10
lines changed

2 files changed

+107
-10
lines changed

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/protocol/message/AlertMessage.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,30 +114,42 @@ public String toCompactString() {
114114
StringBuilder sb = new StringBuilder();
115115
String levelString;
116116
String descriptionString;
117+
118+
// Determine level string
117119
if (level != null && level.getValue() != null) {
118120
levelString = AlertLevel.getAlertLevel(level.getValue()).name();
121+
} else if (config != null && config.length == 2) {
122+
// Use config as fallback for level
123+
AlertLevel alertLevel = AlertLevel.getAlertLevel((byte) config[0]);
124+
if (alertLevel != null) {
125+
levelString = alertLevel.name();
126+
} else {
127+
levelString = "" + config[0];
128+
}
119129
} else {
120-
levelString = "null";
130+
levelString = "not configured";
121131
}
132+
133+
// Determine description string
122134
if (description != null && description.getValue() != null) {
123135
AlertDescription desc = AlertDescription.getAlertDescription(description.getValue());
124136
if (desc != null) {
125137
descriptionString = desc.name();
126138
} else {
127139
descriptionString = "" + description.getValue();
128140
}
129-
} else {
130-
if (config != null && config.length == 2) {
131-
AlertDescription desc = AlertDescription.getAlertDescription((byte) config[1]);
132-
if (desc != null) {
133-
descriptionString = desc.name();
134-
} else {
135-
descriptionString = "" + config[1];
136-
}
141+
} else if (config != null && config.length == 2) {
142+
// Use config as fallback for description
143+
AlertDescription desc = AlertDescription.getAlertDescription((byte) config[1]);
144+
if (desc != null) {
145+
descriptionString = desc.name();
137146
} else {
138-
descriptionString = "null";
147+
descriptionString = "" + config[1];
139148
}
149+
} else {
150+
descriptionString = "not configured";
140151
}
152+
141153
sb.append("Alert(").append(levelString).append(",").append(descriptionString).append(")");
142154
return sb.toString();
143155
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* TLS-Attacker - A Modular Penetration Testing Framework for TLS
3+
*
4+
* Copyright 2014-2023 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH
5+
*
6+
* Licensed under Apache License, Version 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0.txt
8+
*/
9+
package de.rub.nds.tlsattacker.core.protocol.message;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
import de.rub.nds.tlsattacker.core.constants.AlertDescription;
14+
import de.rub.nds.tlsattacker.core.constants.AlertLevel;
15+
import org.junit.jupiter.api.Test;
16+
17+
public class AlertMessageToCompactStringTest {
18+
19+
@Test
20+
public void testToCompactStringWithNullValues() {
21+
AlertMessage message = new AlertMessage();
22+
// Both level and description are null, no config set
23+
assertEquals("Alert(not configured,not configured)", message.toCompactString());
24+
}
25+
26+
@Test
27+
public void testToCompactStringWithConfig() {
28+
AlertMessage message = new AlertMessage();
29+
// Set config with WARNING level (1) and BAD_RECORD_MAC description (20)
30+
message.setConfig(new byte[] {1, 20});
31+
assertEquals("Alert(WARNING,BAD_RECORD_MAC)", message.toCompactString());
32+
}
33+
34+
@Test
35+
public void testToCompactStringWithExplicitValues() {
36+
AlertMessage message = new AlertMessage();
37+
message.setLevel(AlertLevel.FATAL.getValue());
38+
message.setDescription(AlertDescription.HANDSHAKE_FAILURE.getValue());
39+
assertEquals("Alert(FATAL,HANDSHAKE_FAILURE)", message.toCompactString());
40+
}
41+
42+
@Test
43+
public void testToCompactStringWithUnknownValues() {
44+
AlertMessage message = new AlertMessage();
45+
// Use values that don't correspond to known alert types
46+
message.setConfig(new byte[] {99, 99});
47+
assertEquals("Alert(UNDEFINED,99)", message.toCompactString());
48+
}
49+
50+
@Test
51+
public void testToCompactStringWithPartialConfig() {
52+
AlertMessage message = new AlertMessage();
53+
// Config with only one byte - should fall back to "not configured"
54+
message.setConfig(new byte[] {1});
55+
assertEquals("Alert(not configured,not configured)", message.toCompactString());
56+
}
57+
58+
@Test
59+
public void testToCompactStringWithLevelButNoDescription() {
60+
AlertMessage message = new AlertMessage();
61+
message.setLevel(AlertLevel.WARNING.getValue());
62+
// Description is null, no config
63+
assertEquals("Alert(WARNING,not configured)", message.toCompactString());
64+
}
65+
66+
@Test
67+
public void testToCompactStringWithDescriptionButNoLevel() {
68+
AlertMessage message = new AlertMessage();
69+
message.setDescription(AlertDescription.CLOSE_NOTIFY.getValue());
70+
// Level is null, no config
71+
assertEquals("Alert(not configured,CLOSE_NOTIFY)", message.toCompactString());
72+
}
73+
74+
@Test
75+
public void testToCompactStringPrefersExplicitValuesOverConfig() {
76+
AlertMessage message = new AlertMessage();
77+
// Set config
78+
message.setConfig(new byte[] {1, 20});
79+
// But also set explicit values - these should take precedence
80+
message.setLevel(AlertLevel.FATAL.getValue());
81+
message.setDescription(AlertDescription.UNEXPECTED_MESSAGE.getValue());
82+
assertEquals("Alert(FATAL,UNEXPECTED_MESSAGE)", message.toCompactString());
83+
}
84+
}
85+

0 commit comments

Comments
 (0)