@sasjs/adapter 6.2.0 → 6.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2432,6 +2432,7 @@ class RequestClient {
2432
2432
  this.verboseMode = false;
2433
2433
  this.csrfToken = { headerName: '', value: '' };
2434
2434
  this.isRecoveringFromNetworkError = false;
2435
+ this.deploymentTransport = false;
2435
2436
  this.mockReadiness = (0, mockReadiness_1.captureMockReadiness)();
2436
2437
  /**
2437
2438
  * this method returns all requests, an array of SASjsRequest type
@@ -2665,8 +2666,19 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
2665
2666
  }
2666
2667
  };
2667
2668
  this.handleError = (e_1, callback_1, ...args_1) => __awaiter(this, [e_1, callback_1, ...args_1], void 0, function* (e, callback, debug = false) {
2668
- var _a;
2669
+ var _a, _b;
2669
2670
  const response = e.response;
2671
+ // A missing response may mean a write succeeded or a redirect was blocked.
2672
+ // Retrying would repeat an operation whose outcome is unknown.
2673
+ if (this.deploymentTransport && !response && e.isAxiosError) {
2674
+ throw (0, responseReceived_1.markResponseReceived)(e, false);
2675
+ }
2676
+ // These recovery callbacks recursively replay the original operation and can
2677
+ // repeat an uncertain deployment write. Only the bounded CSRF challenge
2678
+ // retry below is permitted on the restricted transport.
2679
+ if (this.deploymentTransport &&
2680
+ (e instanceof errors_1.AuthorizeError || e instanceof InvalidSASjsCsrfError_1.InvalidSASjsCsrfError))
2681
+ throw e;
2670
2682
  if (e instanceof errors_1.AuthorizeError) {
2671
2683
  const res = yield this.httpClient
2672
2684
  .get(e.confirmUrl, {
@@ -2709,6 +2721,26 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
2709
2721
  });
2710
2722
  }
2711
2723
  if ((response === null || response === void 0 ? void 0 : response.status) === 403 || (response === null || response === void 0 ? void 0 : response.status) === 449) {
2724
+ if (this.deploymentTransport) {
2725
+ const challenge = this.parseCsrfToken(response);
2726
+ const previous = challenge && ((_a = response.config.headers) === null || _a === void 0 ? void 0 : _a.get(challenge.headerName));
2727
+ if (!(challenge === null || challenge === void 0 ? void 0 : challenge.headerName) ||
2728
+ !/^x-[a-z0-9-]*(?:csrf|xsrf)[a-z0-9-]*$/.test(challenge.headerName) ||
2729
+ !challenge.value ||
2730
+ previous === challenge.value)
2731
+ throw (0, responseReceived_1.markResponseReceived)(e, true);
2732
+ this.csrfToken = challenge;
2733
+ try {
2734
+ const config = response.config;
2735
+ config.headers.set(challenge.headerName, challenge.value);
2736
+ const retried = yield this.httpClient.request(config);
2737
+ (0, exports.throwIfError)(retried);
2738
+ return this.parseResponse(retried);
2739
+ }
2740
+ catch (retryError) {
2741
+ throw (0, responseReceived_1.markResponseReceived)(retryError, Boolean(retryError.response));
2742
+ }
2743
+ }
2712
2744
  this.parseAndSetCsrfToken(response);
2713
2745
  if (this.csrfToken.headerName && this.csrfToken.value) {
2714
2746
  return yield callback().catch((err) => {
@@ -2744,7 +2776,7 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
2744
2776
  .get('/', { withXSRFToken: true })
2745
2777
  .catch((err) => err.response);
2746
2778
  if (rootResponse === null || rootResponse === void 0 ? void 0 : rootResponse.data) {
2747
- const cookie = (_a = /<script>document.cookie = '(XSRF-TOKEN=.*; Max-Age=86400; SameSite=Strict; Path=\/;)'<\/script>/.exec(rootResponse.data)) === null || _a === void 0 ? void 0 : _a[1];
2779
+ const cookie = (_b = /<script>document.cookie = '(XSRF-TOKEN=.*; Max-Age=86400; SameSite=Strict; Path=\/;)'<\/script>/.exec(rootResponse.data)) === null || _b === void 0 ? void 0 : _b[1];
2748
2780
  if (cookie && typeof document !== 'undefined') {
2749
2781
  document.cookie = cookie;
2750
2782
  }
@@ -2753,10 +2785,7 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
2753
2785
  return yield callback();
2754
2786
  }
2755
2787
  catch (retryError) {
2756
- // Session could not be recovered — surface LoginRequiredError. The
2757
- // retry itself may have reached the server and been refused, which is
2758
- // not the same as never arriving: the marker follows the retry's own
2759
- // answer, not the opaque network error that started this.
2788
+ // Session could not be recovered — surface LoginRequiredError
2760
2789
  throw (0, responseReceived_1.markResponseReceived)(new errors_1.LoginRequiredError(), (0, responseReceived_1.responseWasReceived)(retryError));
2761
2790
  }
2762
2791
  finally {
@@ -2776,6 +2805,7 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
2776
2805
  }
2777
2806
  }
2778
2807
  setConfig(baseUrl, httpsAgentOptions) {
2808
+ this.deploymentTransport = false;
2779
2809
  this.createHttpClient(baseUrl, httpsAgentOptions);
2780
2810
  }
2781
2811
  saveLocalStorageToken(accessToken, refreshToken) {
@@ -2811,6 +2841,45 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
2811
2841
  document.cookie = `${name}=; Max-Age=0; Path=/;`;
2812
2842
  }
2813
2843
  }
2844
+ copyCsrfFrom(source) {
2845
+ const locationBase = typeof location !== 'undefined' ? location.href : undefined;
2846
+ const targetUrl = new URL(this.getBaseUrl() || locationBase || '', locationBase);
2847
+ const sourceUrl = new URL(source.getBaseUrl() || locationBase || '', locationBase);
2848
+ if (targetUrl.origin !== sourceUrl.origin)
2849
+ throw new Error('Cannot copy cross-origin CSRF state');
2850
+ this.csrfToken = Object.assign({}, source.csrfToken);
2851
+ }
2852
+ /** Restrict a dedicated deployment client to its origin and prohibit redirects. */
2853
+ restrictDeploymentTransport() {
2854
+ const locationBase = typeof location !== 'undefined' ? location.href : undefined;
2855
+ const base = new URL(this.getBaseUrl() || locationBase || '', locationBase);
2856
+ this.httpClient.defaults.maxRedirects = 0;
2857
+ if (!(0, isNode_1.isNode)()) {
2858
+ if (typeof Request === 'undefined' ||
2859
+ new Request(base.href, { redirect: 'error' }).redirect !== 'error')
2860
+ throw new Error('Safe deployment requires fetch redirect support');
2861
+ this.httpClient.defaults.adapter = 'fetch';
2862
+ this.httpClient.defaults.fetchOptions = { redirect: 'error' };
2863
+ }
2864
+ this.httpClient.interceptors.request.use((config) => {
2865
+ if (config.data &&
2866
+ typeof config.data.getBuffer === 'function' &&
2867
+ typeof config.data.getBoundary === 'function') {
2868
+ const boundary = config.data.getBoundary();
2869
+ config.data = config.data.getBuffer();
2870
+ config.headers.set('Content-Type', `multipart/form-data; boundary=${boundary}`);
2871
+ }
2872
+ const url = new URL(config.url || '', config.baseURL || base.href);
2873
+ if (url.origin !== base.origin ||
2874
+ /[\\\u0000-\u001f\u007f]/.test(config.url || ''))
2875
+ throw new Error('Unsafe deployment request URL');
2876
+ return config;
2877
+ });
2878
+ this.deploymentTransport = true;
2879
+ }
2880
+ isDeploymentTransport() {
2881
+ return this.deploymentTransport;
2882
+ }
2814
2883
  getBaseUrl() {
2815
2884
  return this.httpClient.defaults.baseURL || '';
2816
2885
  }
@@ -2942,7 +3011,7 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
2942
3011
  return this.parseResponse(response);
2943
3012
  })
