mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(appsync): refuse a long-term access key sent with a session token (#5880)
AWS answers 403 UnrecognizedClientException "The security token included in the request is invalid" to a long-term key with a session token, and to a temporary key with a wrong one. An unknown key, a temporary key with no token and an empty token get the same message with a period. AppSync now uses the session token check API Gateway and Lambda Web share.
This commit is contained in:
1 parent
115a919a83
commit
ffb3d84bad
5 files changed
+106
-43
No files matched your search
+12
@@ -8,6 +8,8 @@ public final class AppSyncAuth {
|
||||
public static final String UNAUTHORIZED_MESSAGE = "You are not authorized to make this call.";
|
||||
public static final String MISSING_AUTHORIZATION_HEADER = "Missing authorization header";
|
||||
public static final String FIELD_UNAUTHORIZED_TYPE = "Unauthorized";
|
||||
public static final String UNRECOGNIZED_CLIENT_TYPE = "UnrecognizedClientException";
|
||||
public static final String INVALID_SECURITY_TOKEN = "The security token included in the request is invalid";
|
||||
|
||||
public static String fieldUnauthorizedMessage(String fieldName, String typeName) {
|
||||
return "Not Authorized to access " + fieldName + " on type " + typeName;
|
||||
@@ -26,6 +28,16 @@ public final class AppSyncAuth {
|
||||
return new AppSyncTransportException(401, UNAUTHORIZED_TYPE, UNAUTHORIZED_MESSAGE);
|
||||
}
|
||||
|
||||
/** An access key AppSync does not know, a temporary key with no session token, or an empty token. */
|
||||
public static AppSyncTransportException unrecognizedClient() {
|
||||
return new AppSyncTransportException(403, UNRECOGNIZED_CLIENT_TYPE, INVALID_SECURITY_TOKEN + ".");
|
||||
}
|
||||
|
||||
/** A session token that does not fit the key. AWS drops the period here. */
|
||||
public static AppSyncTransportException invalidSessionToken() {
|
||||
return new AppSyncTransportException(403, UNRECOGNIZED_CLIENT_TYPE, INVALID_SECURITY_TOKEN);
|
||||
}
|
||||
|
||||
public static AppSyncTransportException missingAuthorizationHeader() {
|
||||
return new AppSyncTransportException(401, UNAUTHORIZED_TYPE, MISSING_AUTHORIZATION_HEADER);
|
||||
}
|
||||
|
||||
+16
-32
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
import io.github.hectorvent.floci.core.common.auth.CredentialScope;
|
||||
import io.github.hectorvent.floci.core.common.auth.SessionTokenCheck;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4AuthorizationHeader;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
|
||||
import io.github.hectorvent.floci.services.appsync.graphql.AppSyncTransportException;
|
||||
@@ -124,7 +125,7 @@ public class IamAuthValidator {
|
||||
if (secretKey == null) {
|
||||
LOG.debugv("AppSync SigV4 request references unregistered access key={0}",
|
||||
SigV4RequestValidator.sanitizeForLog(accessKeyId));
|
||||
throw AppSyncAuth.unauthorized();
|
||||
throw AppSyncAuth.unrecognizedClient();
|
||||
}
|
||||
checkSessionToken(accessKeyId, info.requestHeaders());
|
||||
try {
|
||||
@@ -166,40 +167,19 @@ public class IamAuthValidator {
|
||||
return iamService.findSecretKey(accessKeyId).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rejects a temporary ({@code ASIA...}) credential that does not present the session token
|
||||
* Floci issued with it. Mirrors {@code ExecuteApiSigV4Authorizer.checkSessionToken}: a matching
|
||||
* secret alone is not the whole credential, since AWS requires the session token on every
|
||||
* request made with temporary credentials to confirm it is live and genuinely STS-issued.
|
||||
*
|
||||
* <p>The token is compared against the value recorded at mint time, not merely required to be
|
||||
* present, so a fabricated token is rejected along with a missing one. A session recorded
|
||||
* before Floci tracked tokens has no issued value to compare against ({@code findSessionToken}
|
||||
* is empty there), so presence is all that can be demanded of it: rejecting that case would
|
||||
* lock out an otherwise valid credential.
|
||||
*
|
||||
* <p>The token does not need to ride in {@code SignedHeaders}: comparing it against the issued
|
||||
* value binds it regardless of where it rode, the same way the execute-api authorizer does.
|
||||
*/
|
||||
/** Rejects a session token that does not fit the access key. The local {@code test} key takes any token. */
|
||||
private void checkSessionToken(String accessKeyId, Map<String, String> requestHeaders) {
|
||||
if (!IamService.isTemporaryAccessKey(accessKeyId)) {
|
||||
if (LEGACY_ACCESS_KEY_ID.equals(accessKeyId)) {
|
||||
return;
|
||||
}
|
||||
String presented = header(requestHeaders, SECURITY_TOKEN);
|
||||
if (isBlank(presented)) {
|
||||
LOG.debugv("AppSync SigV4 request uses temporary credential accessKey={0} with no {1}",
|
||||
SigV4RequestValidator.sanitizeForLog(accessKeyId), SECURITY_TOKEN);
|
||||
throw AppSyncAuth.unauthorized();
|
||||
}
|
||||
String issued = iamService.findSessionToken(accessKeyId).orElse(null);
|
||||
if (issued == null) {
|
||||
return;
|
||||
}
|
||||
if (!MessageDigest.isEqual(issued.getBytes(StandardCharsets.UTF_8),
|
||||
presented.getBytes(StandardCharsets.UTF_8))) {
|
||||
LOG.debugv("AppSync SigV4 request presents a session token that does not match the one"
|
||||
+ " issued for accessKey={0}", SigV4RequestValidator.sanitizeForLog(accessKeyId));
|
||||
throw AppSyncAuth.unauthorized();
|
||||
String presented = hasHeader(requestHeaders, SECURITY_TOKEN) ? header(requestHeaders, SECURITY_TOKEN) : null;
|
||||
AppSyncTransportException refusal = switch (SessionTokenCheck.of(iamService, accessKeyId, presented)) {
|
||||
case VALID -> null;
|
||||
case MISSING -> AppSyncAuth.unrecognizedClient();
|
||||
case INVALID -> AppSyncAuth.invalidSessionToken();
|
||||
};
|
||||
if (refusal != null) {
|
||||
throw refusal;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,6 +233,10 @@ public class IamAuthValidator {
|
||||
return "";
|
||||
}
|
||||
|
||||
private static boolean hasHeader(Map<String, String> headers, String name) {
|
||||
return headers != null && headers.keySet().stream().anyMatch(name::equalsIgnoreCase);
|
||||
}
|
||||
|
||||
private static boolean isBlank(String value) {
|
||||
return value == null || value.isBlank();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user