15
15
16
16
package software .amazon .awssdk .services .s3control .internal .interceptors ;
17
17
18
- import java .net .URI ;
18
+ import java .util .regex .Matcher ;
19
+ import java .util .regex .Pattern ;
20
+
19
21
import software .amazon .awssdk .annotations .SdkInternalApi ;
20
22
import software .amazon .awssdk .auth .signer .AwsSignerExecutionAttribute ;
21
23
import software .amazon .awssdk .core .exception .SdkClientException ;
32
34
*/
33
35
@ SdkInternalApi
34
36
public class EndpointAddressInterceptor implements ExecutionInterceptor {
37
+ private static final Pattern HOSTNAME_COMPLIANT_PATTERN = Pattern .compile ("[A-Za-z0-9\\ -]+" );
38
+ private static final int HOSTNAME_MAX_LENGTH = 63 ;
35
39
36
40
private static final String ENDPOINT_PREFIX = "s3-control" ;
37
41
@@ -47,7 +51,6 @@ public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context,
47
51
}
48
52
49
53
String accountId = request .headers ().get (X_AMZ_ACCOUNT_ID ).get (0 );
50
- URI endpoint = request .getUri ();
51
54
52
55
S3ControlConfiguration config = (S3ControlConfiguration ) executionAttributes .getAttribute (
53
56
AwsSignerExecutionAttribute .SERVICE_CONFIG );
@@ -78,6 +81,7 @@ private String resolveHost(SdkHttpRequest request, String accountId, S3ControlCo
78
81
host = host .replace (ENDPOINT_PREFIX , String .format ("%s-%s" , ENDPOINT_PREFIX , "fips" ));
79
82
80
83
}
84
+ validateComponentIsHostnameCompliant (accountId , "account id" );
81
85
return String .format ("%s.%s" , accountId , host );
82
86
}
83
87
@@ -88,4 +92,26 @@ private boolean isDualstackEnabled(S3ControlConfiguration configuration) {
88
92
private boolean isFipsEnabled (S3ControlConfiguration configuration ) {
89
93
return configuration != null && configuration .fipsModeEnabled ();
90
94
}
95
+
96
+ private static void validateComponentIsHostnameCompliant (String component , String componentName ) {
97
+ if (component .isEmpty ()) {
98
+ throw new IllegalArgumentException (
99
+ String .format ("An argument has been passed that is not valid: the required '%s' "
100
+ + "component is missing." , componentName ));
101
+ }
102
+
103
+ if (component .length () > HOSTNAME_MAX_LENGTH ) {
104
+ throw new IllegalArgumentException (
105
+ String .format ("An argument has been passed that is not valid: the '%s' "
106
+ + "component exceeds the maximum length of %d characters." , componentName ,
107
+ HOSTNAME_MAX_LENGTH ));
108
+ }
109
+
110
+ Matcher m = HOSTNAME_COMPLIANT_PATTERN .matcher (component );
111
+ if (!m .matches ()) {
112
+ throw new IllegalArgumentException (
113
+ String .format ("An argument has been passed that is not valid: the '%s' "
114
+ + "component must only contain alphanumeric characters and dashes." , componentName ));
115
+ }
116
+ }
91
117
}
0 commit comments