mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(lambda): add cookies array to function URL events (#5288)
* Fix #5255 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
985921c3db
commit
673c809eff
5 files changed
+127
-20
No files matched your search
@@ -0,0 +1,41 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Reads the {@code cookies} array of a payload format 2.0 Lambda event, which HTTP API
|
||||
* integrations, HTTP API Lambda authorizers and function URLs all send, from the request's
|
||||
* {@code Cookie} headers.
|
||||
*/
|
||||
public final class CookieHeaders {
|
||||
|
||||
private CookieHeaders() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Every cookie-pair from every {@code Cookie} header, in request order. The header name
|
||||
* matches case-insensitively, each value is split on {@code ;}, pairs are trimmed and empty
|
||||
* segments are dropped.
|
||||
*/
|
||||
public static List<String> cookiePairs(Map<String, List<String>> requestHeaders) {
|
||||
List<String> cookies = new ArrayList<>();
|
||||
for (Map.Entry<String, List<String>> e : requestHeaders.entrySet()) {
|
||||
if (!HttpHeaders.COOKIE.equalsIgnoreCase(e.getKey())) {
|
||||
continue;
|
||||
}
|
||||
for (String header : e.getValue()) {
|
||||
for (String cookie : header.split(";")) {
|
||||
String trimmed = cookie.trim();
|
||||
if (!trimmed.isEmpty()) {
|
||||
cookies.add(trimmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return cookies;
|
||||
}
|
||||
}
|
||||
+2
-20
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsEndpoints;
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.CookieHeaders;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4AuthorizationHeader;
|
||||
@@ -3551,7 +3552,7 @@ public class ApiGatewayExecuteController {
|
||||
private static void putV2CookiesHeadersAndQuery(ObjectNode event,
|
||||
MultivaluedMap<String, String> requestHeaders,
|
||||
MultivaluedMap<String, String> queryParams) {
|
||||
List<String> cookies = v2Cookies(requestHeaders);
|
||||
List<String> cookies = CookieHeaders.cookiePairs(requestHeaders);
|
||||
if (!cookies.isEmpty()) {
|
||||
ArrayNode cookiesNode = event.putArray("cookies");
|
||||
cookies.forEach(cookiesNode::add);
|
||||
@@ -3574,25 +3575,6 @@ public class ApiGatewayExecuteController {
|
||||
}
|
||||
}
|
||||
|
||||
/** Every cookie-pair from every {@code Cookie} header, in request order. */
|
||||
private static List<String> v2Cookies(MultivaluedMap<String, String> requestHeaders) {
|
||||
List<String> cookies = new ArrayList<>();
|
||||
for (Map.Entry<String, List<String>> e : requestHeaders.entrySet()) {
|
||||
if (!HttpHeaders.COOKIE.equalsIgnoreCase(e.getKey())) {
|
||||
continue;
|
||||
}
|
||||
for (String header : e.getValue()) {
|
||||
for (String cookie : header.split(";")) {
|
||||
String trimmed = cookie.trim();
|
||||
if (!trimmed.isEmpty()) {
|
||||
cookies.add(trimmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return cookies;
|
||||
}
|
||||
|
||||
private static boolean isV2TextContentType(String contentType) {
|
||||
if (contentType == null || contentType.isBlank()) {
|
||||
return false;
|
||||
|
||||
+9
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.CookieHeaders;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
@@ -10,6 +11,7 @@ import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
@@ -33,6 +35,7 @@ import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -147,6 +150,12 @@ public class LambdaUrlInvocationController {
|
||||
root.put("rawPath", rawPath);
|
||||
root.put("rawQueryString", uriInfo.getRequestUri().getRawQuery() != null ? uriInfo.getRequestUri().getRawQuery() : "");
|
||||
|
||||
List<String> cookies = CookieHeaders.cookiePairs(headers.getRequestHeaders());
|
||||
if (!cookies.isEmpty()) {
|
||||
ArrayNode cookiesNode = root.putArray("cookies");
|
||||
cookies.forEach(cookiesNode::add);
|
||||
}
|
||||
|
||||
ObjectNode headersNode = root.putObject("headers");
|
||||
headers.getRequestHeaders().forEach((k, v) -> headersNode.put(k.toLowerCase(), String.join(",", v)));
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class CookieHeadersTest {
|
||||
|
||||
@Test
|
||||
void splitsEveryCookieHeaderInRequestOrder() {
|
||||
Map<String, List<String>> headers = new LinkedHashMap<>();
|
||||
headers.put("Cookie", List.of("a=1; b=2;; ", " c=3 "));
|
||||
headers.put("x-other", List.of("v"));
|
||||
headers.put("cookie", List.of("d=4"));
|
||||
|
||||
assertEquals(List.of("a=1", "b=2", "c=3", "d=4"), CookieHeaders.cookiePairs(headers));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noCookieHeaderGivesNoPairs() {
|
||||
assertTrue(CookieHeaders.cookiePairs(Map.of("x-other", List.of("v"))).isEmpty());
|
||||
assertTrue(CookieHeaders.cookiePairs(Map.of("Cookie", List.of(" ; "))).isEmpty());
|
||||
}
|
||||
}
|
||||
+46
@@ -301,6 +301,52 @@ class LambdaUrlInvocationControllerTest {
|
||||
assertNull(response.getEntity());
|
||||
}
|
||||
|
||||
private JsonNode eventFor(HttpHeaders headers) {
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("my-function");
|
||||
fn.setFunctionArn(FUNCTION_ARN);
|
||||
fn.setAccountId("100000000012");
|
||||
|
||||
LambdaService lambdaService = mock(LambdaService.class);
|
||||
when(lambdaService.getTargetByUrlId("url-id")).thenReturn(fn);
|
||||
InvokeResult invokeResult = new InvokeResult();
|
||||
invokeResult.setStatusCode(200);
|
||||
invokeResult.setPayload("{\"statusCode\":200}".getBytes(StandardCharsets.UTF_8));
|
||||
when(lambdaService.invokeArn(eq(FUNCTION_ARN), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(invokeResult);
|
||||
|
||||
newController(lambdaService).handleGet("url-id", "", headers,
|
||||
uriInfoFor("http://localhost/lambda-url/url-id/"));
|
||||
|
||||
ArgumentCaptor<byte[]> event = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(lambdaService).invokeArn(eq(FUNCTION_ARN), event.capture(), eq(InvocationType.RequestResponse));
|
||||
return readTree(event.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cookieHeadersBecomeCookiesArray() {
|
||||
HttpHeaders headers = headersWith(null);
|
||||
headers.getRequestHeaders().add("Cookie", "a=1; b=2;; ");
|
||||
headers.getRequestHeaders().add("cookie", " c=3 ");
|
||||
headers.getRequestHeaders().putSingle("x-other", "v");
|
||||
|
||||
JsonNode eventNode = eventFor(headers);
|
||||
|
||||
assertEquals(3, eventNode.get("cookies").size());
|
||||
assertEquals("a=1", eventNode.get("cookies").get(0).asText());
|
||||
assertEquals("b=2", eventNode.get("cookies").get(1).asText());
|
||||
assertEquals("c=3", eventNode.get("cookies").get(2).asText());
|
||||
assertTrue(eventNode.get("headers").has("cookie"));
|
||||
assertEquals("v", eventNode.get("headers").get("x-other").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestWithoutCookiesOmitsCookiesField() {
|
||||
JsonNode eventNode = eventFor(headersWith(null));
|
||||
|
||||
assertFalse(eventNode.has("cookies"));
|
||||
}
|
||||
|
||||
private JsonNode readTree(byte[] json) {
|
||||
try {
|
||||
return new ObjectMapper().readTree(json);
|
||||
|
||||
Reference in new issue
Block a user