View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.sift;
15  
16  import ch.qos.logback.classic.spi.ILoggingEvent;
17  import ch.qos.logback.core.sift.AbstractDiscriminator;
18  import ch.qos.logback.core.util.BatchedFixedIntervalInvocationGate;
19  import ch.qos.logback.core.util.Duration;
20  import ch.qos.logback.core.util.OptionHelper;
21  
22  import java.util.Map;
23  
24  /**
25   * MDCBasedDiscriminator essentially returns the value mapped to an MDC key. If
26   * the said value is null, then a default value is returned.
27   * <p/>
28   * <p>
29   * Both Key and the DefaultValue are user specified properties.
30   * </p>
31   * <p>
32   * MDC values containing any of the characters {@code /}, {@code \}, {@code $},
33   * <code>{</code>, <code>}</code>, <code>[</code>, <code>]</code>, <code>(</code>,
34   * <code>)</code> , <code>|</code>, <code>?</code>, <code>*</code>, <code>+</code>,
35   * <code>%</code>, <code>,</code>, <code>@</code> or the sequence {@code ..} are rejected,
36   * in which case the default value is returned. This prevents path separators, relative path components,
37   * variable substitutions such as <code>${file.separator}</code>, regex
38   * special characters as well as the anchor character '%' or the ',' and '@' characters from
39   * leaking into file names or email addresses.
40   * </p>
41   *
42   * <p>Moreover, the maximum length of an MDC value is limited to 64 characters.</p>
43   *
44   * @author Ceki G&uuml;lc&uuml;
45   */
46  public class MDCBasedDiscriminator extends AbstractDiscriminator<ILoggingEvent> {
47  
48      static final String FORBIDDEN_CHARACTERS = "/\\${}[]()|?*+%@,";
49      private static final int MAX_MDC_VALUE = 64;
50  
51      private static final char DOT = '.';
52  
53      static final String EMPTY_VALUE_WARNING = "MDC value [%s] is empty, using default value [%s] instead";
54      static final String REJECTED_VALUE_WARNING = "MDC value [%s] contains forbidden characters, using default value [%s] instead";
55      static final String REJECTED_VALUE_LENGTH_WARNING = "MDC value of length %s is longer than the maximum allowed length of " + MAX_MDC_VALUE + " characters, using default value [%s] instead";
56  
57  
58      private String key;
59      private String defaultValue;
60      /**
61       * Limits how often rejected-value warnings are emitted on the hot path.
62       */
63      private final BatchedFixedIntervalInvocationGate invocationGate =
64              new BatchedFixedIntervalInvocationGate(4, Duration.buildByMinutes(10));
65  
66      @Override
67      public void start() {
68          int errors = 0;
69          if (OptionHelper.isNullOrEmptyOrAllSpaces(key)) {
70              errors++;
71              addError("The \"Key\" property must be set");
72          }
73          if (OptionHelper.isNullOrEmptyOrAllSpaces(defaultValue)) {
74              errors++;
75              addError("The \"DefaultValue\" property must be set");
76          }
77          if (errors == 0) {
78              started = true;
79          }
80      }
81  
82      /**
83       * Return the value associated with an MDC entry designated by the Key property.
84       * If that value is null, then return the value assigned to the DefaultValue
85       * property.
86       * <p>
87       * If the MDC value contains any of the characters {@code /}, {@code \},
88       * {@code $}, <code>{</code> or <code>}</code>, or the sequence {@code ..}, then
89       * the value assigned to the DefaultValue property is returned.
90       * </p>
91       */
92      public String getDiscriminatingValue(ILoggingEvent event) {
93          // http://jira.qos.ch/browse/LBCLASSIC-213
94          Map<String, String> mdcMap = event.getMDCPropertyMap();
95          if (mdcMap == null) {
96              return defaultValue;
97          }
98          String mdcValue = mdcMap.get(key);
99          if (mdcValue == null) {
100             return defaultValue;
101         } else {
102             String sanitized = sanitizePathCharacters(mdcValue, event.getTimeStamp());
103             return sanitized == null ? defaultValue : sanitized;
104         }
105     }
106 
107 
108     /**
109      * Returns the value unchanged if it is safe as a file-name segment, and
110      * {@code null} otherwise.
111      * <p>
112      * A value is rejected if it contains any of the characters {@code /},
113      * {@code \}, {@code $}, <code>{</code> or <code>}</code>, or the sequence
114      * {@code ..}. A (rate limited) warning is emitted for each rejected value.
115      * </p>
116      * <p>
117      * Performs a single scan and no allocation.
118      * </p>
119      */
120     String sanitizePathCharacters(String value, long timestamp) {
121         if (value == null) {
122             return null;
123         }
124         final int len = value.length();
125         if(len == 0) {
126             if (!invocationGate.isTooSoon(timestamp)) {
127                 addWarn(String.format(EMPTY_VALUE_WARNING, value, defaultValue));
128             }
129             return null;
130         }
131 
132         if  (len > MAX_MDC_VALUE) {
133             if (!invocationGate.isTooSoon(timestamp)) {
134                 addWarn(String.format(REJECTED_VALUE_LENGTH_WARNING, len, defaultValue));
135             }
136             return null;
137         }
138 
139         char previous = 0;
140         for (int i = 0; i < len; i++) {
141             char c = value.charAt(i);
142             if (isForbiddenCharacter(c) || (c == DOT && previous == DOT)) {
143                 if (!invocationGate.isTooSoon(timestamp)) {
144                     addWarn(String.format(REJECTED_VALUE_WARNING, value, defaultValue));
145                 }
146                 return null;
147             }
148             previous = c;
149         }
150         return value;
151     }
152 
153     private static boolean isForbiddenCharacter(char c) {
154         return FORBIDDEN_CHARACTERS.indexOf(c) != -1;
155     }
156 
157 
158     public String getKey() {
159         return key;
160     }
161 
162     public void setKey(String key) {
163         this.key = key;
164     }
165 
166     /**
167      * @return
168      * @see #setDefaultValue(String)
169      */
170     public String getDefaultValue() {
171         return defaultValue;
172     }
173 
174     /**
175      * The default MDC value in case the MDC is not set for {@link #setKey(String)
176      * mdcKey}.
177      * <p/>
178      * <p>
179      * For example, if {@link #setKey(String) Key} is set to the value "someKey",
180      * and the MDC is not set for "someKey", then this appender will use the default
181      * value, which you can set with the help of this method.
182      *
183      * @param defaultValue
184      */
185     public void setDefaultValue(String defaultValue) {
186         this.defaultValue = defaultValue;
187     }
188 }