github-advanced-security[bot] commented on code in PR #3680:
URL: https://github.com/apache/cxf/pull/3680#discussion_r4561362487
##########
rt/rs/security/http-signature/src/main/java/org/apache/cxf/rs/security/httpsignature/MessageVerifier.java:
##########
@@ -151,9 +167,74 @@
method,
uri,
signedHeaders);
+ verifyDateHeader(messageHeaders, m);
LOG.fine("Finished signature verification");
}
+ /**
+ * Reject a message whose signed "Date" header is outside the configured
time window, so that a captured
+ * signed message can not be replayed once its time to live has passed. A
"Date" header which is not
+ * covered by the signature is ignored, as it can be changed along with
the rest of the unsigned message.
+ * Values set on this class take precedence over the contextual properties.
+ */
+ private void verifyDateHeader(Map<String, List<String>> messageHeaders,
Message m) {
+ int ttl = timeToLive != null ? timeToLive
+ : getContextualInteger(m,
HTTPSignatureConstants.RSSEC_HTTP_SIGNATURE_TIME_TO_LIVE, DEFAULT_TIME_TO_LIVE);
+ int futureTtl = futureTimeToLive != null ? futureTimeToLive
+ : getContextualInteger(m,
HTTPSignatureConstants.RSSEC_HTTP_SIGNATURE_FUTURE_TIME_TO_LIVE,
+ DEFAULT_FUTURE_TIME_TO_LIVE);
+ // A time to live of 0 would reject every message, so 0 or less
disables the age check. For the future
+ // check 0 means no clock skew is allowed and only a negative value
disables it.
+ if (ttl <= 0 && futureTtl < 0) {
+ return;
+ }
+ List<String> signedHeaders =
Signature.fromString(messageHeaders.get("Signature").get(0)).getHeaders();
+ if (!signedHeaders.contains("date")) {
+ return;
+ }
+ // Check the same value that was signed, see
SignatureHeaderUtils.mapHeaders
+ List<String> dates = new ArrayList<>();
+ for (Map.Entry<String, List<String>> entry :
messageHeaders.entrySet()) {
+ if ("date".equalsIgnoreCase(entry.getKey())) {
+ dates.addAll(entry.getValue());
+ }
+ }
+ if (dates.isEmpty()) {
+ return;
+ }
+ if (dates.size() > 1) {
+ throw new InvalidSignatureException("there are multiple signed
Date header values");
+ }
+ final long dateTime;
+ try {
+ dateTime =
HttpUtils.getHttpDateFormat().parse(dates.get(0)).getTime();
+ } catch (ParseException ex) {
+ throw new InvalidSignatureException("the signed Date header can
not be parsed");
+ }
+ long now = System.currentTimeMillis();
+ if (ttl > 0 && now - dateTime > TimeUnit.SECONDS.toMillis(ttl)) {
+ throw new InvalidSignatureException("the signed Date header is
older than the time to live");
+ }
+ if (futureTtl >= 0 && dateTime - now >
TimeUnit.SECONDS.toMillis(futureTtl)) {
+ throw new InvalidSignatureException("the signed Date header is in
the future");
+ }
+ }
+
+ private static int getContextualInteger(Message m, String key, int
defaultValue) {
+ Object value = m.getContextualProperty(key);
+ if (value instanceof Number) {
+ return ((Number)value).intValue();
+ }
+ if (value instanceof String) {
+ try {
+ return Integer.parseInt((String)value);
+ } catch (NumberFormatException ex) {
+ LOG.warning("Incorrect integer value of " + value + "
specified for: " + key);
Review Comment:
## CodeQL / Log Injection
This log entry depends on a [user-provided value](1).
This log entry depends on a [user-provided value](2).
This log entry depends on a [user-provided value](3).
[Show more
details](https://github.com/apache/cxf/security/code-scanning/2263)
##########
rt/rs/security/http-signature/src/main/java/org/apache/cxf/rs/security/httpsignature/MessageVerifier.java:
##########
@@ -151,9 +167,74 @@
method,
uri,
signedHeaders);
+ verifyDateHeader(messageHeaders, m);
LOG.fine("Finished signature verification");
}
+ /**
+ * Reject a message whose signed "Date" header is outside the configured
time window, so that a captured
+ * signed message can not be replayed once its time to live has passed. A
"Date" header which is not
+ * covered by the signature is ignored, as it can be changed along with
the rest of the unsigned message.
+ * Values set on this class take precedence over the contextual properties.
+ */
+ private void verifyDateHeader(Map<String, List<String>> messageHeaders,
Message m) {
+ int ttl = timeToLive != null ? timeToLive
+ : getContextualInteger(m,
HTTPSignatureConstants.RSSEC_HTTP_SIGNATURE_TIME_TO_LIVE, DEFAULT_TIME_TO_LIVE);
+ int futureTtl = futureTimeToLive != null ? futureTimeToLive
+ : getContextualInteger(m,
HTTPSignatureConstants.RSSEC_HTTP_SIGNATURE_FUTURE_TIME_TO_LIVE,
+ DEFAULT_FUTURE_TIME_TO_LIVE);
+ // A time to live of 0 would reject every message, so 0 or less
disables the age check. For the future
+ // check 0 means no clock skew is allowed and only a negative value
disables it.
+ if (ttl <= 0 && futureTtl < 0) {
+ return;
+ }
+ List<String> signedHeaders =
Signature.fromString(messageHeaders.get("Signature").get(0)).getHeaders();
+ if (!signedHeaders.contains("date")) {
+ return;
+ }
+ // Check the same value that was signed, see
SignatureHeaderUtils.mapHeaders
+ List<String> dates = new ArrayList<>();
+ for (Map.Entry<String, List<String>> entry :
messageHeaders.entrySet()) {
+ if ("date".equalsIgnoreCase(entry.getKey())) {
+ dates.addAll(entry.getValue());
+ }
+ }
+ if (dates.isEmpty()) {
+ return;
+ }
+ if (dates.size() > 1) {
+ throw new InvalidSignatureException("there are multiple signed
Date header values");
+ }
+ final long dateTime;
+ try {
+ dateTime =
HttpUtils.getHttpDateFormat().parse(dates.get(0)).getTime();
+ } catch (ParseException ex) {
+ throw new InvalidSignatureException("the signed Date header can
not be parsed");
+ }
+ long now = System.currentTimeMillis();
+ if (ttl > 0 && now - dateTime > TimeUnit.SECONDS.toMillis(ttl)) {
+ throw new InvalidSignatureException("the signed Date header is
older than the time to live");
+ }
+ if (futureTtl >= 0 && dateTime - now >
TimeUnit.SECONDS.toMillis(futureTtl)) {
+ throw new InvalidSignatureException("the signed Date header is in
the future");
+ }
+ }
+
+ private static int getContextualInteger(Message m, String key, int
defaultValue) {
+ Object value = m.getContextualProperty(key);
+ if (value instanceof Number) {
+ return ((Number)value).intValue();
+ }
+ if (value instanceof String) {
+ try {
+ return Integer.parseInt((String)value);
+ } catch (NumberFormatException ex) {
+ LOG.warning("Incorrect integer value of " + value + "
specified for: " + key);
Review Comment:
## CodeQL / Insertion of sensitive information into log files
This [potentially sensitive information](1) is written to a log file.
[Show more
details](https://github.com/apache/cxf/security/code-scanning/2262)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]