|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__ . '/errors.php'; |
| 4 | + |
| 5 | +function handleGetObject($params) |
| 6 | +{ |
| 7 | + // Get ClientID from HTTP header |
| 8 | + $clientId = $_SERVER['HTTP_X_CLIENT_ID'] ?? $_SERVER['HTTP_CLIENTID'] ?? null; |
| 9 | + |
| 10 | + if (empty($clientId)) { |
| 11 | + return GenericServerError("ClientID header is required", 400); |
| 12 | + } |
| 13 | + |
| 14 | + # Get the S3EncryptionClient from the client_cache |
| 15 | + $s3ecClientTuple = getCachedClient($clientId); |
| 16 | + if ($s3ecClientTuple == null) { |
| 17 | + return GenericServerError("No client found for ClientID: " . $clientId, 404); |
| 18 | + } |
| 19 | + |
| 20 | + $metadata = $_SERVER['HTTP_CONTENT_METADATA'] ?? ''; |
| 21 | + $encryptionContext = metadataStringToMap($metadata); |
| 22 | + |
| 23 | + // Extract bucket and key from URL parameters |
| 24 | + $bucket = $params['bucket'] ?? null; |
| 25 | + $key = $params['key'] ?? null; |
| 26 | + |
| 27 | + if (is_null($bucket) || is_null($key)) { |
| 28 | + return GenericServerError("Invalidb bucket or key parameters", 400); |
| 29 | + } |
| 30 | + |
| 31 | + $s3ec = $s3ecClientTuple["encryptionClient"]; |
| 32 | + $materialProvider = $s3ecClientTuple["materialsProvider"]; |
| 33 | + $clientConfig = $s3ecClientTuple["config"]; |
| 34 | + $legacyConfig = $clientConfig["legacy"] ?? false; |
| 35 | + $legacy = null; |
| 36 | + if ($legacyConfig === false) { |
| 37 | + $legacy = "V2"; |
| 38 | + } else { |
| 39 | + $legacy = "V2_AND_LEGACY"; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + // Start output buffering before the AWS call to capture any unwanted output |
| 44 | + ob_start(); |
| 45 | + |
| 46 | + $result = $s3ec->getObject([ |
| 47 | + '@SecurityProfile' => $legacy, |
| 48 | + '@MaterialsProvider' => $materialProvider, |
| 49 | + '@KmsEncryptionContext' => $encryptionContext, |
| 50 | + 'Bucket' => $bucket, |
| 51 | + 'Key' => $key, |
| 52 | + ]); |
| 53 | + |
| 54 | + // Capture and discard any unwanted output from AWS SDK |
| 55 | + $unwantedOutput = ob_get_clean(); |
| 56 | + if (!empty($unwantedOutput)) { |
| 57 | + error_log("AWS SDK produced unexpected output: " . strlen($unwantedOutput) . " bytes"); |
| 58 | + } |
| 59 | + |
| 60 | + $body = $result['Body']->getContents(); |
| 61 | + $formattedMetadata = formatMetadataForResponse($result["Metadata"]); |
| 62 | + |
| 63 | + // Now set headers safely |
| 64 | + header("Content-Metadata: " . $formattedMetadata); |
| 65 | + header("Content-Type: application/octet-stream"); |
| 66 | + header("Content-Length: " . strlen($body)); |
| 67 | + return $body; |
| 68 | + } catch (InvalidArgumentException $e) { |
| 69 | + // Clean up output buffer if still active |
| 70 | + if (ob_get_level()) { |
| 71 | + ob_end_clean(); |
| 72 | + } |
| 73 | + return GenericServerError("Invalid argument: " . $e->getMessage(), 400); |
| 74 | + } catch (Exception $e) { |
| 75 | + // Clean up output buffer if still active |
| 76 | + if (ob_get_level()) { |
| 77 | + ob_end_clean(); |
| 78 | + } |
| 79 | + if (strpos($e->getMessage(), "@SecurityProfile=V2") !== false) { |
| 80 | + return S3EncryptionClientError($e->getMessage() . " " . "Enable legacy wrapping algorithms to use legacy key wrapping algorithm: kms"); |
| 81 | + } else { |
| 82 | + return GenericServerError("Server argument: " . $e->getMessage(), 500); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments