s3_validation_utils.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package s3api
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  6. )
  7. // isValidKMSKeyID performs basic validation of KMS key identifiers.
  8. // Following Minio's approach: be permissive and accept any reasonable key format.
  9. // Only reject keys with leading/trailing spaces or other obvious issues.
  10. //
  11. // This function is used across multiple S3 API handlers to ensure consistent
  12. // validation of KMS key IDs in various contexts (bucket encryption, object operations, etc.).
  13. func isValidKMSKeyID(keyID string) bool {
  14. // Reject empty keys
  15. if keyID == "" {
  16. return false
  17. }
  18. // Following Minio's validation: reject keys with leading/trailing spaces
  19. if strings.HasPrefix(keyID, " ") || strings.HasSuffix(keyID, " ") {
  20. return false
  21. }
  22. // Also reject keys with internal spaces (common sense validation)
  23. if strings.Contains(keyID, " ") {
  24. return false
  25. }
  26. // Reject keys with control characters or newlines
  27. if strings.ContainsAny(keyID, "\t\n\r\x00") {
  28. return false
  29. }
  30. // Accept any reasonable length key (be permissive for various KMS providers)
  31. if len(keyID) > 0 && len(keyID) <= s3_constants.MaxKMSKeyIDLength {
  32. return true
  33. }
  34. return false
  35. }
  36. // ValidateIV validates that an initialization vector has the correct length for AES encryption
  37. func ValidateIV(iv []byte, name string) error {
  38. if len(iv) != s3_constants.AESBlockSize {
  39. return fmt.Errorf("invalid %s length: expected %d bytes, got %d", name, s3_constants.AESBlockSize, len(iv))
  40. }
  41. return nil
  42. }
  43. // ValidateSSEKMSKey validates that an SSE-KMS key is not nil and has required fields
  44. func ValidateSSEKMSKey(sseKey *SSEKMSKey) error {
  45. if sseKey == nil {
  46. return fmt.Errorf("SSE-KMS key cannot be nil")
  47. }
  48. return nil
  49. }
  50. // ValidateSSECKey validates that an SSE-C key is not nil
  51. func ValidateSSECKey(customerKey *SSECustomerKey) error {
  52. if customerKey == nil {
  53. return fmt.Errorf("SSE-C customer key cannot be nil")
  54. }
  55. return nil
  56. }
  57. // ValidateSSES3Key validates that an SSE-S3 key is not nil
  58. func ValidateSSES3Key(sseKey *SSES3Key) error {
  59. if sseKey == nil {
  60. return fmt.Errorf("SSE-S3 key cannot be nil")
  61. }
  62. return nil
  63. }