fix(signin): complete aws login --remote against an endpoint override (#5552)

The AWS CLI builds the cross-device redirect from the Sign-In endpoint
it resolved, so with --endpoint-url it sends Floci's own address plus
/v1/sessions/confirmation. Floci accepted only an https AWS sign-in host
there and served no confirmation page, so the authorize request was
refused and the remote login could not finish.

A cross-device redirect is now also accepted on the scheme, host and
port the authorize request arrived on, and GET /v1/sessions/confirmation
shows the verification code in the form the CLI decodes. Any other
origin is still refused.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-12 23:09:28 -04:00
1 parent b4758b0018
commit bcda5958e0
6 files changed
+256 -6

No files matched your search

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.signin;
import io.github.hectorvent.floci.core.common.Base64Url;
import io.github.hectorvent.floci.core.common.Digests;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
@@ -10,10 +11,14 @@ import org.junit.jupiter.api.Test;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
@@ -27,6 +32,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class SigninIntegrationTest {
private static final String CLIENT_ID = "arn:aws:signin:::devtools/same-device";
private static final String CROSS_DEVICE_CLIENT_ID = "arn:aws:signin:::devtools/cross-device";
private static final String REDIRECT_URI = "http://127.0.0.1:4567/oauth/callback";
@Test
@@ -108,6 +114,89 @@ class SigninIntegrationTest {
assertEquals(state, params.get("state"));
}
@Test
void crossDeviceLoginShowsAVerificationCodeTheCliCanExchange() throws Exception {
String verifier = verifier();
String state = UUID.randomUUID().toString();
// What `aws login --remote --endpoint-url <floci>` sends: the endpoint it was given.
String redirectUri = "http://localhost:" + RestAssured.port + "/v1/sessions/confirmation";
String consentLocation = request()
.redirects().follow(false)
.queryParam("client_id", CROSS_DEVICE_CLIENT_ID)
.queryParam("code_challenge", challenge(verifier))
.queryParam("code_challenge_method", "SHA-256")
.queryParam("redirect_uri", redirectUri)
.queryParam("response_type", "code")
.queryParam("scope", "openid")
.queryParam("state", state)
.when()
.get("/v1/authorize")
.then()
.statusCode(302)
.extract()
.header("Location");
String confirmationLocation = approve(consentLocation);
assertTrue(confirmationLocation.startsWith(redirectUri + "?"), confirmationLocation);
String page = request()
.when()
.get(confirmationLocation)
.then()
.statusCode(200)
.header("Cache-Control", equalTo("no-store"))
.contentType(containsString("text/html"))
.extract()
.asString();
Matcher shown = Pattern.compile("<p class=\"verification-code\">([^<]+)</p>").matcher(page);
assertTrue(shown.find(), page);
Map<String, String> pasted = queryParams(
new String(Base64.getDecoder().decode(shown.group(1)), StandardCharsets.UTF_8));
assertEquals(state, pasted.get("state"));
request()
.contentType("application/json")
.body(Map.of(
"clientId", CROSS_DEVICE_CLIENT_ID,
"grantType", "authorization_code",
"code", pasted.get("code"),
"redirectUri", redirectUri,
"codeVerifier", verifier))
.when()
.post("/v1/token")
.then()
.statusCode(200)
.body("tokenType", equalTo("aws_sigv4"))
.body("accessToken.accessKeyId", notNullValue());
}
@Test
void crossDeviceLoginRefusesARedirectToAnotherOrigin() throws Exception {
request()
.redirects().follow(false)
.queryParam("client_id", CROSS_DEVICE_CLIENT_ID)
.queryParam("code_challenge", challenge(verifier()))
.queryParam("code_challenge_method", "SHA-256")
.queryParam("redirect_uri", "http://example.com:" + RestAssured.port + "/v1/sessions/confirmation")
.queryParam("response_type", "code")
.queryParam("scope", "openid")
.queryParam("state", "state")
.when()
.get("/v1/authorize")
.then()
.statusCode(400);
}
@Test
void confirmationPageWithoutACodeIsARequestError() {
request()
.when()
.get("/v1/sessions/confirmation")
.then()
.statusCode(400)
.contentType(containsString("text/html"));
}
@Test
void authorizeRejectsOauthS256Alias() throws Exception {
request()
@@ -10,10 +10,12 @@ import io.github.hectorvent.floci.testing.MutableClock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
@@ -114,6 +116,38 @@ class SigninServiceTest {
assertInvalidAuthorization(challenge(VERIFIER), CLIENT_ID, "x".repeat(2049), "state");
}
@Test
void crossDeviceRedirectIsAcceptedOnTheOriginTheRequestArrivedOn() throws Exception {
URI origin = URI.create("http://localhost:4566/");
crossDeviceAuthorize("http://localhost:4566/v1/sessions/confirmation", origin);
crossDeviceAuthorize("http://LOCALHOST:4566/v1/sessions/confirmation", origin);
crossDeviceAuthorize("https://floci.example/v1/sessions/confirmation", URI.create("https://floci.example:443/"));
crossDeviceAuthorize("https://us-east-1.signin.aws.amazon.com/v1/sessions/confirmation", origin);
assertInvalidRequest(() -> crossDeviceAuthorize("http://localhost:4567/v1/sessions/confirmation", origin));
assertInvalidRequest(() -> crossDeviceAuthorize("https://localhost:4566/v1/sessions/confirmation", origin));
assertInvalidRequest(() -> crossDeviceAuthorize("http://example.com:4566/v1/sessions/confirmation", origin));
assertInvalidRequest(() -> crossDeviceAuthorize("http://localhost:4566/v1/sessions/other", origin));
assertInvalidRequest(() -> crossDeviceAuthorize("http://localhost:4566/v1/sessions/confirmation?x=1", origin));
assertInvalidRequest(() -> crossDeviceAuthorize("http://user@localhost:4566/v1/sessions/confirmation", origin));
assertInvalidRequest(() -> crossDeviceAuthorize("http://localhost:4566/v1/sessions/confirmation", null));
assertInvalidRequest(() -> service.beginAuthorization(CLIENT_ID, challengeUnchecked(VERIFIER), "SHA-256",
"http://localhost:4566/v1/sessions/confirmation", "code", "openid", "state", null, origin));
}
@Test
void verificationCodeIsTheBase64QueryTheCliDecodes() {
String verificationCode = service.verificationCode("the code/+", "state-1");
String decoded = new String(Base64.getDecoder().decode(verificationCode), StandardCharsets.UTF_8);
assertEquals(Map.of("state", "state-1", "code", "the code/+"), query("https://x/?" + decoded));
assertInvalidRequest(() -> service.verificationCode(null, "state"));
assertInvalidRequest(() -> service.verificationCode("code", ""));
assertInvalidRequest(() -> service.verificationCode("c".repeat(513), "state"));
assertInvalidRequest(() -> service.verificationCode("code", "s".repeat(129)));
}
@Test
void validatesPkceVerifierBoundaries() throws Exception {
String minimumVerifier = "A".repeat(43);
@@ -402,6 +436,11 @@ class SigninServiceTest {
"code", "openid", state, null);
}
private String crossDeviceAuthorize(String redirectUri, URI requestOrigin) {
return service.beginAuthorization(SigninService.CROSS_DEVICE_CLIENT, challengeUnchecked(VERIFIER),
"SHA-256", redirectUri, "code", "openid", "state", null, requestOrigin);
}
private TokenResult exchangeCode(String code, String verifier) {
return service.exchange(CLIENT_ID, "authorization_code", code,
REDIRECT_URI, verifier, null, null);