mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): answer a refused MFA device as AWS words it (#8855)
Every MFA refusal here was this package's own sentence. IAM answers EnableMFADevice and ResyncMFADevice with 'Authentication code for device is not valid.' and a duplicate virtual device with 'MFA device already exists.', both recorded and both wholly fixed text. STS answers two sentences rather than one, by which half of the claim failed: a serial no device of the caller's answers to, or a code that device did not produce. Both are 403 AccessDenied, so the sentence is the only thing that tells a caller which half to look at, and the whitespace inside them is AWS's own. RequestMfaClaim carries the reason now, so the seam both session operations share can word them apart.
This commit is contained in:
1 parent
29fc72d156
commit
532b64eaea
7 files changed
+524
-25
No files matched your search
@@ -3,25 +3,47 @@ package io.floci.aws.core.common;
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* What one request's MFA members amount to: nothing claimed, a device presented and authenticated
|
||||
* at an instant, or a claim that failed.
|
||||
* The MFA a request claimed and whether this emulator could make the claim good, computed once per
|
||||
* request because a one time code is spent when it authenticates.
|
||||
*
|
||||
* <p>AWS "provides two API operations that let users pass MFA information: GetSessionToken and
|
||||
* AssumeRole", and a claim either holds or is refused: "if a policy includes an MFA condition, a
|
||||
* request is denied if users have not been MFA authenticated, or if they provide an invalid MFA
|
||||
* device identifier or invalid TOTP".
|
||||
*
|
||||
* <p>It is read twice in one request and computed once, which is why it is held on the request
|
||||
* rather than recomputed where it is needed. The enforcement filter needs it because the policy for
|
||||
* the call is evaluated there, against the request that carries the device; the STS operation needs
|
||||
* it because it answers the refusal and mints the session, and because it runs whether or not
|
||||
* enforcement is on. Computing it twice would not do: a code is spent when it authenticates, so the
|
||||
* second reading would find the step taken and refuse what the first allowed.
|
||||
* @param claimed whether the request named either MFA member at all
|
||||
* @param authenticatedAt when the device was used, or null when the claim failed
|
||||
* @param failure which half of the claim failed, null unless it did
|
||||
*/
|
||||
public record RequestMfaClaim(boolean claimed, Instant authenticatedAt) {
|
||||
public record RequestMfaClaim(boolean claimed, Instant authenticatedAt, Failure failure) {
|
||||
|
||||
/**
|
||||
* Which half of an MFA claim failed, because AWS answers the two with different sentences: a
|
||||
* serial that is not the caller's device is one refusal, a code that does not match it
|
||||
* another, and a caller cannot tell them apart from the code or the status, both being 403
|
||||
* {@code AccessDenied}.
|
||||
*/
|
||||
public enum Failure {
|
||||
|
||||
/** No device of the caller's answers to the serial the request named. */
|
||||
SERIAL,
|
||||
|
||||
/** The device answered, and the code was not one of its outputs or was already spent. */
|
||||
CODE
|
||||
}
|
||||
|
||||
/** A request that named neither member, which claims nothing and refuses nothing. */
|
||||
public static final RequestMfaClaim NONE = new RequestMfaClaim(false, null);
|
||||
public static final RequestMfaClaim NONE = new RequestMfaClaim(false, null, null);
|
||||
|
||||
/** A claim this emulator made good at {@code authenticatedAt}. */
|
||||
public static RequestMfaClaim authenticated(Instant authenticatedAt) {
|
||||
return new RequestMfaClaim(true, authenticatedAt, null);
|
||||
}
|
||||
|
||||
/** A claim naming a serial no device of the caller's answers to. */
|
||||
public static RequestMfaClaim unknownSerial() {
|
||||
return new RequestMfaClaim(true, null, Failure.SERIAL);
|
||||
}
|
||||
|
||||
/** A claim whose code the caller's device did not produce, or produced for a spent step. */
|
||||
public static RequestMfaClaim invalidCode() {
|
||||
return new RequestMfaClaim(true, null, Failure.CODE);
|
||||
}
|
||||
|
||||
/** A claim this emulator could not make good, which is the operation's to answer. */
|
||||
public boolean failed() {
|
||||
|
||||
@@ -4494,7 +4494,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
synchronized (mfaDeviceLock) {
|
||||
if (virtualMfaDevices.get(serialNumber).isPresent()) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
"MFADevice entity at the same path and name already exists.", 409);
|
||||
"MFA device already exists.", 409);
|
||||
}
|
||||
virtualMfaDevices.put(serialNumber, device);
|
||||
}
|
||||
@@ -4668,8 +4668,9 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
private void requireConsecutiveCodes(VirtualMfaDevice device, String code1, String code2, int driftSteps) {
|
||||
if (!VirtualMfaCodes.matchesConsecutiveCodes(device.getBase32Seed(), code1, code2,
|
||||
driftSteps, Instant.now())) {
|
||||
// Recorded on EnableMFADevice and ResyncMFADevice, 2026-11-05, wholly fixed text.
|
||||
throw new AwsException("InvalidAuthenticationCode",
|
||||
"Invalid MFA one time pass code.", 403);
|
||||
"Authentication code for device is not valid.", 403);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4722,27 +4723,31 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
return RequestMfaClaim.NONE;
|
||||
}
|
||||
if (serialNumber == null || tokenCode == null) {
|
||||
return new RequestMfaClaim(true, null);
|
||||
// Which half is missing decides the sentence: no code is a code that cannot validate,
|
||||
// and no serial is a device nothing can be looked up by.
|
||||
return tokenCode == null ? RequestMfaClaim.invalidCode() : RequestMfaClaim.unknownSerial();
|
||||
}
|
||||
Optional<String> userName = mfaUserName(accessKeyId);
|
||||
if (userName.isEmpty()) {
|
||||
return new RequestMfaClaim(true, holdsKey(accessKeyId) ? null : clock.instant());
|
||||
return holdsKey(accessKeyId)
|
||||
? RequestMfaClaim.unknownSerial()
|
||||
: RequestMfaClaim.authenticated(clock.instant());
|
||||
}
|
||||
synchronized (mfaDeviceLock) {
|
||||
Optional<VirtualMfaDevice> device = virtualMfaDevices.get(serialNumber)
|
||||
.filter(candidate -> userName.get().equals(candidate.getUserName()));
|
||||
if (device.isEmpty()) {
|
||||
return new RequestMfaClaim(true, null);
|
||||
return RequestMfaClaim.unknownSerial();
|
||||
}
|
||||
OptionalLong step = Totp.matchingStep(device.get().getBase32Seed(), tokenCode,
|
||||
ENABLE_DRIFT_STEPS, Instant.now());
|
||||
Long spent = device.get().getLastUsedTotpStep();
|
||||
if (step.isEmpty() || (spent != null && step.getAsLong() <= spent)) {
|
||||
return new RequestMfaClaim(true, null);
|
||||
return RequestMfaClaim.invalidCode();
|
||||
}
|
||||
device.get().setLastUsedTotpStep(step.getAsLong());
|
||||
virtualMfaDevices.put(serialNumber, device.get());
|
||||
return new RequestMfaClaim(true, clock.instant());
|
||||
return RequestMfaClaim.authenticated(clock.instant());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -242,8 +242,9 @@ public class StsQueryHandler {
|
||||
* GetSessionToken include MFA information, you can check for MFA in individual API operations
|
||||
* made by the credentials". A call that names either member is a claim of MFA and has to make
|
||||
* it good; what answers a claim that fails is the access denied AWS documents for the case,
|
||||
* "an incorrect MFA code causes the API to return an access denied error", carrying the
|
||||
* sentence the IAM actions already write for a code that does not match.
|
||||
* "an incorrect MFA code causes the API to return an access denied error", in the two
|
||||
* sentences {@link #mfaRefusal} carries, one for a serial that is not the caller's and one for
|
||||
* a code that does not match the device it names.
|
||||
*
|
||||
* <p>The claim is read through the request's own holder rather than taken here, because the
|
||||
* policy for this call was evaluated before the operation ran and had to see the same device,
|
||||
@@ -259,11 +260,29 @@ public class StsQueryHandler {
|
||||
iamService.requestMfaClaim(requestContext, callerAccessKeyId(), serialNumber, tokenCode);
|
||||
if (claim.failed()) {
|
||||
return new MfaOutcome(null, AwsQueryResponse.error("AccessDenied",
|
||||
"Invalid MFA one time pass code.", AwsNamespaces.STS, 403));
|
||||
mfaRefusal(claim.failure()), AwsNamespaces.STS, 403));
|
||||
}
|
||||
return new MfaOutcome(claim.authenticatedAt(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The sentence a failed MFA claim earns, which AWS words by which half of the claim failed.
|
||||
* Both are 403 {@code AccessDenied} with a sender fault, both are wholly fixed text, and
|
||||
* neither echoes the serial or the code: a caller is told which half to look at and nothing
|
||||
* about the values it sent. Recorded on {@code GetSessionToken} against a real account, the
|
||||
* serial half on 2026-10-29 and the code half on 2026-11-13, and each carries whitespace AWS
|
||||
* itself emits, two spaces inside the first and a trailing space on the second.
|
||||
*
|
||||
* <p>{@code AssumeRole} takes the same pair through this seam and carries no recording of its
|
||||
* own, so it answers the same two sentences, which is a reading rather than a measurement.
|
||||
*/
|
||||
private static String mfaRefusal(RequestMfaClaim.Failure failure) {
|
||||
return failure == RequestMfaClaim.Failure.SERIAL
|
||||
? "MultiFactorAuthentication failed, unable to validate MFA code. "
|
||||
+ "Please verify your MFA serial number is valid and associated with this user."
|
||||
: "MultiFactorAuthentication failed with invalid MFA one time pass code. ";
|
||||
}
|
||||
|
||||
/** The MFA a call authenticated, or the refusal it earned. */
|
||||
private record MfaOutcome(Instant authenticatedAt, Response denial) {
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user