s3api_object_handlers_postpolicy.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package s3api
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "mime/multipart"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "github.com/dustin/go-humanize"
  13. "github.com/gorilla/mux"
  14. "github.com/seaweedfs/seaweedfs/weed/glog"
  15. "github.com/seaweedfs/seaweedfs/weed/s3api/policy"
  16. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  17. )
  18. func (s3a *S3ApiServer) PostPolicyBucketHandler(w http.ResponseWriter, r *http.Request) {
  19. // https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html
  20. // https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html
  21. bucket := mux.Vars(r)["bucket"]
  22. glog.V(3).Infof("PostPolicyBucketHandler %s", bucket)
  23. reader, err := r.MultipartReader()
  24. if err != nil {
  25. s3err.WriteErrorResponse(w, r, s3err.ErrMalformedPOSTRequest)
  26. return
  27. }
  28. form, err := reader.ReadForm(int64(5 * humanize.MiByte))
  29. if err != nil {
  30. s3err.WriteErrorResponse(w, r, s3err.ErrMalformedPOSTRequest)
  31. return
  32. }
  33. defer form.RemoveAll()
  34. fileBody, fileName, fileSize, formValues, err := extractPostPolicyFormValues(form)
  35. if err != nil {
  36. s3err.WriteErrorResponse(w, r, s3err.ErrMalformedPOSTRequest)
  37. return
  38. }
  39. if fileBody == nil {
  40. s3err.WriteErrorResponse(w, r, s3err.ErrPOSTFileRequired)
  41. return
  42. }
  43. defer fileBody.Close()
  44. formValues.Set("Bucket", bucket)
  45. if fileName != "" && strings.Contains(formValues.Get("Key"), "${filename}") {
  46. formValues.Set("Key", strings.Replace(formValues.Get("Key"), "${filename}", fileName, -1))
  47. }
  48. object := formValues.Get("Key")
  49. successRedirect := formValues.Get("success_action_redirect")
  50. successStatus := formValues.Get("success_action_status")
  51. var redirectURL *url.URL
  52. if successRedirect != "" {
  53. redirectURL, err = url.Parse(successRedirect)
  54. if err != nil {
  55. s3err.WriteErrorResponse(w, r, s3err.ErrMalformedPOSTRequest)
  56. return
  57. }
  58. }
  59. // Verify policy signature.
  60. errCode := s3a.iam.doesPolicySignatureMatch(formValues)
  61. if errCode != s3err.ErrNone {
  62. s3err.WriteErrorResponse(w, r, errCode)
  63. return
  64. }
  65. policyBytes, err := base64.StdEncoding.DecodeString(formValues.Get("Policy"))
  66. if err != nil {
  67. s3err.WriteErrorResponse(w, r, s3err.ErrMalformedPOSTRequest)
  68. return
  69. }
  70. // Handle policy if it is set.
  71. if len(policyBytes) > 0 {
  72. postPolicyForm, err := policy.ParsePostPolicyForm(string(policyBytes))
  73. if err != nil {
  74. s3err.WriteErrorResponse(w, r, s3err.ErrPostPolicyConditionInvalidFormat)
  75. return
  76. }
  77. // Make sure formValues adhere to policy restrictions.
  78. if err = policy.CheckPostPolicy(formValues, postPolicyForm); err != nil {
  79. w.Header().Set("Location", r.URL.Path)
  80. w.WriteHeader(http.StatusTemporaryRedirect)
  81. return
  82. }
  83. // Ensure that the object size is within expected range, also the file size
  84. // should not exceed the maximum single Put size (5 GiB)
  85. lengthRange := postPolicyForm.Conditions.ContentLengthRange
  86. if lengthRange.Valid {
  87. if fileSize < lengthRange.Min {
  88. s3err.WriteErrorResponse(w, r, s3err.ErrEntityTooSmall)
  89. return
  90. }
  91. if fileSize > lengthRange.Max {
  92. s3err.WriteErrorResponse(w, r, s3err.ErrEntityTooLarge)
  93. return
  94. }
  95. }
  96. }
  97. uploadUrl := fmt.Sprintf("http://%s%s/%s%s", s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, bucket, urlEscapeObject(object))
  98. etag, errCode := s3a.putToFiler(r, uploadUrl, fileBody, "")
  99. if errCode != s3err.ErrNone {
  100. s3err.WriteErrorResponse(w, r, errCode)
  101. return
  102. }
  103. if successRedirect != "" {
  104. // Replace raw query params..
  105. redirectURL.RawQuery = getRedirectPostRawQuery(bucket, object, etag)
  106. w.Header().Set("Location", redirectURL.String())
  107. s3err.WriteEmptyResponse(w, r, http.StatusSeeOther)
  108. return
  109. }
  110. setEtag(w, etag)
  111. // Decide what http response to send depending on success_action_status parameter
  112. switch successStatus {
  113. case "201":
  114. resp := PostResponse{
  115. Bucket: bucket,
  116. Key: object,
  117. ETag: `"` + etag + `"`,
  118. Location: w.Header().Get("Location"),
  119. }
  120. s3err.WriteXMLResponse(w, r, http.StatusCreated, resp)
  121. s3err.PostLog(r, http.StatusCreated, s3err.ErrNone)
  122. case "200":
  123. s3err.WriteEmptyResponse(w, r, http.StatusOK)
  124. default:
  125. writeSuccessResponseEmpty(w, r)
  126. }
  127. }
  128. // Extract form fields and file data from a HTTP POST Policy
  129. func extractPostPolicyFormValues(form *multipart.Form) (filePart io.ReadCloser, fileName string, fileSize int64, formValues http.Header, err error) {
  130. // / HTML Form values
  131. fileName = ""
  132. // Canonicalize the form values into http.Header.
  133. formValues = make(http.Header)
  134. for k, v := range form.Value {
  135. formValues[http.CanonicalHeaderKey(k)] = v
  136. }
  137. // Validate form values.
  138. if err = validateFormFieldSize(formValues); err != nil {
  139. return nil, "", 0, nil, err
  140. }
  141. // this means that filename="" was not specified for file key and Go has
  142. // an ugly way of handling this situation. Refer here
  143. // https://golang.org/src/mime/multipart/formdata.go#L61
  144. if len(form.File) == 0 {
  145. var b = &bytes.Buffer{}
  146. for _, v := range formValues["File"] {
  147. b.WriteString(v)
  148. }
  149. fileSize = int64(b.Len())
  150. filePart = io.NopCloser(b)
  151. return filePart, fileName, fileSize, formValues, nil
  152. }
  153. // Iterator until we find a valid File field and break
  154. for k, v := range form.File {
  155. canonicalFormName := http.CanonicalHeaderKey(k)
  156. if canonicalFormName == "File" {
  157. if len(v) == 0 {
  158. return nil, "", 0, nil, errors.New("Invalid arguments specified")
  159. }
  160. // Fetch fileHeader which has the uploaded file information
  161. fileHeader := v[0]
  162. // Set filename
  163. fileName = fileHeader.Filename
  164. // Open the uploaded part
  165. filePart, err = fileHeader.Open()
  166. if err != nil {
  167. return nil, "", 0, nil, err
  168. }
  169. // Compute file size
  170. fileSize, err = filePart.(io.Seeker).Seek(0, 2)
  171. if err != nil {
  172. return nil, "", 0, nil, err
  173. }
  174. // Reset Seek to the beginning
  175. _, err = filePart.(io.Seeker).Seek(0, 0)
  176. if err != nil {
  177. return nil, "", 0, nil, err
  178. }
  179. // File found and ready for reading
  180. break
  181. }
  182. }
  183. return filePart, fileName, fileSize, formValues, nil
  184. }
  185. // Validate form field size for s3 specification requirement.
  186. func validateFormFieldSize(formValues http.Header) error {
  187. // Iterate over form values
  188. for k := range formValues {
  189. // Check if value's field exceeds S3 limit
  190. if int64(len(formValues.Get(k))) > int64(1*humanize.MiByte) {
  191. return errors.New("Data size larger than expected")
  192. }
  193. }
  194. // Success.
  195. return nil
  196. }
  197. func getRedirectPostRawQuery(bucket, key, etag string) string {
  198. redirectValues := make(url.Values)
  199. redirectValues.Set("bucket", bucket)
  200. redirectValues.Set("key", key)
  201. redirectValues.Set("etag", "\""+etag+"\"")
  202. return redirectValues.Encode()
  203. }
  204. // Check to see if Policy is signed correctly.
  205. func (iam *IdentityAccessManagement) doesPolicySignatureMatch(formValues http.Header) s3err.ErrorCode {
  206. // For SignV2 - Signature field will be valid
  207. if _, ok := formValues["Signature"]; ok {
  208. return iam.doesPolicySignatureV2Match(formValues)
  209. }
  210. return iam.doesPolicySignatureV4Match(formValues)
  211. }