mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(auth): reject ambiguous SigV4 authorization headers (#8332)
* fix(auth): reject ambiguous SigV4 authorization headers Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
5df26c5135
commit
e182cdad82
3 files changed
+242
-1
No files matched your search
@@ -22,6 +22,8 @@ public record SigV4AuthorizationHeader(String credential, String signedHeaders,
|
||||
* algorithm. A parameter missing from an otherwise well-formed header comes back null on the
|
||||
* record instead, left for the caller to reject alongside whatever else it requires (e.g. an
|
||||
* {@code X-Amz-Date} it reads from a different source than this header).
|
||||
* Duplicate parameters and conflicting or incomplete repeated headers return a record with
|
||||
* null parameters. Equivalent complete repeated headers resolve to the same parameters.
|
||||
*/
|
||||
public static SigV4AuthorizationHeader parse(String authorization) {
|
||||
return parse(authorization, ALGORITHM);
|
||||
@@ -37,16 +39,48 @@ public record SigV4AuthorizationHeader(String credential, String signedHeaders,
|
||||
return null;
|
||||
}
|
||||
Map<String, String> parameters = new LinkedHashMap<>();
|
||||
Map<String, String> firstHeader = null;
|
||||
boolean missingEqualSign = false;
|
||||
for (String part : trimmed.substring(algorithm.length()).split(",")) {
|
||||
part = part.trim();
|
||||
if (startsHeader(part, algorithm)) {
|
||||
if (missingEqualSign || !complete(parameters)
|
||||
|| firstHeader != null && !firstHeader.equals(parameters)) {
|
||||
return incomplete(missingEqualSign);
|
||||
}
|
||||
firstHeader = parameters;
|
||||
parameters = new LinkedHashMap<>();
|
||||
part = part.substring(algorithm.length()).trim();
|
||||
}
|
||||
int equals = part.indexOf('=');
|
||||
if (equals > 0) {
|
||||
parameters.put(part.substring(0, equals).trim(), part.substring(equals + 1).trim());
|
||||
String name = part.substring(0, equals).trim();
|
||||
if (parameters.putIfAbsent(name, part.substring(equals + 1).trim()) != null) {
|
||||
return incomplete(missingEqualSign);
|
||||
}
|
||||
} else if (equals < 0 && !part.isBlank()) {
|
||||
missingEqualSign = true;
|
||||
}
|
||||
}
|
||||
if (firstHeader != null && (missingEqualSign || !complete(parameters) || !firstHeader.equals(parameters))) {
|
||||
return incomplete(missingEqualSign);
|
||||
}
|
||||
return new SigV4AuthorizationHeader(parameters.get("Credential"), parameters.get("SignedHeaders"),
|
||||
parameters.get("Signature"), missingEqualSign);
|
||||
}
|
||||
|
||||
private static boolean startsHeader(String part, String algorithm) {
|
||||
return part.regionMatches(true, 0, algorithm, 0, algorithm.length())
|
||||
&& (part.length() == algorithm.length() || Character.isWhitespace(part.charAt(algorithm.length())));
|
||||
}
|
||||
|
||||
private static boolean complete(Map<String, String> parameters) {
|
||||
return !parameters.getOrDefault("Credential", "").isBlank()
|
||||
&& !parameters.getOrDefault("SignedHeaders", "").isBlank()
|
||||
&& !parameters.getOrDefault("Signature", "").isBlank();
|
||||
}
|
||||
|
||||
private static SigV4AuthorizationHeader incomplete(boolean missingEqualSign) {
|
||||
return new SigV4AuthorizationHeader(null, null, null, missingEqualSign);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user