2944
3013
  .catch((e) => __awaiter(this, void 0, void 0, function* () {
2945
- return yield this.handleError(e, () => this.patch(url, data, accessToken));
3014
+ return yield this.handleError(e, () => this.patch(url, data, accessToken, overrideHeaders));
2946
3015
  }));
2947
3016
  });
2948
3017
  }
@@ -3034,17 +3103,6 @@ exports.RequestClient = RequestClient;
3034
3103
  * included here.
3035
3104
  */
3036
3105
  const isTokenRefreshUrl = (url) => !!url && /(^|\/)auth\/refresh(\?|$)/.test(url);
3037
- /**
3038
- * Raises the error a response status implies, marked as one built from a
3039
- * response.
3040
- *
3041
- * The marking has to happen here: every error below is a new object carrying
3042
- * the body, so the axios error - the thing that knew a response had arrived -
3043
- * is gone by the time a caller catches it (sasjs/adapter#895). A 401 from a
3044
- * refresh route is named by `throwForStatus` as a rejected refresh token
3045
- * (sasjs/adapter#975); this marks it as an answer that arrived, so a transport
3046
- * failure and a refusal are not reported with the same title.
3047
- */
3048
3106
  const throwIfError = (response) => {
3049
3107
  try {
3050
3108
  throwForStatus(response);