2021-07-27 18:42:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ExternalUploadManager
|
|
|
|
DOWNLOAD_LIMIT = 100.megabytes
|
FEATURE: Uppy direct S3 multipart uploads in composer (#14051)
This pull request introduces the endpoints required, and the JavaScript functionality in the `ComposerUppyUpload` mixin, for direct S3 multipart uploads. There are four new endpoints in the uploads controller:
* `create-multipart.json` - Creates the multipart upload in S3 along with an `ExternalUploadStub` record, storing information about the file in the same way as `generate-presigned-put.json` does for regular direct S3 uploads
* `batch-presign-multipart-parts.json` - Takes a list of part numbers and the unique identifier for an `ExternalUploadStub` record, and generates the presigned URLs for those parts if the multipart upload still exists and if the user has permission to access that upload
* `complete-multipart.json` - Completes the multipart upload in S3. Needs the full list of part numbers and their associated ETags which are returned when the part is uploaded to the presigned URL above. Only works if the user has permission to access the associated `ExternalUploadStub` record and the multipart upload still exists.
After we confirm the upload is complete in S3, we go through the regular `UploadCreator` flow, the same as `complete-external-upload.json`, and promote the temporary upload S3 into a full `Upload` record, moving it to its final destination.
* `abort-multipart.json` - Aborts the multipart upload on S3 and destroys the `ExternalUploadStub` record if the user has permission to access that upload.
Also added are a few new columns to `ExternalUploadStub`:
* multipart - Whether or not this is a multipart upload
* external_upload_identifier - The "upload ID" for an S3 multipart upload
* filesize - The size of the file when the `create-multipart.json` or `generate-presigned-put.json` is called. This is used for validation.
When the user completes a direct S3 upload, either regular or multipart, we take the `filesize` that was captured when the `ExternalUploadStub` was first created and compare it with the final `Content-Length` size of the file where it is stored in S3. Then, if the two do not match, we throw an error, delete the file on S3, and ban the user from uploading files for N (default 5) minutes. This would only happen if the user uploads a different file than what they first specified, or in the case of multipart uploads uploaded larger chunks than needed. This is done to prevent abuse of S3 storage by bad actors.
Also included in this PR is an update to vendor/uppy.js. This has been built locally from the latest uppy source at https://github.com/transloadit/uppy/commit/d613b849a6591083f8a0968aa8d66537e231bbcd. This must be done so that I can get my multipart upload changes into Discourse. When the Uppy team cuts a proper release, we can bump the package.json versions instead.
2021-08-24 18:46:54 -04:00
|
|
|
SIZE_MISMATCH_BAN_MINUTES = 5
|
|
|
|
BAN_USER_REDIS_PREFIX = "ban_user_from_external_uploads_"
|
2021-07-27 18:42:25 -04:00
|
|
|
|
|
|
|
class ChecksumMismatchError < StandardError; end
|
|
|
|
class DownloadFailedError < StandardError; end
|
|
|
|
class CannotPromoteError < StandardError; end
|
FEATURE: Uppy direct S3 multipart uploads in composer (#14051)
This pull request introduces the endpoints required, and the JavaScript functionality in the `ComposerUppyUpload` mixin, for direct S3 multipart uploads. There are four new endpoints in the uploads controller:
* `create-multipart.json` - Creates the multipart upload in S3 along with an `ExternalUploadStub` record, storing information about the file in the same way as `generate-presigned-put.json` does for regular direct S3 uploads
* `batch-presign-multipart-parts.json` - Takes a list of part numbers and the unique identifier for an `ExternalUploadStub` record, and generates the presigned URLs for those parts if the multipart upload still exists and if the user has permission to access that upload
* `complete-multipart.json` - Completes the multipart upload in S3. Needs the full list of part numbers and their associated ETags which are returned when the part is uploaded to the presigned URL above. Only works if the user has permission to access the associated `ExternalUploadStub` record and the multipart upload still exists.
After we confirm the upload is complete in S3, we go through the regular `UploadCreator` flow, the same as `complete-external-upload.json`, and promote the temporary upload S3 into a full `Upload` record, moving it to its final destination.
* `abort-multipart.json` - Aborts the multipart upload on S3 and destroys the `ExternalUploadStub` record if the user has permission to access that upload.
Also added are a few new columns to `ExternalUploadStub`:
* multipart - Whether or not this is a multipart upload
* external_upload_identifier - The "upload ID" for an S3 multipart upload
* filesize - The size of the file when the `create-multipart.json` or `generate-presigned-put.json` is called. This is used for validation.
When the user completes a direct S3 upload, either regular or multipart, we take the `filesize` that was captured when the `ExternalUploadStub` was first created and compare it with the final `Content-Length` size of the file where it is stored in S3. Then, if the two do not match, we throw an error, delete the file on S3, and ban the user from uploading files for N (default 5) minutes. This would only happen if the user uploads a different file than what they first specified, or in the case of multipart uploads uploaded larger chunks than needed. This is done to prevent abuse of S3 storage by bad actors.
Also included in this PR is an update to vendor/uppy.js. This has been built locally from the latest uppy source at https://github.com/transloadit/uppy/commit/d613b849a6591083f8a0968aa8d66537e231bbcd. This must be done so that I can get my multipart upload changes into Discourse. When the Uppy team cuts a proper release, we can bump the package.json versions instead.
2021-08-24 18:46:54 -04:00
|
|
|
class SizeMismatchError < StandardError; end
|
2021-07-27 18:42:25 -04:00
|
|
|
|
|
|
|
attr_reader :external_upload_stub
|
|
|
|
|
FEATURE: Uppy direct S3 multipart uploads in composer (#14051)
This pull request introduces the endpoints required, and the JavaScript functionality in the `ComposerUppyUpload` mixin, for direct S3 multipart uploads. There are four new endpoints in the uploads controller:
* `create-multipart.json` - Creates the multipart upload in S3 along with an `ExternalUploadStub` record, storing information about the file in the same way as `generate-presigned-put.json` does for regular direct S3 uploads
* `batch-presign-multipart-parts.json` - Takes a list of part numbers and the unique identifier for an `ExternalUploadStub` record, and generates the presigned URLs for those parts if the multipart upload still exists and if the user has permission to access that upload
* `complete-multipart.json` - Completes the multipart upload in S3. Needs the full list of part numbers and their associated ETags which are returned when the part is uploaded to the presigned URL above. Only works if the user has permission to access the associated `ExternalUploadStub` record and the multipart upload still exists.
After we confirm the upload is complete in S3, we go through the regular `UploadCreator` flow, the same as `complete-external-upload.json`, and promote the temporary upload S3 into a full `Upload` record, moving it to its final destination.
* `abort-multipart.json` - Aborts the multipart upload on S3 and destroys the `ExternalUploadStub` record if the user has permission to access that upload.
Also added are a few new columns to `ExternalUploadStub`:
* multipart - Whether or not this is a multipart upload
* external_upload_identifier - The "upload ID" for an S3 multipart upload
* filesize - The size of the file when the `create-multipart.json` or `generate-presigned-put.json` is called. This is used for validation.
When the user completes a direct S3 upload, either regular or multipart, we take the `filesize` that was captured when the `ExternalUploadStub` was first created and compare it with the final `Content-Length` size of the file where it is stored in S3. Then, if the two do not match, we throw an error, delete the file on S3, and ban the user from uploading files for N (default 5) minutes. This would only happen if the user uploads a different file than what they first specified, or in the case of multipart uploads uploaded larger chunks than needed. This is done to prevent abuse of S3 storage by bad actors.
Also included in this PR is an update to vendor/uppy.js. This has been built locally from the latest uppy source at https://github.com/transloadit/uppy/commit/d613b849a6591083f8a0968aa8d66537e231bbcd. This must be done so that I can get my multipart upload changes into Discourse. When the Uppy team cuts a proper release, we can bump the package.json versions instead.
2021-08-24 18:46:54 -04:00
|
|
|
def self.ban_user_from_external_uploads!(user:, ban_minutes: 5)
|
|
|
|
Discourse.redis.setex("#{BAN_USER_REDIS_PREFIX}#{user.id}", ban_minutes.minutes.to_i, "1")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.user_banned?(user)
|
|
|
|
Discourse.redis.get("#{BAN_USER_REDIS_PREFIX}#{user.id}") == "1"
|
|
|
|
end
|
|
|
|
|
2021-10-18 23:25:42 -04:00
|
|
|
def initialize(external_upload_stub, upload_create_opts = {})
|
2021-07-27 18:42:25 -04:00
|
|
|
@external_upload_stub = external_upload_stub
|
2021-10-18 23:25:42 -04:00
|
|
|
@upload_create_opts = upload_create_opts
|
2021-07-27 18:42:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_promote?
|
|
|
|
external_upload_stub.status == ExternalUploadStub.statuses[:created]
|
|
|
|
end
|
|
|
|
|
|
|
|
def promote_to_upload!
|
|
|
|
raise CannotPromoteError if !can_promote?
|
|
|
|
|
|
|
|
external_upload_stub.update!(status: ExternalUploadStub.statuses[:uploaded])
|
|
|
|
external_stub_object = Discourse.store.object_from_path(external_upload_stub.key)
|
|
|
|
external_etag = external_stub_object.etag
|
|
|
|
external_size = external_stub_object.size
|
|
|
|
external_sha1 = external_stub_object.metadata["sha1-checksum"]
|
|
|
|
|
|
|
|
# This could be legitimately nil, if it's too big to download on the
|
|
|
|
# server, or it could have failed. To this end we set a should_download
|
|
|
|
# variable as well to check.
|
|
|
|
tempfile = nil
|
|
|
|
should_download = external_size < DOWNLOAD_LIMIT
|
FEATURE: Uppy direct S3 multipart uploads in composer (#14051)
This pull request introduces the endpoints required, and the JavaScript functionality in the `ComposerUppyUpload` mixin, for direct S3 multipart uploads. There are four new endpoints in the uploads controller:
* `create-multipart.json` - Creates the multipart upload in S3 along with an `ExternalUploadStub` record, storing information about the file in the same way as `generate-presigned-put.json` does for regular direct S3 uploads
* `batch-presign-multipart-parts.json` - Takes a list of part numbers and the unique identifier for an `ExternalUploadStub` record, and generates the presigned URLs for those parts if the multipart upload still exists and if the user has permission to access that upload
* `complete-multipart.json` - Completes the multipart upload in S3. Needs the full list of part numbers and their associated ETags which are returned when the part is uploaded to the presigned URL above. Only works if the user has permission to access the associated `ExternalUploadStub` record and the multipart upload still exists.
After we confirm the upload is complete in S3, we go through the regular `UploadCreator` flow, the same as `complete-external-upload.json`, and promote the temporary upload S3 into a full `Upload` record, moving it to its final destination.
* `abort-multipart.json` - Aborts the multipart upload on S3 and destroys the `ExternalUploadStub` record if the user has permission to access that upload.
Also added are a few new columns to `ExternalUploadStub`:
* multipart - Whether or not this is a multipart upload
* external_upload_identifier - The "upload ID" for an S3 multipart upload
* filesize - The size of the file when the `create-multipart.json` or `generate-presigned-put.json` is called. This is used for validation.
When the user completes a direct S3 upload, either regular or multipart, we take the `filesize` that was captured when the `ExternalUploadStub` was first created and compare it with the final `Content-Length` size of the file where it is stored in S3. Then, if the two do not match, we throw an error, delete the file on S3, and ban the user from uploading files for N (default 5) minutes. This would only happen if the user uploads a different file than what they first specified, or in the case of multipart uploads uploaded larger chunks than needed. This is done to prevent abuse of S3 storage by bad actors.
Also included in this PR is an update to vendor/uppy.js. This has been built locally from the latest uppy source at https://github.com/transloadit/uppy/commit/d613b849a6591083f8a0968aa8d66537e231bbcd. This must be done so that I can get my multipart upload changes into Discourse. When the Uppy team cuts a proper release, we can bump the package.json versions instead.
2021-08-24 18:46:54 -04:00
|
|
|
|
|
|
|
# We require that the file size is specified ahead of time, and compare
|
|
|
|
# it here to make sure that people are not uploading excessively large
|
|
|
|
# files to the external provider. If this happens, the user will be banned
|
|
|
|
# from uploading to the external provider for N minutes.
|
|
|
|
if external_size != external_upload_stub.filesize
|
|
|
|
ExternalUploadManager.ban_user_from_external_uploads!(
|
|
|
|
user: external_upload_stub.created_by,
|
|
|
|
ban_minutes: SIZE_MISMATCH_BAN_MINUTES
|
|
|
|
)
|
|
|
|
raise SizeMismatchError.new("expected: #{external_upload_stub.filesize}, actual: #{external_size}")
|
|
|
|
end
|
|
|
|
|
2021-07-27 18:42:25 -04:00
|
|
|
if should_download
|
|
|
|
tempfile = download(external_upload_stub.key, external_upload_stub.upload_type)
|
|
|
|
|
|
|
|
raise DownloadFailedError if tempfile.blank?
|
|
|
|
|
|
|
|
actual_sha1 = Upload.generate_digest(tempfile)
|
|
|
|
if external_sha1 && external_sha1 != actual_sha1
|
|
|
|
raise ChecksumMismatchError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO (martin): See if these additional opts will be needed
|
2021-10-18 23:25:42 -04:00
|
|
|
# - check if retain_hours is needed
|
2021-07-27 18:42:25 -04:00
|
|
|
opts = {
|
|
|
|
type: external_upload_stub.upload_type,
|
|
|
|
existing_external_upload_key: external_upload_stub.key,
|
|
|
|
external_upload_too_big: external_size > DOWNLOAD_LIMIT,
|
|
|
|
filesize: external_size
|
2021-10-18 23:25:42 -04:00
|
|
|
}.merge(@upload_create_opts)
|
2021-07-27 18:42:25 -04:00
|
|
|
|
|
|
|
UploadCreator.new(tempfile, external_upload_stub.original_filename, opts).create_for(
|
|
|
|
external_upload_stub.created_by_id
|
|
|
|
)
|
|
|
|
rescue
|
2021-08-24 21:11:19 -04:00
|
|
|
if !SiteSetting.enable_upload_debug_mode
|
2021-08-25 01:35:55 -04:00
|
|
|
# We don't need to do anything special to abort multipart uploads here,
|
|
|
|
# because at this point (calling promote_to_upload!), the multipart
|
|
|
|
# upload would already be complete.
|
|
|
|
Discourse.store.delete_file(external_upload_stub.key)
|
2021-08-24 21:11:19 -04:00
|
|
|
external_upload_stub.destroy!
|
|
|
|
else
|
|
|
|
external_upload_stub.update(status: ExternalUploadStub.statuses[:failed])
|
|
|
|
end
|
FEATURE: Uppy direct S3 multipart uploads in composer (#14051)
This pull request introduces the endpoints required, and the JavaScript functionality in the `ComposerUppyUpload` mixin, for direct S3 multipart uploads. There are four new endpoints in the uploads controller:
* `create-multipart.json` - Creates the multipart upload in S3 along with an `ExternalUploadStub` record, storing information about the file in the same way as `generate-presigned-put.json` does for regular direct S3 uploads
* `batch-presign-multipart-parts.json` - Takes a list of part numbers and the unique identifier for an `ExternalUploadStub` record, and generates the presigned URLs for those parts if the multipart upload still exists and if the user has permission to access that upload
* `complete-multipart.json` - Completes the multipart upload in S3. Needs the full list of part numbers and their associated ETags which are returned when the part is uploaded to the presigned URL above. Only works if the user has permission to access the associated `ExternalUploadStub` record and the multipart upload still exists.
After we confirm the upload is complete in S3, we go through the regular `UploadCreator` flow, the same as `complete-external-upload.json`, and promote the temporary upload S3 into a full `Upload` record, moving it to its final destination.
* `abort-multipart.json` - Aborts the multipart upload on S3 and destroys the `ExternalUploadStub` record if the user has permission to access that upload.
Also added are a few new columns to `ExternalUploadStub`:
* multipart - Whether or not this is a multipart upload
* external_upload_identifier - The "upload ID" for an S3 multipart upload
* filesize - The size of the file when the `create-multipart.json` or `generate-presigned-put.json` is called. This is used for validation.
When the user completes a direct S3 upload, either regular or multipart, we take the `filesize` that was captured when the `ExternalUploadStub` was first created and compare it with the final `Content-Length` size of the file where it is stored in S3. Then, if the two do not match, we throw an error, delete the file on S3, and ban the user from uploading files for N (default 5) minutes. This would only happen if the user uploads a different file than what they first specified, or in the case of multipart uploads uploaded larger chunks than needed. This is done to prevent abuse of S3 storage by bad actors.
Also included in this PR is an update to vendor/uppy.js. This has been built locally from the latest uppy source at https://github.com/transloadit/uppy/commit/d613b849a6591083f8a0968aa8d66537e231bbcd. This must be done so that I can get my multipart upload changes into Discourse. When the Uppy team cuts a proper release, we can bump the package.json versions instead.
2021-08-24 18:46:54 -04:00
|
|
|
|
2021-07-27 18:42:25 -04:00
|
|
|
raise
|
|
|
|
ensure
|
|
|
|
tempfile&.close!
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def download(key, type)
|
|
|
|
url = Discourse.store.signed_url_for_path(external_upload_stub.key)
|
|
|
|
FileHelper.download(
|
|
|
|
url,
|
|
|
|
max_file_size: DOWNLOAD_LIMIT,
|
|
|
|
tmp_file_name: "discourse-upload-#{type}",
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|