Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

fix direct upload for all content types

Markus Kahl320d1e77dec5
diff --git a/app/contracts/attachments/prepare_upload_contract.rb b/app/contracts/attachments/prepare_upload_contract.rb
index 91f6a717dee..fde24606c6e 100644
--- a/app/contracts/attachments/prepare_upload_contract.rb
+++ b/app/contracts/attachments/prepare_upload_contract.rb
@@ -37,5 +37,24 @@ module Attachments
def validate_direct_uploads_active
errors.add :base, :not_available unless OpenProject::Configuration.direct_uploads?
end
+
+ ##
+ # The browser hasn't given a specific content type.
+ # So we don't check the content type here during the prepare_upload step yet.
+ #
+ # We'll do it again later in the FinishDirectUploadJob where the normal create contract
+ # without this opt-out is used, and where a more specific content type may be
+ # determined.
+ def validate_content_type
+ return if pending_content_type?
+
+ super
+ end
+
+ def pending_content_type?
+ return false unless OpenProject::Configuration.direct_uploads?
+
+ model.content_type == OpenProject::ContentTypeDetector::SENSIBLE_DEFAULT
+ end
end
end
diff --git a/app/services/attachments/set_prepared_attributes_service.rb b/app/services/attachments/set_prepared_attributes_service.rb
index f46d99334cc..9eb9c7a2fd7 100644
--- a/app/services/attachments/set_prepared_attributes_service.rb
+++ b/app/services/attachments/set_prepared_attributes_service.rb
@@ -50,9 +50,9 @@ module Attachments
model.extend(OpenProject::ChangedBySystem)
model.change_by_system do
model.downloads = -1
- # Set a content type as the file is not present
- # The real content type will be set by FinishDirectUploadJob
- model.content_type = params[:content_type] || OpenProject::ContentTypeDetector::SENSIBLE_DEFAULT
+ # Set a preliminary content type as the file is not present
+ # The content type will be updated by the FinishDirectUploadJob if necessary.
+ model.content_type = params[:content_type].presence || OpenProject::ContentTypeDetector::SENSIBLE_DEFAULT
end
end
diff --git a/app/workers/attachments/finish_direct_upload_job.rb b/app/workers/attachments/finish_direct_upload_job.rb
index 7a16504b441..cd0c810fd1c 100644
--- a/app/workers/attachments/finish_direct_upload_job.rb
+++ b/app/workers/attachments/finish_direct_upload_job.rb
@@ -77,8 +77,7 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
end
def validate_attachment(attachment)
- contract = ::Attachments::CreateContract
- .new(attachment, attachment.author)
+ contract = create_contract attachment
unless contract.valid?
errors = contract.errors.full_messages.join(", ")
@@ -86,6 +85,10 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
end
end
+ def create_contract(attachment)
+ ::Attachments::CreateContract.new(attachment, attachment.author)
+ end
+
def journalize_container(attachment)
journable = attachment.container
diff --git a/frontend/npm-shrinkwrap.json b/frontend/npm-shrinkwrap.json
index 74e3d2dc8f1..0158ddce619 100644
--- a/frontend/npm-shrinkwrap.json
+++ b/frontend/npm-shrinkwrap.json
@@ -2829,6 +2829,12 @@
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.149.tgz",
"integrity": "sha512-ijGqzZt/b7BfzcK9vTrS6MFljQRPn5BFWOx8oE0GYxribu6uV+aA9zZuXI1zc/etK9E8nrgdoF2+LgUw7+9tJQ=="
},
+ "@types/mime": {
+ "version": "2.0.3",
+ "resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.3.tgz",
+ "integrity": "sha512-Jus9s4CDbqwocc5pOAnh8ShfrnMcPHuJYzVcSUU7lrh8Ni5HuIqX3oilL86p3dlTrk0LzHRCgA/GQ7uNCw6l2Q==",
+ "dev": true
+ },
"@types/minimatch": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz",
@@ -8068,6 +8074,13 @@
"secure-compare": "3.0.1",
"union": "~0.5.0",
"url-join": "^2.0.5"
+ },
+ "dependencies": {
+ "mime": {
+ "version": "1.6.0",
+ "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
+ "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg=="
+ }
}
},
"http-signature": {
@@ -9212,6 +9225,12 @@
"tslib": "^1.10.0"
},
"dependencies": {
+ "mime": {
+ "version": "1.6.0",
+ "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
+ "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==",
+ "optional": true
+ },
"source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
@@ -9694,9 +9713,9 @@
}
},
"mime": {
- "version": "1.6.0",
- "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
- "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg=="
+ "version": "2.5.2",
+ "resolved": "https://registry.npmjs.org/mime/-/mime-2.5.2.tgz",
+ "integrity": "sha512-tqkh47FzKeCPD2PUiPB6pkbMzsCasjxAfC62/Wap5qrUWcb+sFasXUC5I3gYM5iBM8v/Qpn4UK0x+j0iHyFPDg=="
},
"mime-db": {
"version": "1.46.0",
@@ -9704,11 +9723,18 @@
"integrity": "sha512-svXaP8UQRZ5K7or+ZmfNhg2xX3yKDMUzqadsSqi4NCH/KomcH75MAMYAGVlvXn4+b/xOPhS3I2uHKRUzvjY7BQ=="
},
"mime-types": {
- "version": "2.1.29",
- "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.29.tgz",
- "integrity": "sha512-Y/jMt/S5sR9OaqteJtslsFZKWOIIqMACsJSiHghlCAyhf7jfVYjKBmLiX8OgpWeW+fjJ2b+Az69aPFPkUOY6xQ==",
+ "version": "2.1.33",
+ "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.33.tgz",
+ "integrity": "sha512-plLElXp7pRDd0bNZHw+nMd52vRYjLwQjygaNg7ddJ2uJtTlmnTCjWuPKxVu6//AdaRuME84SvLW91sIkBqGT0g==",
"requires": {
- "mime-db": "1.46.0"
+ "mime-db": "1.50.0"
+ },
+ "dependencies": {
+ "mime-db": {
+ "version": "1.50.0",
+ "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.50.0.tgz",
+ "integrity": "sha512-9tMZCDlYHqeERXEHO9f/hKfNXhre5dK2eE/krIvUjZbS2KPcqGDfNShIWS1uW9XOTKQKqK6qbeOci18rbfW77A=="
+ }
}
},
"mimic-fn": {
@@ -10194,6 +10220,11 @@
"isarray": "^1.0.0"
}
},
+ "path-browserify": {
+ "version": "0.0.1",
+ "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-0.0.1.tgz",
+ "integrity": "sha512-BapA40NHICOS+USX9SN4tyhq+A2RrN/Ws5F0Z5aMHDp98Fl86lX8Oti8B7uN93L4Ifv4fHOEA+pQw87gmMO/lQ=="
+ },
"punycode": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz",
@@ -10958,11 +10989,6 @@
"resolved": "https://registry.npmjs.org/pascalcase/-/pascalcase-0.1.1.tgz",
"integrity": "sha1-s2PlXoAGym/iF4TS2yK9FdeRfxQ="
},
- "path-browserify": {
- "version": "0.0.1",
- "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-0.0.1.tgz",
- "integrity": "sha512-BapA40NHICOS+USX9SN4tyhq+A2RrN/Ws5F0Z5aMHDp98Fl86lX8Oti8B7uN93L4Ifv4fHOEA+pQw87gmMO/lQ=="
- },
"path-dirname": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/path-dirname/-/path-dirname-1.0.2.tgz",
@@ -12940,6 +12966,11 @@
"statuses": "~1.5.0"
},
"dependencies": {
+ "mime": {
+ "version": "1.6.0",
+ "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
+ "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg=="
+ },
"ms": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
diff --git a/frontend/package.json b/frontend/package.json
index 94953e349f9..d53b383e111 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -13,6 +13,7 @@
"@angular/language-service": "11.2.3",
"@jsdevtools/coverage-istanbul-loader": "3.0.5",
"@types/jasmine": "~3.6.0",
+ "@types/mime": "^2.0.3",
"@typescript-eslint/eslint-plugin": "^4.16.1",
"@typescript-eslint/parser": "^4.16.1",
"codelyzer": "^6.0.0",
@@ -96,6 +97,7 @@
"json5": "^2.1.0",
"lodash": "^4.17.19",
"mark.js": "^8.11.0",
+ "mime": "^2.5.2",
"moment": "^2.24.0",
"moment-timezone": "^0.5.26",
"mousetrap": "~1.6.3",
diff --git a/frontend/src/app/components/api/op-file-upload/op-direct-file-upload.service.ts b/frontend/src/app/components/api/op-file-upload/op-direct-file-upload.service.ts
index 636e4c28620..627cffbe158 100644
--- a/frontend/src/app/components/api/op-file-upload/op-direct-file-upload.service.ts
+++ b/frontend/src/app/components/api/op-file-upload/op-direct-file-upload.service.ts
@@ -33,6 +33,8 @@ import { from, Observable, of } from "rxjs";
import { share, switchMap } from "rxjs/operators";
import { OpenProjectFileUploadService, UploadBlob, UploadFile, UploadInProgress } from './op-file-upload.service';
+import * as mime from "mime";
+
interface PrepareUploadResult {
url:string;
form:FormData;
@@ -100,13 +102,16 @@ export class OpenProjectDirectFileUploadService extends OpenProjectFileUploadSer
};
}
- public getDirectUploadFormFrom(url:string, file:UploadFile|UploadBlob):Promise<PrepareUploadResult> {
+ public async getDirectUploadFormFrom(url:string, file:UploadFile|UploadBlob):Promise<PrepareUploadResult> {
+ const fileName = file.customName || file.name;
+ const contentType = file.type || (fileName && mime.getType(fileName)) || '';
+
const formData = new FormData();
const metadata = {
description: file.description,
- fileName: file.customName || file.name,
+ fileName: fileName,
fileSize: file.size,
- contentType: file.type
+ contentType: contentType,
};
/*
@@ -121,28 +126,25 @@ export class OpenProjectDirectFileUploadService extends OpenProjectFileUploadSer
JSON.stringify(metadata),
);
- const result = this
+ const result = await this
.http
.request<HalResource>(
- "post",
+ 'post',
url,
{
body: formData,
withCredentials: true,
- responseType: "json" as any
- }
+ responseType: 'json' as any,
+ },
)
- .toPromise()
- .then((res) => {
- const form = new FormData();
+ .toPromise();
- _.each(res._links.addAttachment.form_fields, (value, key) => {
- form.append(key, value);
- });
+ const form = new FormData();
- return { url: res._links.addAttachment.href, form: form, response: res };
- });
+ _.each(result._links.addAttachment.form_fields, (value, key) => {
+ form.append(key, value);
+ });
- return result;
+ return { url: result._links.addAttachment.href, form, response: result };
}
}
diff --git a/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb b/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb
index 2788b911f84..20aae827e90 100644
--- a/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb
+++ b/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb
@@ -154,6 +154,34 @@ shared_examples 'it supports direct uploads' do
end
end
end
+
+ context 'with an attachment whitelist', with_settings: { attachment_whitelist: ['text/csv'] } do
+ context 'with an allowed content type' do
+ let(:metadata) { { fileName: 'cats.csv', fileSize: file.size, contentType: 'text/csv' } }
+
+ it 'should succeed' do
+ expect(subject.status).to eq 201
+ end
+ end
+
+ context 'with a forbidden content type' do
+ let(:metadata) { { fileName: 'cats.txt', fileSize: file.size, contentType: 'text/plain' } }
+
+ it 'should fail' do
+ expect(subject.status).to eq 422
+ expect(subject.body).to include "not whitelisted"
+ end
+ end
+
+ context 'with a non-specific content type not on the whitelist' do
+ let(:metadata) { { fileName: 'cats.bin', fileSize: file.size, contentType: 'application/binary' } }
+
+ # the actual whitelist check will be performed in the FinishDirectUpload job in this case
+ it 'should still succeed' do
+ expect(subject.status).to eq 201
+ end
+ end
+ end
end
end
end