From 5343ca00d8f0cb74d790d692f7447fdede24384b Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Tue, 22 Sep 2020 09:26:16 +0100 Subject: [PATCH 1/3] fix(file-upload): maintain separate CSRF token for file uploads --- src/SASViyaApiClient.ts | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index bd7c8d1..b127788 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -36,6 +36,7 @@ export class SASViyaApiClient { } private csrfToken: CsrfToken | null = null + private fileUploadCsrfToken: CsrfToken | null = null private sessionManager = new SessionManager( this.serverUrl, this.contextName, @@ -1335,7 +1336,9 @@ export class SASViyaApiClient { const uploadResponse = await this.request( `${this.serverUrl}/files/files#rawUpload`, - createFileRequest + createFileRequest, + 'json', + 'fileUpload' ) uploadedFiles.push({ tableName, file: uploadResponse.result }) @@ -1490,22 +1493,36 @@ export class SASViyaApiClient { this.setCsrfToken(csrfToken) } + setFileUploadCsrfToken = (csrfToken: CsrfToken) => { + this.fileUploadCsrfToken = csrfToken + } + private async request( url: string, options: RequestInit, - contentType: 'text' | 'json' = 'json' + contentType: 'text' | 'json' = 'json', + type: 'fileUpload' | 'other' = 'other' ) { - if (this.csrfToken) { - options.headers = { - ...options.headers, - [this.csrfToken.headerName]: this.csrfToken.value + const callback = + type === 'fileUpload' + ? this.setFileUploadCsrfToken + : this.setCsrfTokenLocal + + if (type === 'other') { + if (this.csrfToken) { + options.headers = { + ...options.headers, + [this.csrfToken.headerName]: this.csrfToken.value + } + } + } else { + if (this.fileUploadCsrfToken) { + options.headers = { + ...options.headers, + [this.fileUploadCsrfToken.headerName]: this.fileUploadCsrfToken.value + } } } - return await makeRequest( - url, - options, - this.setCsrfTokenLocal, - contentType - ) + return await makeRequest(url, options, callback, contentType) } } From 9f6591d7e392fafdb52d79bbccb5ab244a0bcbcf Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Tue, 22 Sep 2020 09:32:45 +0100 Subject: [PATCH 2/3] chore(sasjs-tests): fix failing tests, bump adapter version --- sasjs-tests/package-lock.json | 6 +++--- sasjs-tests/package.json | 2 +- sasjs-tests/src/testSuites/RequestData.ts | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sasjs-tests/package-lock.json b/sasjs-tests/package-lock.json index 9fffa77..0a6980a 100644 --- a/sasjs-tests/package-lock.json +++ b/sasjs-tests/package-lock.json @@ -1357,9 +1357,9 @@ "integrity": "sha512-shAmDyaQC4H92APFoIaVDHCx5bStIocgvbwQyxPRrbUY20V1EYTbSDchWbuwlMG3V17cprZhA6+78JfB+3DTPw==" }, "@sasjs/adapter": { - "version": "1.3.13", - "resolved": "https://registry.npmjs.org/@sasjs/adapter/-/adapter-1.3.13.tgz", - "integrity": "sha512-dWcDxgY3FB7Yx1I5dPpeQeyJDu4lezhIFrjn6lbdwRhV15aqOt4l9o9qZP+VbgOXqyi9gN0Y+p+vs2chBDFQqg==", + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@sasjs/adapter/-/adapter-1.12.0.tgz", + "integrity": "sha512-0uGQH9ynomWzdBaEujEtcR38q6V7LCgG0mrb1Wellv6cC/IHD3j6WfeZZAgtiMPeOSJjbCDBOlVnzC2TlBqJFw==", "requires": { "es6-promise": "^4.2.8", "form-data": "^3.0.0", diff --git a/sasjs-tests/package.json b/sasjs-tests/package.json index f0d67fc..40e8293 100644 --- a/sasjs-tests/package.json +++ b/sasjs-tests/package.json @@ -4,7 +4,7 @@ "homepage": ".", "private": true, "dependencies": { - "@sasjs/adapter": "^1.3.13", + "@sasjs/adapter": "^1.12.0", "@sasjs/test-framework": "^1.4.0", "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.5.0", diff --git a/sasjs-tests/src/testSuites/RequestData.ts b/sasjs-tests/src/testSuites/RequestData.ts index 5adcc22..b6f17ae 100644 --- a/sasjs-tests/src/testSuites/RequestData.ts +++ b/sasjs-tests/src/testSuites/RequestData.ts @@ -88,7 +88,7 @@ export const sendArrTests = (adapter: SASjs): TestSuite => ({ return adapter.request("common/sendArr", data).catch((e) => e); }, assertion: (error: any) => { - return !!error && !!error.MESSAGE; + return !!error && !!error.body && !!error.body.message; } }, { @@ -185,7 +185,7 @@ export const sendObjTests = (adapter: SASjs): TestSuite => ({ }; return adapter.request("common/sendObj", invalidData).catch((e) => e); }, - assertion: (error: any) => !!error && !!error.MESSAGE + assertion: (error: any) => !!error && !!error.body && !!error.body.message }, { title: "Single string value", @@ -219,7 +219,7 @@ export const sendObjTests = (adapter: SASjs): TestSuite => ({ .catch((e) => e); }, assertion: (error: any) => { - return !!error && !!error.MESSAGE; + return !!error && !!error.body && !!error.body.message; } }, { From 95f3ebd51d9fb3119ba643034738ee37651c57a6 Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Tue, 22 Sep 2020 12:40:11 +0100 Subject: [PATCH 3/3] chore(dx): add pull request template --- PULL_REQUEST_TEMPLATE.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 PULL_REQUEST_TEMPLATE.md diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..ff4fc83 --- /dev/null +++ b/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,17 @@ +## Issue + +Link any related issue(s) in this section. + +## Intent + +What this PR intends to achieve. + +## Implementation + +What code changes have been made to achieve the intent. + +## Checks + +- [ ] Code is formatted correctly (`npm run lint:fix`). +- [ ] All unit tests are passing (`npm test`). +- [ ] All `sasjs-tests` are passing (instructions available [here](https://github.com/sasjs/adapter/blob/master/sasjs-tests/README.md)).