fix(lambda): delete only the named version when DeleteFunction has a Qualifier (#2824)

* fix(lambda): delete only the named version when DeleteFunction has a Qualifier

DeleteFunction accepted a Qualifier and dropped it: the controller never
bound the query parameter, so every call took the unqualified path and
removed the function, $LATEST and every other version. A caller pruning
one old version silently lost the whole function, with a success
response and no warning.

The qualified path now removes that version alone. Error behaviour
follows the live service rather than the reference, which documents none
of it: deleting $LATEST by qualifier and naming an alias are both
InvalidParameterValueException, a version an alias references is a
ResourceConflictException, and a version that does not exist is a silent
success rather than a 404.

Closes #2819

* test(lambda): cover qualified DeleteFunction through the AWS SDK

The integration test drives the endpoint over raw HTTP, so nothing
exercised the qualified delete through a real SDK client. Adds SDK
coverage for the four cases: deleting one version leaves the function
and its other versions, $LATEST cannot be deleted by qualifier, a
version an alias references is refused, and an absent version is a
no-op.

* fix(lambda): pattern-validate the DeleteFunction qualifier

Character.isDigit accepts non-ASCII decimal digits, so a full-width or
Arabic-Indic digit was read as a version number and answered with a
silent 204 instead of being rejected. The live service pattern-validates
the qualifier before anything else and its constraint is ASCII-only;
that check now runs first, and the digit test no longer relies on
Character.isDigit.
This commit is contained in:
Brian Taylor authored and GitHub committed 2026-08-31 22:01:34 -04:00
1 parent 7473c57720
commit ca4c84c9e0
5 files changed
+440 -2

No files matched your search

@@ -196,9 +196,10 @@ public class LambdaController {
@DELETE
@Path("/functions/{functionName}")
public Response deleteFunction(@Context HttpHeaders headers,
@PathParam("functionName") String functionName) {
@PathParam("functionName") String functionName,
@QueryParam("Qualifier") String qualifier) {
String region = regionResolver.resolveRegion(headers);
lambdaService.deleteFunction(region, functionName);
lambdaService.deleteFunction(region, functionName, qualifier);
return Response.noContent().build();
}
@@ -57,6 +57,14 @@ import java.util.regex.Pattern;
public class LambdaService implements ResourceProvider {
private static final Logger LOG = Logger.getLogger(LambdaService.class);
/**
* Qualifier constraint the live service enforces, taken from its own ValidationException
* rather than the API reference, which publishes a laxer pattern. Notably it is ASCII-only,
* so a non-ASCII digit is rejected here rather than being read as a version number.
*/
private static final String QUALIFIER_PATTERN = "\\$(LATEST(\\.PUBLISHED)?)|[a-zA-Z0-9-_$]+";
private static final Pattern QUALIFIER = Pattern.compile(QUALIFIER_PATTERN);
private static final Pattern EFS_ACCESS_POINT_ARN = Pattern.compile(
"^arn:aws[a-zA-Z-]*:elasticfilesystem:(?:eusc-)?[a-z]{2}"
+ "(?:(?:-gov)|(?:-iso(?:b)?))?-[a-z]+-\\d:"
@@ -706,7 +714,74 @@ public class LambdaService implements ResourceProvider {
return fn;
}
/**
* DeleteFunction without a Qualifier: removes the function and every version of it.
*/
public void deleteFunction(String region, String functionName) {
deleteFunction(region, functionName, null);
}
/**
* DeleteFunction. A Qualifier names a single published version to remove, leaving the
* function and its other versions in place; without one the whole function goes.
*/
public void deleteFunction(String region, String functionName, String qualifier) {
if (qualifier != null && !qualifier.isEmpty()) {
deleteFunctionVersion(region, functionName, qualifier);
return;
}
deleteWholeFunction(region, functionName);
}
/**
* Removes one published version. Error behaviour follows the live service: deleting
* {@code $LATEST} or naming an alias is rejected, a version an alias points at is a
* conflict, and a version that does not exist is a silent success rather than a 404.
*/
private void deleteFunctionVersion(String region, String functionName, String qualifier) {
if (!QUALIFIER.matcher(qualifier).matches()) {
throw new AwsException("ValidationException",
"1 validation error detected: Value '" + qualifier + "' at 'qualifier' failed"
+ " to satisfy constraint: Member must satisfy regular expression"
+ " pattern: " + QUALIFIER_PATTERN, 400);
}
LambdaFunction fn = getFunction(region, functionName);
if ("$LATEST".equals(qualifier)) {
throw new AwsException("InvalidParameterValueException",
"$LATEST version cannot be deleted without deleting the function.", 400);
}
if (!qualifier.chars().allMatch(c -> c >= '0' && c <= '9')) {
// A non-numeric qualifier names an alias, which the live service refuses to
// resolve here rather than deleting the version behind it.
throw new AwsException("InvalidParameterValueException",
"Deletion of aliases is not currently supported.", 400);
}
String name = fn.getFunctionName();
List<String> referencing = aliasStore == null ? List.of()
: aliasStore.list(region, name).stream()
.filter(alias -> qualifier.equals(alias.getFunctionVersion()))
.map(LambdaAlias::getName)
.toList();
if (!referencing.isEmpty()) {
throw new AwsException("ResourceConflictException",
"Unable to delete version because the following aliases reference it: "
+ referencing, 409);
}
Optional<LambdaFunction> version = functionStore.get(region, name, qualifier);
if (version.isEmpty()) {
return;
}
synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {
warmPool.drainEnvironment(version.get());
functionStore.deleteVersion(region, name, qualifier);
// The snapshot shares $LATEST's code directory, so this only reclaims once no
// remaining version references it.
reclaimLegacyCodeDirectoryIfUnused(name);
}
LOG.infov("Deleted version {0} of Lambda function: {1}", qualifier, name);
}
private void deleteWholeFunction(String region, String functionName) {
LambdaFunction fn = getFunction(region, functionName); // throws 404 if not found
functionName = fn.getFunctionName();
String arn = fn.getFunctionArn();