mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudfront): hold a viewer certificate to the rules its entry states (#8028)
A distribution could name a certificate in ACMCertificateArn and in IAMCertificateId at once, and could name one in either of them without the MinimumProtocolVersion and SSLSupportMethod that go with it. It could also carry a security policy or a viewer support method outside the values those members admit, carry SSLv3 alongside sni-only, and claim CloudFrontDefaultCertificate while naming a certificate of its own. The ViewerCertificate entry states every one of those rules, and nothing here read any of them, so a configuration AWS refuses was taken and the distribution went on to serve it. They are read now on the distribution create and the distribution update, and the shape of the protocol members before any of it, so a request that breaks a rule is refused for the rule and not for a certificate nothing matches. A member carried blank counts as no value, the way a blank location member already names no certificate, and a distribution on the CloudFront domain name needs neither location member. Two of the refusals are published. A MinimumProtocolVersion outside its enumeration is InvalidMinimumProtocolVersion, whose own sentence is that the minimum protocol version specified is not valid. SSLv3 alongside sni-only is InvalidProtocolSettings, whose sentence names that pair and nothing else and which is the same rule as the floor the member states for SNI, "you must specify TLSv1 or higher", SSLv3 being the only value below TLSv1. That code is declared on CreateDistribution, CreateDistributionWithTags and CopyDistribution and has never been declared on UpdateDistribution, which has declared InvalidMinimumProtocolVersion since 2015, so the update answers the pair with the code it declares and keeps the pair's published sentence. The declared code is the one a caller's SDK can map, an undeclared one reaching it as a generic error, and the sentence keeps the two causes the update answers under that one code apart. The others are readings. An SSLSupportMethod outside its enumeration has no code of its own and answers InvalidArgument, what this service already answers a member outside its shape with. The three rules about the certificate itself answer InvalidViewerCertificate 400: InvalidArgument and InvalidMinimumProtocolVersion are modelled on both writes as well and no sentence says which one a caller gets, and the Developer Guide page that enumerates what InvalidViewerCertificate answers reaches none of those cases, naming the certificate's own trust, its chain, its format and the names it covers. That code's own documentation is that a viewer certificate specified is not valid, which is what each of the three carries. One rule in the group replaces a value instead of refusing the request. MinimumProtocolVersion's entry says that when CloudFrontDefaultCertificate is true CloudFront "automatically sets the security policy to TLSv1 regardless of the value that you set here", so the policy such a request asks for is overwritten and the config reads back TLSv1. A distribution created with no ViewerCertificate at all already read as the default certificate and reads TLSv1 with it now, where it had been answering TLSv1.2_2021. Two more readings go with the group. The claim itself is a member declared boolean, so it is held to the four forms XML gives a boolean, true, false, 1 and 0, rather than read for the word true alone: taken for false, a request saying yes would dodge every rule the member governs, the refusal beside a named certificate and the replaced policy alike. Anything else answers the InvalidArgument this service answers a member outside its shape with, CloudFront modelling no error for a malformed document. And the SSLv3 pair is read for a distribution on a certificate of its own alone, since on the CloudFront certificate the policy is replaced and the value that would make the pair is discarded, so refusing the request for it would contradict the override. Both enumerations are read either way. The us-east-1 restriction AWS states for ACM certificates is left unenforced, as is the /cloudfront upload path before it: refusing either here could meet a distribution somebody already built. All of the above, that included, is set out in docs/services/cloudfront.md.
This commit is contained in:
1 parent
a609eb2824
commit
9e6f62e959
5 files changed
+859
-10
No files matched your search
@@ -2253,6 +2253,14 @@ public class CloudFrontController {
|
||||
return xml.end("ActiveTrustedKeyGroups").build();
|
||||
}
|
||||
|
||||
/**
|
||||
* A config carrying no viewer certificate reads as the certificate CloudFront serves its own
|
||||
* domain name with, and the security policy that comes with it is
|
||||
* {@code TLSv1}: {@code MinimumProtocolVersion}'s entry says CloudFront "automatically sets the
|
||||
* security policy to TLSv1 regardless of the value that you set here" when
|
||||
* {@code CloudFrontDefaultCertificate} is true, and {@link CloudFrontService} stores that same
|
||||
* value for a request that sets the member itself.
|
||||
*/
|
||||
private String xmlViewerCertificate(Map<String, String> vc) {
|
||||
XmlBuilder xml = new XmlBuilder().start("ViewerCertificate");
|
||||
if (vc != null && !vc.isEmpty()) {
|
||||
@@ -2261,7 +2269,8 @@ public class CloudFrontController {
|
||||
}
|
||||
} else {
|
||||
xml.elem("CloudFrontDefaultCertificate", "true")
|
||||
.elem("MinimumProtocolVersion", "TLSv1.2_2021");
|
||||
.elem("MinimumProtocolVersion",
|
||||
CloudFrontService.DEFAULT_CERTIFICATE_PROTOCOL_VERSION);
|
||||
}
|
||||
xml.end("ViewerCertificate");
|
||||
return xml.build();
|
||||
|
||||
@@ -75,6 +75,30 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
|
||||
private static final List<String> OAC_SIGNING_BEHAVIORS =
|
||||
List.of("always-amz-auth", "never", "always", "no-override");
|
||||
private static final Set<String> FUNCTION_RUNTIMES = Set.of("cloudfront-js-1.0", "cloudfront-js-2.0");
|
||||
/**
|
||||
* The security policies {@code MinimumProtocolVersion} admits, the enumeration its shape
|
||||
* declares. {@code SSLv3} is the only one below {@code TLSv1}, which is why the floor the
|
||||
* member states for {@code sni-only} and the pair {@code InvalidProtocolSettings} names are
|
||||
* the same rule.
|
||||
*/
|
||||
private static final Set<String> MINIMUM_PROTOCOL_VERSIONS = Set.of("SSLv3", "TLSv1",
|
||||
"TLSv1_2016", "TLSv1.1_2016", "TLSv1.2_2018", "TLSv1.2_2019", "TLSv1.2_2021",
|
||||
"TLSv1.2_2025", "TLSv1.3_2025");
|
||||
/** The values {@code SSLSupportMethod} admits, the enumeration its shape declares. */
|
||||
private static final Set<String> SSL_SUPPORT_METHODS = Set.of("sni-only", "vip", "static-ip");
|
||||
/**
|
||||
* The forms a member declared {@code boolean} can carry, XML giving such a value as
|
||||
* {@code true}, {@code false}, {@code 1} or {@code 0}.
|
||||
*/
|
||||
private static final Map<String, Boolean> XML_BOOLEANS =
|
||||
Map.of("true", true, "1", true, "false", false, "0", false);
|
||||
/**
|
||||
* The security policy a distribution on the CloudFront domain name ends up on, whatever it
|
||||
* asked for: {@code MinimumProtocolVersion}'s own entry says that when
|
||||
* {@code CloudFrontDefaultCertificate} is true CloudFront "automatically sets the security
|
||||
* policy to TLSv1 regardless of the value that you set here".
|
||||
*/
|
||||
static final String DEFAULT_CERTIFICATE_PROTOCOL_VERSION = "TLSv1";
|
||||
private static final int MAX_ORIGIN_CUSTOM_HEADERS = 30;
|
||||
private static final int MAX_CUSTOM_HEADER_NAME_LENGTH = 256;
|
||||
private static final int MAX_CUSTOM_HEADER_VALUE_LENGTH = 1_783;
|
||||
@@ -213,6 +237,8 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
|
||||
validateResponseHeadersPolicyReferences(dist.getConfig(), null);
|
||||
validateTrustedKeyGroups(dist.getConfig());
|
||||
validateOriginAccessControls(dist.getConfig());
|
||||
validateViewerCertificateSettings(dist.getConfig(), DistributionWrite.CREATE);
|
||||
applyDefaultCertificateProtocolVersion(dist.getConfig());
|
||||
return whileCertificatesHeld(dist.getConfig(), () -> {
|
||||
validateViewerCertificate(dist.getConfig());
|
||||
String id = generateDistributionId();
|
||||
@@ -248,6 +274,8 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
|
||||
validateResponseHeadersPolicyReferences(updated.getConfig(), id);
|
||||
validateTrustedKeyGroups(updated.getConfig());
|
||||
validateOriginAccessControls(updated.getConfig());
|
||||
validateViewerCertificateSettings(updated.getConfig(), DistributionWrite.UPDATE);
|
||||
applyDefaultCertificateProtocolVersion(updated.getConfig());
|
||||
return whileCertificatesHeld(updated.getConfig(), () -> {
|
||||
validateViewerCertificate(updated.getConfig());
|
||||
updated.setId(id);
|
||||
@@ -291,11 +319,19 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
|
||||
}
|
||||
|
||||
private static String acmCertificateArn(DistributionConfig config) {
|
||||
return viewerCertificateMember(config, "ACMCertificateArn");
|
||||
}
|
||||
|
||||
/**
|
||||
* A member of the viewer certificate, or null when it is absent or blank. An element left empty
|
||||
* reaches this protocol as a blank value, so a member carried blank carries no value.
|
||||
*/
|
||||
private static String viewerCertificateMember(DistributionConfig config, String member) {
|
||||
if (config == null || config.getViewerCertificate() == null) {
|
||||
return null;
|
||||
}
|
||||
String certificateArn = config.getViewerCertificate().get("ACMCertificateArn");
|
||||
return certificateArn == null || certificateArn.isBlank() ? null : certificateArn;
|
||||
String value = config.getViewerCertificate().get(member);
|
||||
return value == null || value.isBlank() ? null : value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -306,11 +342,155 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
|
||||
* Server Certificates in {@code docs/services/iam.md}.
|
||||
*/
|
||||
private static String iamCertificateId(DistributionConfig config) {
|
||||
if (config == null || config.getViewerCertificate() == null) {
|
||||
return null;
|
||||
return viewerCertificateMember(config, "IAMCertificateId");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the request claims the certificate CloudFront serves its own domain name with. The
|
||||
* member is declared {@code boolean} in the model and reaches this protocol as text, so it is
|
||||
* held to the four forms XML gives a boolean, {@code true}, {@code false}, {@code 1} and
|
||||
* {@code 0}, rather than read for the word {@code true} alone. Anything else answers the
|
||||
* {@code InvalidArgument} this service answers a member outside its shape with, which is
|
||||
* Floci's reading: CloudFront models no error for a malformed document, and taking the value
|
||||
* for false instead would let a request dodge every rule this member governs, the refusals
|
||||
* beside a named certificate and the security policy it is overridden to alike. An absent
|
||||
* member, or one carried blank, leaves the distribution on a certificate of its own.
|
||||
*/
|
||||
private static boolean cloudFrontDefaultCertificate(DistributionConfig config) {
|
||||
String claimed = viewerCertificateMember(config, "CloudFrontDefaultCertificate");
|
||||
if (claimed == null) {
|
||||
return false;
|
||||
}
|
||||
String certificateId = config.getViewerCertificate().get("IAMCertificateId");
|
||||
return certificateId == null || certificateId.isBlank() ? null : certificateId;
|
||||
Boolean parsed = XML_BOOLEANS.get(claimed.strip().toLowerCase(Locale.ROOT));
|
||||
if (parsed == null) {
|
||||
throw new AwsException("InvalidArgument",
|
||||
"The CloudFrontDefaultCertificate specified is not valid.", 400);
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Which distribution write is being validated. The two differ in one answer, the code the
|
||||
* {@code SSLv3} with {@code sni-only} pair is refused with, which is the code the operation
|
||||
* declares: {@code CreateDistribution}, {@code CreateDistributionWithTags} and
|
||||
* {@code CopyDistribution} declare {@code InvalidProtocolSettings}, where
|
||||
* {@code UpdateDistribution} has never declared it in any version of this API and has declared
|
||||
* {@code InvalidMinimumProtocolVersion} since 2015. Where no sentence settles which a caller
|
||||
* gets, the declared code is what a typed SDK can map: an undeclared one reaches such a caller
|
||||
* as a generic error, its deserializer carrying no case for it.
|
||||
*/
|
||||
private enum DistributionWrite {
|
||||
CREATE("InvalidProtocolSettings"),
|
||||
UPDATE("InvalidMinimumProtocolVersion");
|
||||
|
||||
private final String protocolPairCode;
|
||||
|
||||
DistributionWrite(String protocolPairCode) {
|
||||
this.protocolPairCode = protocolPairCode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The two enumerations the viewer certificate declares, and the one pair it admits apart but
|
||||
* not together. A {@code MinimumProtocolVersion} outside its set is published:
|
||||
* {@code InvalidMinimumProtocolVersion}, "The minimum protocol version specified is not
|
||||
* valid.", is modelled on both distribution writes and its own sentence names this case. An
|
||||
* {@code SSLSupportMethod} outside its set has no code of its own, so it reads here as the
|
||||
* {@code InvalidArgument} this service already answers a member outside its shape with.
|
||||
* {@code SSLv3} alongside {@code sni-only} is published as well, by
|
||||
* {@code InvalidProtocolSettings}, whose sentence names that pair and nothing else, and it is
|
||||
* the same rule as the floor {@code MinimumProtocolVersion} states for SNI, "you must specify
|
||||
* TLSv1 or higher". That code is declared on the two creates and the copy and never on the
|
||||
* update, so the code this refusal answers is the write's own, as {@link DistributionWrite}
|
||||
* sets out. The sentence stays the pair's published one either way, which keeps the two causes
|
||||
* the update answers under one code apart. The pair is read for a distribution on a certificate
|
||||
* of its own alone: on the CloudFront certificate the security policy is replaced with
|
||||
* {@code TLSv1} "regardless of the value that you set here", so the value that would make the
|
||||
* pair is discarded and refusing the request for it would contradict the override. Both
|
||||
* enumerations are read either way, a member outside the set its shape declares being outside
|
||||
* its shape whatever the distribution is on.
|
||||
*/
|
||||
private static void validateViewerCertificateProtocols(DistributionConfig config,
|
||||
boolean defaultCertificate,
|
||||
DistributionWrite write) {
|
||||
String protocolVersion = viewerCertificateMember(config, "MinimumProtocolVersion");
|
||||
if (protocolVersion != null && !MINIMUM_PROTOCOL_VERSIONS.contains(protocolVersion)) {
|
||||
throw new AwsException("InvalidMinimumProtocolVersion",
|
||||
"The minimum protocol version specified is not valid.", 400);
|
||||
}
|
||||
String sslSupportMethod = viewerCertificateMember(config, "SSLSupportMethod");
|
||||
if (sslSupportMethod != null && !SSL_SUPPORT_METHODS.contains(sslSupportMethod)) {
|
||||
throw new AwsException("InvalidArgument",
|
||||
"The SSLSupportMethod specified is not valid.", 400);
|
||||
}
|
||||
if (!defaultCertificate && "SSLv3".equals(protocolVersion)
|
||||
&& "sni-only".equals(sslSupportMethod)) {
|
||||
throw new AwsException(write.protocolPairCode,
|
||||
"You cannot specify SSLv3 as the minimum protocol version if you only want to "
|
||||
+ "support only clients that support Server Name Indication (SNI).", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The rules the viewer certificate's own entry states about the certificate it names, and
|
||||
* nothing else here holds a caller to. Its location is set "in one of the following fields
|
||||
* (not both)", {@code ACMCertificateArn} or {@code IAMCertificateId}; a certificate named in
|
||||
* either one "must also specify values for MinimumProtocolVersion and SSLSupportMethod"; and a
|
||||
* request that sets {@code CloudFrontDefaultCertificate} is told to "leave all other fields
|
||||
* empty", so a certificate named beside it contradicts the request's own claim. None of the
|
||||
* three refusals has a published code: {@code InvalidArgument} and
|
||||
* {@code InvalidMinimumProtocolVersion} are modelled on the distribution writes as well and no
|
||||
* sentence says which a caller gets, and the Developer Guide page enumerating what
|
||||
* {@code InvalidViewerCertificate} answers reaches none of these cases. So all three read here
|
||||
* as the certificate the request carries not being valid, which is what
|
||||
* {@code InvalidViewerCertificate} is documented for. The readings are set out in
|
||||
* {@code docs/services/cloudfront.md}. The members are held to their shape first, the claim
|
||||
* itself and then the protocol members, and all of it runs before the certificate is looked
|
||||
* up, so a request breaking a rule is refused for the rule rather than for a certificate
|
||||
* nothing matches.
|
||||
*/
|
||||
private static void validateViewerCertificateSettings(DistributionConfig config,
|
||||
DistributionWrite write) {
|
||||
boolean defaultCertificate = cloudFrontDefaultCertificate(config);
|
||||
validateViewerCertificateProtocols(config, defaultCertificate, write);
|
||||
String certificateArn = acmCertificateArn(config);
|
||||
String certificateId = iamCertificateId(config);
|
||||
if (certificateArn != null && certificateId != null) {
|
||||
throw new AwsException("InvalidViewerCertificate",
|
||||
"You must specify a value for ACMCertificateArn or IAMCertificateId, not both.", 400);
|
||||
}
|
||||
if (certificateArn == null && certificateId == null) {
|
||||
return;
|
||||
}
|
||||
if (defaultCertificate) {
|
||||
throw new AwsException("InvalidViewerCertificate",
|
||||
"You cannot specify ACMCertificateArn or IAMCertificateId when "
|
||||
+ "CloudFrontDefaultCertificate is true.", 400);
|
||||
}
|
||||
if (viewerCertificateMember(config, "MinimumProtocolVersion") == null
|
||||
|| viewerCertificateMember(config, "SSLSupportMethod") == null) {
|
||||
throw new AwsException("InvalidViewerCertificate",
|
||||
"You must specify values for MinimumProtocolVersion and SSLSupportMethod.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The one rule in this group that replaces a value instead of refusing the request.
|
||||
* {@code MinimumProtocolVersion}'s entry says that when {@code CloudFrontDefaultCertificate} is
|
||||
* true CloudFront "automatically sets the security policy to TLSv1 regardless of the value that
|
||||
* you set here", so the policy a request asks for there is overwritten and the config reads
|
||||
* back the one the distribution is really on. The parsed map is replaced rather than written
|
||||
* to, a caller inside this JVM being free to hand in one that cannot be.
|
||||
*/
|
||||
private static void applyDefaultCertificateProtocolVersion(DistributionConfig config) {
|
||||
if (config == null || !cloudFrontDefaultCertificate(config)
|
||||
|| DEFAULT_CERTIFICATE_PROTOCOL_VERSION.equals(
|
||||
config.getViewerCertificate().get("MinimumProtocolVersion"))) {
|
||||
return;
|
||||
}
|
||||
Map<String, String> viewerCertificate = new LinkedHashMap<>(config.getViewerCertificate());
|
||||
viewerCertificate.put("MinimumProtocolVersion", DEFAULT_CERTIFICATE_PROTOCOL_VERSION);
|
||||
config.setViewerCertificate(viewerCertificate);
|
||||
}
|
||||
|
||||
private void validateViewerCertificate(DistributionConfig config) {
|
||||
|
||||
Reference in new issue
Block a user