diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java index 97fd19f0ab..021a298df8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java @@ -740,6 +740,8 @@ public class LambdaService implements ResourceProvider { String s3Bucket = (String) request.get("S3Bucket"); String s3Key = (String) request.get("S3Key"); + requireMatchingRevisionId(fn, request); + if (zipFileBase64 != null) { fn.setS3Bucket(null); fn.setS3Key(null); @@ -774,6 +776,22 @@ public class LambdaService implements ResourceProvider { return fn; } + /** + * RevisionId optimistic locking: rejects the request with 412 when it carries a RevisionId that + * is not the function's current one. Callers hold the per-function lock and call this before + * mutating {@code fn}. + */ + private static void requireMatchingRevisionId(LambdaFunction fn, Map request) { + if (request.containsKey("RevisionId")) { + String incomingRevision = (String) request.get("RevisionId"); + if (incomingRevision != null && !incomingRevision.equals(fn.getRevisionId())) { + throw new AwsException("PreconditionFailedException", + "The Revision Id provided does not match the latest Revision Id. " + + "Call the GetFunction/GetAlias API to retrieve the latest Revision Id", 412); + } + } + } + public LambdaFunction updateFunctionConfiguration(String region, String functionName, Map request) { // Shape-checked ahead of the lookup, which is where #3051 put these: a malformed member on // a function that does not exist reports SerializationException, not 404. Splitting the @@ -863,16 +881,8 @@ public class LambdaService implements ResourceProvider { validateFileSystemVpcConfig(requestedFileSystemConfigs, requestedVpcConfig); } - // RevisionId optimistic locking runs after request validation, like AWS, and before any field mutation - if (request.containsKey("RevisionId")) { - String incomingRevision = (String) request.get("RevisionId"); - if (incomingRevision != null && !incomingRevision.equals(fn.getRevisionId())) { - throw new AwsException("PreconditionFailedException", - "The Revision Id provided does not match the latest Revision Id. " - + "Call the GetFunction or the GetFunctionConfiguration API to retrieve " - + "the latest Revision Id for your resource.", 412); - } - } + // Runs after request validation, like AWS, and before any field mutation + requireMatchingRevisionId(fn, request); if (request.containsKey("Description")) { fn.setDescription((String) request.get("Description")); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java index 21b1bb5b80..35d7194c1e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java @@ -2323,4 +2323,37 @@ class LambdaServiceTest { assertEquals("ValidationException", thrown.getErrorCode()); assertEquals(400, thrown.getHttpStatus()); } + + @Test + void updateFunctionCode_withMismatchedRevisionId_throwsPreconditionFailedException() throws Exception { + // Catches: UpdateFunctionCode silently accepts a stale RevisionId and overwrites the function's code + // (the CodeSha256 assertion fails if the check ever runs after code extraction) + Map createReq = baseRequest("revision-lock-func"); + service.createFunction(REGION, createReq); + + LambdaFunction fn = service.getFunction(REGION, "revision-lock-func"); + String initialRevisionId = fn.getRevisionId(); + String initialCodeSha256 = fn.getCodeSha256(); + String mismatchingRevision = "stale-revision-id-abc123"; + String zipBase64 = createZipBase64("index.js"); + + Map updateReq = new HashMap<>(); + updateReq.put("ZipFile", zipBase64); + updateReq.put("RevisionId", mismatchingRevision); + + AwsException ex = assertThrows(AwsException.class, () -> service.updateFunctionCode(REGION, "revision-lock-func", updateReq)); + assertEquals("PreconditionFailedException", ex.getErrorCode()); + assertEquals(412, ex.getHttpStatus()); + assertEquals("The Revision Id provided does not match the latest Revision Id. Call the GetFunction/GetAlias API to retrieve the latest Revision Id", ex.getMessage()); + + LambdaFunction unchangedFn = service.getFunction(REGION, "revision-lock-func"); + assertEquals(initialRevisionId, unchangedFn.getRevisionId()); + assertEquals(initialCodeSha256, unchangedFn.getCodeSha256()); + + updateReq.put("RevisionId", initialRevisionId); + service.updateFunctionCode(REGION, "revision-lock-func", updateReq); + + LambdaFunction updatedFn = service.getFunction(REGION, "revision-lock-func"); + assertNotEquals(initialRevisionId, updatedFn.getRevisionId()); + } }