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

global axios instance breaking with other packages that use it #2103

Open
LeTamanoir opened this issue Nov 24, 2024 · 1 comment
Open

global axios instance breaking with other packages that use it #2103

LeTamanoir opened this issue Nov 24, 2024 · 1 comment

Comments

@LeTamanoir
Copy link

LeTamanoir commented Nov 24, 2024

Versions:

  • @inertiajs/core version: 1.2.0
  • @inertiajs/react version: 1.2.0

Describe the problem:

If I add a custom error interceptor to the global Axios instance in my project that changes the shape of the error it will break Inertia's error handling (handling of 409 Conflict etc...).

for example, the following code from @notabene/pii-sdk adds an error interceptor to the global axios instance:
https://gitlab.com/notabene/open-source/pii-sdk/-/blob/master/src/utils/create-client.ts?ref_type=heads#L42-63

I've opened an issue (https://gitlab.com/notabene/open-source/pii-sdk/-/issues/2) on their repo to ask them if they can create their own instance and not add interceptor to the global one.
But I believe this could also be done in Inertia as it also represent a security risk:
if a package is compromised and adds custom interceptor to perform malicious actions (e.g. steal credentials, etc...) inertia won't be protected and will send all of its request through it.

Steps to reproduce:

I created an example repo: https://github.com/LeTamanoir/inertia-axios-bug

I added interceptors to the global axios instance here: https://github.com/LeTamanoir/inertia-axios-bug/blob/main/resources/js/app.tsx

then you can do the following to reproduce:

normally you should see the request error with a 409 Conflict status code, which will not be handled by inertia to trigger the full page reload as the added interceptor changes the shape of the error which causes this part of the code

} else if (this.isLocationVisitResponse(error.response)) {
const locationUrl = hrefToUrl(error.response.headers['x-inertia-location'])
const requestUrl = url
if (requestUrl.hash && !locationUrl.hash && urlWithoutHash(requestUrl).href === locationUrl.href) {
locationUrl.hash = requestUrl.hash
}
this.locationVisit(locationUrl, preserveScroll === true)
} else if (error.response) {
to not trigger as the response key is not present on the error rejected in the custom interceptor.

To fix this I think creating a local axios instance in inertia's core package should be enough, happy to submit a fix PR if you want 🙏

@LeTamanoir
Copy link
Author

here is an example PR: #2104

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

No branches or pull requests

1 participant