Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload files using Rest #4300

Merged
merged 12 commits into from
Sep 23, 2022
Merged

Upload files using Rest #4300

merged 12 commits into from
Sep 23, 2022

Conversation

zdeveloper
Copy link
Contributor

@zdeveloper zdeveloper commented Sep 20, 2022

BACKEND PULL REQUEST

Related Issue

Changes Proposed

Additional Information

  • decided to not split changes into two PRs to guarantee graphql backward compatibility, for two reasons
    • The app front end reloads when it detects that a new version is out
    • We have a very low number of file upload users

Testing

Checklist for Primary Reviewer

  • Any large-scale changes have been deployed to test, dev, or pentest and smoke tested

@zdeveloper zdeveloper temporarily deployed to Dev2 September 21, 2022 20:48 Inactive
@zdeveloper zdeveloper temporarily deployed to Dev2 September 21, 2022 21:17 Inactive
@@ -34,6 +35,7 @@ public class TestResultUpload extends AuditedEntity {

@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "org_id")
@JsonIgnore
private Organization organization;
Copy link
Contributor Author

@zdeveloper zdeveloper Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Json Ignoring this since we don't want to return it as part of the response object when uploading results

@zdeveloper zdeveloper force-pushed the zedd/upload-files-with-rest branch 2 times, most recently from ff73873 to 4c7842c Compare September 22, 2022 15:24

/** An error thrown when CSV uploads fail for reasons other than validation */
@ResponseStatus(HttpStatus.BAD_REQUEST)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since allows us to set the response status to 400 when we throw this error inside the upload controller

@zdeveloper zdeveloper temporarily deployed to Dev2 September 22, 2022 16:35 Inactive
@zdeveloper zdeveloper temporarily deployed to Dev2 September 22, 2022 17:10 Inactive
@zdeveloper zdeveloper changed the title [Draft] Upload files using Rest Upload files using Rest Sep 22, 2022
@zdeveloper zdeveloper marked this pull request as ready for review September 22, 2022 17:27
@zdeveloper zdeveloper temporarily deployed to Dev2 September 22, 2022 18:13 Inactive
private final TestResultUploadService testResultUploadService;

@PostMapping(PATIENT_UPLOAD)
public String handlePatientsUpload(@RequestParam("file") MultipartFile file) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to put some stricter typing/validation on these multipart files? Such that we can reject if it's not a CSV? In particular, I'm worried that since this won't be applying graphQL's argument limits anymore, theoretically people could try to upload massive or incorrectly formatted files - it's a bit of a security concern, even though this endpoint is only open to authenticated users.

Copy link
Contributor

@emmastephenson emmastephenson Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's a Spring configuration we can enable: spring.servlet.multipart.max-file-size
Great minds think alike - you fixed this in the same minute I commented 🤣

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also adding an assert on the type of the file, even though that does nothing to the security since file extensions are a syntactical sugar on files

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have we thought about using something like clamav for file type and virus scanning?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(we were using clamav for file upload scanning on a defense project and it worked pretty well, though not sure it would work for this specific use case)

Copy link
Contributor Author

@zdeveloper zdeveloper Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think I ever heard of clamav, it could be something we can look into, doesnt seem like a quick thing to integrate with spring though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no I wasn't suggesting it for this PR 😄 maybe just a future thing if we want to bump up our security

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also from what I remember it was not too bad to integrate with java/spring but not a quick add

@zdeveloper zdeveloper force-pushed the zedd/upload-files-with-rest branch from f45092f to 451e7fe Compare September 22, 2022 18:49
@zdeveloper zdeveloper force-pushed the zedd/upload-files-with-rest branch from 451e7fe to db00372 Compare September 22, 2022 18:50
max-file-size: 50MB
server:
error:
include-stacktrace: never
Copy link
Contributor Author

@zdeveloper zdeveloper Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the trace from any exceptions returned to the frontend

@zdeveloper zdeveloper temporarily deployed to Dev2 September 22, 2022 18:58 Inactive
@@ -49,6 +49,12 @@ spring:
jdbc:
initialize-schema: never
table-name: ${spring.jpa.properties.hibernate.default_schema}.spring_session
servlet:
multipart:
max-file-size: 50MB
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is set to the same max file limit enforced by the frontend for result upload

assertThrows(IllegalGraphqlArgumentException.class, () -> sut.uploadPatients(input));
assertThat(caught).hasMessage("PANIC");
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests have been replaced by FileUploadControllerTest

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

94.6% 94.6% Coverage
0.0% 0.0% Duplication

Copy link
Contributor

@georgehager georgehager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks Zedd! I had a question about how we scan files for upload and like 2 tiny things, but nothing blocking 😄

private final TestResultUploadService testResultUploadService;

@PostMapping(PATIENT_UPLOAD)
public String handlePatientsUpload(@RequestParam("file") MultipartFile file) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have we thought about using something like clamav for file type and virus scanning?

private final TestResultUploadService testResultUploadService;

@PostMapping(PATIENT_UPLOAD)
public String handlePatientsUpload(@RequestParam("file") MultipartFile file) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(we were using clamav for file upload scanning on a defense project and it worked pretty well, though not sure it would work for this specific use case)

Copy link
Contributor

@emmastephenson emmastephenson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for getting to these Spring cleanup tickets so quickly 😄

@zdeveloper zdeveloper merged commit 692f679 into main Sep 23, 2022
@zdeveloper zdeveloper deleted the zedd/upload-files-with-rest branch September 23, 2022 13:41
georgehager pushed a commit that referenced this pull request Sep 27, 2022
* upload files via rest

* update frontend tests

* add file upload controller test and remove unused resolvers

* remove unused code

* add tests and enable cors

* add Patient Upload tests

* adding sonar feedback

* add access-token to FileUploadService

* update patient upload error alert title

* update uploads error checking

* limit upload file size to 50MB in the backend to match frontend

* only accept csv files and dont send back exception traces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move file uploads to use Rest instead of Graphql
3 participants