diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java index 46b757d54f2..617c29ad5d6 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java @@ -21,11 +21,15 @@ import org.apache.logging.log4j.util.StringBuilders; /** - * Represents a Message that conforms to an RFC 5424 StructuredData element along with the syslog message. + * Represents a Message that conforms to an RFC 5424 StructuredData element + * along with the syslog message. *
- * Thread-safety note: the contents of this message can be modified after construction. - * When using asynchronous loggers and appenders it is not recommended to modify this message after the message is - * logged, because it is undefined whether the logged message string will contain the old values or the modified + * Thread-safety note: the contents of this message can be modified after + * construction. + * When using asynchronous loggers and appenders it is not recommended to modify + * this message after the message is + * logged, because it is undefined whether the logged message string will + * contain the old values or the modified * values. *
* @@ -38,11 +42,11 @@ public class StructuredDataMessage extends MapMessage+ * The {@code msgId} parameter represents the syslog {@code MSGID} and is + * expected to conform to + * RFC + * 5424 Section 6.2.7. + *
+ *+ * Both {@code sdId} and {@code msgId} are considered trusted inputs (typically + * compile-time constants). + * If these values are derived from external or untrusted sources, it is the + * caller's responsibility + * to validate and sanitize them to ensure RFC-compliant output, especially when + * used with + * {@code Rfc5424Layout}. + *
+ * + * @param sdId The String SD-ID. + * @param msg The message. + * @param msgId The identifier MSGID. */ - public StructuredDataMessage(final String id, final String msg, final String type) { - this(id, msg, type, MAX_LENGTH); + public StructuredDataMessage(final String sdId, final String msg, final String msgId) { + this(sdId, msg, msgId, MAX_LENGTH); } /** - * Creates a StructuredDataMessage using an ID (user specified max characters), message, and type (user specified + * Creates a StructuredDataMessage using an SD-ID (user specified max + * characters), + * message, and MSGID (user specified * maximum number of characters). - * @param id The String id. - * @param msg The message. - * @param type The message type. + *+ * The {@code sdId} parameter represents the syslog {@code SD-ID} and is + * expected + * to conform to + * RFC + * 5424 Section 6.3.2. + * It is recommended to use {@link StructuredDataId} instead of a raw + * {@link String} where possible, + * as it allows specifying a set of allowed keys for structured data elements. + *
+ *+ * The {@code msgId} parameter represents the syslog {@code MSGID} and is + * expected to conform to + * RFC + * 5424 Section 6.2.7. + *
+ *+ * Both {@code sdId} and {@code msgId} are considered trusted inputs (typically + * compile-time constants). + * If these values are derived from external or untrusted sources, it is the + * caller's responsibility + * to validate and sanitize them to ensure RFC-compliant output, especially when + * used with + * {@code Rfc5424Layout}. + *
+ * + * @param sdId The String SD-ID. + * @param msg The message. + * @param msgId The message identifier MSGID. * @param maxLength The maximum length of keys; * @since 2.9.0 */ - public StructuredDataMessage(final String id, final String msg, final String type, final int maxLength) { - this.id = new StructuredDataId(id, null, null, maxLength); + public StructuredDataMessage(final String sdId, final String msg, final String msgId, final int maxLength) { + validateSdId(sdId); + validateMsgId(msgId); + + this.sdId = new StructuredDataId(sdId, null, null, maxLength); this.message = msg; - this.type = type; + this.msgId = msgId; this.maxLength = maxLength; } /** - * Creates a StructuredDataMessage using an ID (max 32 characters), message, type (max 32 characters), and an + * Creates a StructuredDataMessage using an SD-ID (max 32 characters), message, + * MSGID (max 32 characters), and an * initial map of structured data to include. - * @param id The String id. - * @param msg The message. - * @param type The message type. - * @param data The StructuredData map. + *+ * The {@code sdId} parameter represents the syslog {@code SD-ID} and is + * expected + * to conform to + * RFC + * 5424 Section 6.3.2. + * It is recommended to use {@link StructuredDataId} instead of a raw + * {@link String} where possible, + * as it allows specifying a set of allowed keys for structured data elements. + *
+ *+ * The {@code msgId} parameter represents the syslog {@code MSGID} and is + * expected to conform to + * RFC + * 5424 Section 6.2.7. + *
+ *+ * Both {@code sdId} and {@code msgId} are considered trusted inputs (typically + * compile-time constants). + * If these values are derived from external or untrusted sources, it is the + * caller's responsibility + * to validate and sanitize them to ensure RFC-compliant output, especially when + * used with + * {@code Rfc5424Layout}. + *
+ * + * @param sdId The String SD-ID. + * @param msg The message. + * @param msgId The message identifier MSGID. + * @param data The StructuredData map. */ - public StructuredDataMessage(final String id, final String msg, final String type, final Map+ * The {@code sdId} parameter represents the syslog {@code SD-ID} and is + * expected + * to conform to + * RFC + * 5424 Section 6.3.2. + * It is recommended to use {@link StructuredDataId} instead of a raw + * {@link String} where possible, + * as it allows specifying a set of allowed keys for structured data elements. + *
+ *+ * The {@code msgId} parameter represents the syslog {@code MSGID} and is + * expected to conform to + * RFC + * 5424 Section 6.2.7. + *
+ *+ * Both {@code sdId} and {@code msgId} are considered trusted inputs (typically + * compile-time constants). + * If these values are derived from external or untrusted sources, it is the + * caller's responsibility + * to validate and sanitize them to ensure RFC-compliant output, especially when + * used with + * {@code Rfc5424Layout}. + *
+ * + * @param sdId The String SD-ID. + * @param msg The message. + * @param msgId The message identifier. + * @param data The StructuredData map. * @param maxLength The maximum length of keys; * @since 2.9.0 */ public StructuredDataMessage( - final String id, final String msg, final String type, final Map+ * The {@link StructuredDataId} parameter represents the syslog {@code SD-ID} + * and is expected to conform to + * RFC + * 5424 Section 6.3.2. + *
+ *+ * The {@code msgId} parameter represents the syslog {@code MSGID} and is + * expected to conform to + * RFC + * 5424 Section 6.2.7. + *
+ *+ * Both {@code sdId} and {@code msgId} are considered trusted inputs (typically + * compile-time constants). + * If these values are derived from external or untrusted sources, it is the + * caller's responsibility + * to validate and sanitize them to ensure RFC-compliant output, especially when + * used with + * {@code Rfc5424Layout}. + *
+ * + * @param sdId The StructuredDataId. + * @param msg The message. + * @param msgId The message identifier MSGID. + */ + public StructuredDataMessage(final StructuredDataId sdId, final String msg, final String msgId) { + this(sdId, msg, msgId, MAX_LENGTH); + } + + /** + * Creates a StructuredDataMessage using a StructuredDataId, message, and MSGID + * (max 32 characters). + *+ * The {@link StructuredDataId} parameter represents the syslog {@code SD-ID} + * and is expected to conform to + * RFC + * 5424 Section 6.3.2. + *
+ *+ * The {@code msgId} parameter represents the syslog {@code MSGID} and is + * expected to conform to + * RFC + * 5424 Section 6.2.7. + *
+ *+ * Both {@code sdId} and {@code msgId} are considered trusted inputs (typically + * compile-time constants). + * If these values are derived from external or untrusted sources, it is the + * caller's responsibility + * to validate and sanitize them to ensure RFC-compliant output, especially when + * used with + * {@code Rfc5424Layout}. + *
+ * + * @param sdId The StructuredDataId. + * @param msg The message. + * @param msgId The message identifier MSGID. * @param maxLength The maximum length of keys; * @since 2.9.0 */ - public StructuredDataMessage(final StructuredDataId id, final String msg, final String type, final int maxLength) { - this.id = id; + public StructuredDataMessage( + final StructuredDataId sdId, final String msg, final String msgId, final int maxLength) { + + if (sdId == null) { + throw new IllegalArgumentException("SD-ID cannot be null"); + } + validateMsgId(msgId); + + this.sdId = sdId; this.message = msg; - this.type = type; + this.msgId = msgId; this.maxLength = maxLength; } /** - * Creates a StructuredDataMessage using a StructuredDataId, message, type (max 32 characters), and an initial map + * Creates a StructuredDataMessage using a StructuredDataId, message, MSGID (max + * 32 characters), and an initial map * of structured data to include. - * @param id The StructuredDataId. - * @param msg The message. - * @param type The message type. - * @param data The StructuredData map. + *+ * The {@link StructuredDataId} parameter represents the syslog {@code SD-ID} + * and is expected to conform to + * RFC + * 5424 Section 6.3.2. + *
+ *+ * The {@code msgId} parameter represents the syslog {@code MSGID} and is + * expected to conform to + * RFC + * 5424 Section 6.2.7. + *
+ *+ * Both {@code sdId} and {@code msgId} are considered trusted inputs (typically + * compile-time constants). + * If these values are derived from external or untrusted sources, it is the + * caller's responsibility + * to validate and sanitize them to ensure RFC-compliant output, especially when + * used with + * {@code Rfc5424Layout}. + *
+ * + * @param sdId The StructuredDataId. + * @param msg The message. + * @param msgId The message identifier MSGID. + * @param data The StructuredData map. */ public StructuredDataMessage( - final StructuredDataId id, final String msg, final String type, final Map+ * The {@link StructuredDataId} parameter represents the syslog {@code SD-ID} + * and is expected to conform to + * RFC + * 5424 Section 6.3.2. + *
+ *+ * The {@code msgId} parameter represents the syslog {@code MSGID} and is + * expected to conform to + * RFC + * 5424 Section 6.2.7. + *
+ *+ * Both {@code sdId} and {@code msgId} are considered trusted inputs (typically + * compile-time constants). + * If these values are derived from external or untrusted sources, it is the + * caller's responsibility + * to validate and sanitize them to ensure RFC-compliant output, especially when + * used with + * {@code Rfc5424Layout}. + *
+ * + * @param sdId The StructuredDataId. + * @param msg The message. + * @param msgId The message identifier MSGID. + * @param data The StructuredData map. * @param maxLength The maximum length of keys; * @since 2.9.0 */ public StructuredDataMessage( - final StructuredDataId id, + final StructuredDataId sdId, final String msg, - final String type, + final String msgId, final Map