Skip to content

Commit 76cae1f

Browse files
Merge pull request #1034 from humanmade/backport-1023-to-v22-branch
[Backport v22-branch] Fix metadata to check for IMDSV2
2 parents bc295ed + 45b24de commit 76cae1f

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

inc/namespace.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -857,14 +857,49 @@ function get_ec2_instance_metadata() : array {
857857
}
858858

859859
$client = new Client();
860+
$token = null;
860861

862+
// Only support IMDSv2: require token, no fallback.
861863
try {
862-
$request = $client->request( 'GET', 'http://169.254.169.254/latest/dynamic/instance-identity/document', [
863-
'timeout' => 1,
864-
'on_stats' => __NAMESPACE__ . '\\on_request_stats',
865-
] );
864+
$token_response = $client->request(
865+
'PUT',
866+
'http://169.254.169.254/latest/api/token',
867+
[
868+
'timeout' => 1,
869+
'headers' => [
870+
'X-aws-ec2-metadata-token-ttl-seconds' => '21600',
871+
],
872+
'on_stats' => __NAMESPACE__ . '\\on_request_stats',
873+
]
874+
);
875+
if ( $token_response->getStatusCode() === 200 ) {
876+
$token = (string) $token_response->getBody();
877+
} else {
878+
trigger_error( 'IMDSv2 token request failed', E_USER_NOTICE );
879+
if ( function_exists( 'apcu_store' ) ) {
880+
apcu_store( $cache_key, [] );
881+
}
882+
return [];
883+
}
884+
} catch ( Exception $e ) {
885+
trigger_error( 'IMDSv2 token required but could not be retrieved: ' . $e->getMessage(), E_USER_NOTICE );
886+
if ( function_exists( 'apcu_store' ) ) {
887+
apcu_store( $cache_key, [] );
888+
}
889+
return [];
890+
}
891+
892+
try {
893+
$request = $client->request(
894+
'GET',
895+
'http://169.254.169.254/latest/dynamic/instance-identity/document',
896+
[
897+
'timeout' => 1,
898+
'headers' => [ 'X-aws-ec2-metadata-token' => $token ],
899+
'on_stats' => __NAMESPACE__ . '\\on_request_stats',
900+
]
901+
);
866902
} catch ( Exception $e ) {
867-
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
868903
trigger_error( sprintf( 'Unable to get instance metadata. Error: %s', $e->getMessage() ), E_USER_NOTICE );
869904
if ( function_exists( 'apcu_store' ) ) {
870905
apcu_store( $cache_key, [] );
@@ -873,7 +908,6 @@ function get_ec2_instance_metadata() : array {
873908
}
874909

875910
if ( $request->getStatusCode() !== 200 ) {
876-
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
877911
trigger_error( sprintf( 'Unable to get instance metadata. Returned response code: %s', $request->getStatusCode() ), E_USER_NOTICE );
878912
if ( function_exists( 'apcu_store' ) ) {
879913
apcu_store( $cache_key, [] );
@@ -882,15 +916,12 @@ function get_ec2_instance_metadata() : array {
882916
}
883917

884918
$metadata = json_decode( $request->getBody(), true );
885-
886919
if ( ! $metadata ) {
887920
$metadata = [];
888921
}
889-
890922
if ( function_exists( 'apcu_store' ) ) {
891923
apcu_store( $cache_key, $metadata );
892924
}
893-
894925
return $metadata;
895926
}
896927

0 commit comments

Comments
 (0)