You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remove API key from headers in error message #4249
This was originally posted as a bug #4246 but the GitHub bot didn’t seem to like it. Trying again here as a feature request.
Describe the issue
I am catching the error responses from Axios when a request fails. I then use the error request to create logs, log to console or return to the browser. In many of these instances the error contains axios.defaults.headers.common.Authorization set before the request. I started going through deleting them before logging, but they are all over in the response and request, and {config} with the response and request. So far I have:
Ideally there would be a way to clean this up automatically so the outputs are safe to use anywhere.
To Reproduce
Any failed request
New feature.
Remove the Authorisation headers so error message can be used safely, with option to include them for debug.
Environment
Axios Version [0.24.0]
Reacted by Kinan Faham, Ajima Chukwuemeka, Pan, Alexander Hansen, Pavlo Andriiesh, Niklas Wenzel, Filipe Pinheiro, Deane Smith, Leonardo Carreiro, Umang Malik and 9 more
This issue is being automatically closed because it does not follow the issue template. Please read the issue template carefully and follow all of the instructions when opening a new issue.
Thanks
Reacted by maggie44, pcone, Umang Malik, Martin Picard (Thoughtworks), KalaRohit, AdithyaApium and Eleazar
My opinion is that this is not an issue, axios is not in charge of logging anything anywhere. All logging added is the responsibility of the implementor. You can clean the prototype or you can rather only log what you need and not the entire response.
Reacted by Eric MORAND and Dmitriy Mozgovoy
Reacted by Martin Picard (Thoughtworks), KalaRohit, Marwan Khalili, Victor Campello, Bryan Müller, Christoffer Nørbjerg, Kristian Barrett, 3repeating, Cameron Hicks and Adam Zimmerman
I can't think of a scenario where someone would need to get the Authorization header key returned back to them in the logs so I would suspect removing it would not create any friction in usage. Indeed it could be seen as the user responsibility, but as they are in multiple places throughout the error object it is likely at least some will miss it and then this single issue becomes multiplied on each repo using Axios (if they are lucky enough to catch it).
In short, yes this could potentially be placed on the user of the logs to manage, but would prevent user friction if it could be managed in advance.
I still disagree, firstly you should not be logging the entire response in any use case ever, what should be done is you should log only the parts of the response that you would like to keep for traces and to identify errors. When logging the entire response you are actually creating multiple potential issues, you could be logging user data, you can be breaching GDPR and you can even be login their credentials as that request data is in there too.
Also realistically the above suggestion would not cover all use cases by a long stretch. People implement any key-value pair they want for the token sometimes, amazon re-writes the key values when under an API gateway and the list goes on so it would theoretically be impossible for us to strip all of the possible tokens and credentials from the response object.
Reacted by Eric MORAND and Dmitriy Mozgovoy
Reacted by Martin Picard (Thoughtworks), Marwan Khalili, Tofunmi Falade, 3repeating and Alex
In nodejs, an uncaught error would be logged out by default, in which case we really don't want the Authorization header being accidentially leaked by the library's error object.
I understand that it's developer's responsibility to keep sensitive data secret and it's not realistic for the library to strip out all sensitive data. But when the default behavior of the language is to log out the whole error object, is it better to hide some common fields that contains secrets?
Reacted by Alexander Hansen, Sam OR, Igor Cherednichenko, Daniel Gomez Blanco, Sho, Ejnar Kaekebeke, Phillip Calvin, adebia ntoso, E. A. Wooten, Kristian Barrett and 6 more
I used axios in my project without thinking much about it (like most devs when they install an npm package). And now API tokens from request bodies showed up in my server logs.
This is not something that a regular users (= a developer working on a project) can predict.
Therefore, I believe AxiosError.toString() should contain less information by default, with an axios-wide option to opt into more information.
Reacted by Pan, Joey Mezzacappa, Martin Picard (Thoughtworks), 3repeating and matt-brale-xyz
What's the current opinion on this from maintainers? The following statement (which I fully agree with)
you should not be logging the entire response in any use case ever
seems to clash with this one
In nodejs, an uncaught error would be logged out by default
at least in the context of avoiding sensitive information getting leaked elsewhere. Users of Axios may not always be in control of what gets logged out if the default behaviour of the language is to print the whole error to console when it's uncaught. I'd also add that sanitising the error before calling a logging library may also miss other telemetry signals, like tracing, where auto-instrumentation libraries can capture errors within spans add errors to span events.
In general, if there is a possibility that data could expose sensitive information (as it's the case here with default logging in Javascript printing the object), the preferred approach is to sanitise it as close to the domain knowledge as possible (i.e. the Axios codebase or close to it). If Axios error objects were to change or add new fields that could contain sensitive information, the change to sanitise can come in the same version bump.
In terms of HTTP instrumentation, other libraries like opentelemetry-instrumentation-http, or Envoy, have options that disable outputting headers by default, but can be enabled at the users responsibility. Is this something that's been considered for Axios (removing all headers from errors by default but allowing this to be configurable)?
Reacted by Arnau González, Pan, Martin Picard (Thoughtworks), 3repeating and Zachary Wright
I still disagree, firstly you should not be logging the entire response in any use case ever, what should be done is you should log only the parts of the response that you would like to keep for traces and to identify errors. When logging the entire response you are actually creating multiple potential issues, you could be logging user data, you can be breaching GDPR and you can even be login their credentials as that request data is in there too.
Your opinion here is very short sighted. The developers aren't logging this information. Given the behavior of the JS runtime, the library is when it arbitrarily stuffs everything inside of its own custom error object. What benefit is there is in hydrating an error with the entire contents of the request and response objects?
Why is Axios even throwing an error in this instance? The idiomatic approach to handling this would be to simply return the response object and let the developer decide what to do with it. Libraries should only throw errors when they are dealing with library problems. If the request has made it all the way to the endpoint, then Axios has done its job. It's doing more work than is being asked of it by then parsing that response to see if the server returned an error code. The server response is actually none of Axios' business whatsoever.
The one caveat I can think of is when Axios is throwing an error when a timeout is exceeded. However, in that case, there is no response object to return, and the user already has all of the request information in hand - so there's still no benefit to doing more than throwing an error that simply indicates that a timeout was reached.
From my perspective, AxiosError doesn't do anything to enhance the Axios library itself. All it actually does is force developers to catch the exception and raise one of their own.
Reacted by Daniel Gomez Blanco, Arnau González, Martin Picard (Thoughtworks), b3n3d1k7, Benno M, Joey Mezzacappa, Vladislav Deryabkin, Matt Revell, Pan, Ricardo Arturo Cabral and 13 more
Would it be possible to change all of the fields which may contain sensitive data (imho this includes response.data) into private fields (#) with getters be an option?
This way given field with potentially sensitive data may still be accessed in the error object, e.g. to check for specific value in response, but it would not be written to console just by logging the AxiosError object - one would need to access and log a given private field explicitly.
The default error message should just include relatively (though not necessarily absolutely safe) details like request method, response status, request url (this one is debatable, but OWASP does recommend against putting sensitive data in urls).
As it stands any project which uses axios must also implement a custom interceptor to attempt to sanitize the exceptions coming out of axios. Which is pretty annoying, especially given axios's general "easy to use" nature.
Reacted by Jimmie Högklint, tomek3e and Zachary Wright
The problem IMO is not that it cannot be removed (that is what I do in all projects upon realizing it) but that one has to ACTIVELY prevent leakage of potentially sensitive data from an error object, instead of it being not leaked by DEFAULT.
but that one has to ACTIVELY prevent leakage of potentially sensitive data from an error object
And where can this information leak from your process? This is your process, which does nothing but what you implement there. If there is malicious code in the process, it does not matter whether the error returns meta information with access keys or not, because this code still has the same level of access to the file system and environment as yours. Meta information in an Axios error has always been an Axios feature, and it is there not just for debugging, but for implementing retries for requests and similar features that require access to the config or network transport object. Therefore, it cannot be simply removed or made private - what is the point of private properties if the user cannot get it from there?
If you do not need meta information in an Axios error, just remove it in the interceptor or inside the catch block. If you do not need output to the console or logging system, then make the properties non-enumerable in any way.
And where can this information leak from your process?
I think there are some valid cases:
Case 1
axios is used client-side in a web app (or an Electron app) that makes authenticated requests.
Errors are automatically sent to a logging backend.
This will accidentally log the client's auth tokens.
Case 2
A Node.js app is run with production secrets in a production environment.
The developer team doesn't have access to those production secrets except for a small number of admins / dev ops engineers.
An error is thrown in the app. This error is logged.
The error including the API key is written to the logs which the whole developer team has access to.
Suddenly, the private secret is leaked to people who shouldn't have access to it.
In my opinion, axios should have a safe default where no potentially sensitive information is logged, and some mechanism (e.g. a config option, or an enableExtendedLogging function) that enables the current behavior.
The default choice for any library should be the safe one in my opinion. It's hard for developers to foresee that secrets would be logged as part of error messages.
Reacted by Daniel Gomez Blanco, Kagami, thomasgerhardt0408, Pan, Cameron Hicks and Zachary Wright
Thanks for the issue, we are adding this as a potential feature to a mega-feature issue with many feature requests please see the motivation for doing this:
Activity
github-actions commentedon Nov 5, 2021
Hello! 👋
This issue is being automatically closed because it does not follow the issue template. Please read the issue template carefully and follow all of the instructions when opening a new issue.
Thanks
shawnsparks-work commentedon Aug 15, 2022
Opened a security vulnerability for this. We'll see if it gets any traction. https://huntr.dev/bounties/46857f58-7655-42e4-b0cf-8e6d824358b0
jasonsaayman commentedon Sep 5, 2022
My opinion is that this is not an issue, axios is not in charge of logging anything anywhere. All logging added is the responsibility of the implementor. You can clean the prototype or you can rather only log what you need and not the entire response.
maggie44 commentedon Sep 6, 2022
I can't think of a scenario where someone would need to get the Authorization header key returned back to them in the logs so I would suspect removing it would not create any friction in usage. Indeed it could be seen as the user responsibility, but as they are in multiple places throughout the error object it is likely at least some will miss it and then this single issue becomes multiplied on each repo using Axios (if they are lucky enough to catch it).
In short, yes this could potentially be placed on the user of the logs to manage, but would prevent user friction if it could be managed in advance.
jasonsaayman commentedon Sep 6, 2022
I still disagree, firstly you should not be logging the entire response in any use case ever, what should be done is you should log only the parts of the response that you would like to keep for traces and to identify errors. When logging the entire response you are actually creating multiple potential issues, you could be logging user data, you can be breaching GDPR and you can even be login their credentials as that request data is in there too.
Also realistically the above suggestion would not cover all use cases by a long stretch. People implement any key-value pair they want for the token sometimes, amazon re-writes the key values when under an API gateway and the list goes on so it would theoretically be impossible for us to strip all of the possible tokens and credentials from the response object.
kpkonghk01 commentedon Jan 31, 2023
In nodejs, an uncaught error would be logged out by default, in which case we really don't want the Authorization header being accidentially leaked by the library's error object.
I understand that it's developer's responsibility to keep sensitive data secret and it's not realistic for the library to strip out all sensitive data. But when the default behavior of the language is to log out the whole error object, is it better to hide some common fields that contains secrets?
nikwen commentedon May 25, 2023
I used axios in my project without thinking much about it (like most devs when they install an npm package). And now API tokens from request bodies showed up in my server logs.
This is not something that a regular users (= a developer working on a project) can predict.
Therefore, I believe AxiosError.toString() should contain less information by default, with an axios-wide option to opt into more information.
danielgblanco commentedon Oct 6, 2023
What's the current opinion on this from maintainers? The following statement (which I fully agree with)
seems to clash with this one
at least in the context of avoiding sensitive information getting leaked elsewhere. Users of Axios may not always be in control of what gets logged out if the default behaviour of the language is to print the whole error to console when it's uncaught. I'd also add that sanitising the error before calling a logging library may also miss other telemetry signals, like tracing, where auto-instrumentation libraries can capture errors within spans add errors to span events.
In general, if there is a possibility that data could expose sensitive information (as it's the case here with default logging in Javascript printing the object), the preferred approach is to sanitise it as close to the domain knowledge as possible (i.e. the Axios codebase or close to it). If Axios error objects were to change or add new fields that could contain sensitive information, the change to sanitise can come in the same version bump.
In terms of HTTP instrumentation, other libraries like opentelemetry-instrumentation-http, or Envoy, have options that disable outputting headers by default, but can be enabled at the users responsibility. Is this something that's been considered for Axios (removing all headers from errors by default but allowing this to be configurable)?
lmcarreiro commentedon Oct 11, 2023
In the company I work, we just spent some time replacing axios by node-fetch to avoid this problem.
In axios docs, there are even some recommendations to do that without any warning that this could expose credentials: https://axios-http.com/docs/handling_errors
mro-rhansen2 commentedon Mar 26, 2024
Your opinion here is very short sighted. The developers aren't logging this information. Given the behavior of the JS runtime, the library is when it arbitrarily stuffs everything inside of its own custom error object. What benefit is there is in hydrating an error with the entire contents of the request and response objects?
Why is Axios even throwing an error in this instance? The idiomatic approach to handling this would be to simply return the response object and let the developer decide what to do with it. Libraries should only throw errors when they are dealing with library problems. If the request has made it all the way to the endpoint, then Axios has done its job. It's doing more work than is being asked of it by then parsing that response to see if the server returned an error code. The server response is actually none of Axios' business whatsoever.
The one caveat I can think of is when Axios is throwing an error when a timeout is exceeded. However, in that case, there is no response object to return, and the user already has all of the request information in hand - so there's still no benefit to doing more than throwing an error that simply indicates that a timeout was reached.
From my perspective, AxiosError doesn't do anything to enhance the Axios library itself. All it actually does is force developers to catch the exception and raise one of their own.
tomek3e commentedon Nov 6, 2024
Would it be possible to change all of the fields which may contain sensitive data (imho this includes response.data) into private fields (#) with getters be an option?
This way given field with potentially sensitive data may still be accessed in the error object, e.g. to check for specific value in response, but it would not be written to console just by logging the AxiosError object - one would need to access and log a given private field explicitly.
3repeating commentedon Mar 26, 2025
The current behavior is an unreasonable default behavior. Logging of errors is expected and common behavior (and is even suggested in multiple places in the axios docs: https://axios-http.com/docs/handling_errors https://axios-http.com/docs/example https://axios-http.com/docs/post_example https://axios-http.com/docs/res_schema). Giving the authors of axios recognize logging request/response bodies and headers is not a good idea this information should not be part of the default error toString. Instead, it should be hidden behind some other call like "getFullDetails()".
The default error message should just include relatively (though not necessarily absolutely safe) details like request method, response status, request url (this one is debatable, but OWASP does recommend against putting sensitive data in urls).
As it stands any project which uses axios must also implement a custom interceptor to attempt to sanitize the exceptions coming out of axios. Which is pretty annoying, especially given axios's general "easy to use" nature.
DigitalBrainJS commentedon Mar 27, 2025
What's stopping you from simply removing unnecessary fields from the error object rendering?
If needed, I think we can add a similar logic to the core without much risk of breaking changes if the community really needs it.
tomek3e commentedon Mar 28, 2025
The problem IMO is not that it cannot be removed (that is what I do in all projects upon realizing it) but that one has to ACTIVELY prevent leakage of potentially sensitive data from an error object, instead of it being not leaked by DEFAULT.
DigitalBrainJS commentedon Mar 28, 2025
And where can this information leak from your process? This is your process, which does nothing but what you implement there. If there is malicious code in the process, it does not matter whether the error returns meta information with access keys or not, because this code still has the same level of access to the file system and environment as yours. Meta information in an Axios error has always been an Axios feature, and it is there not just for debugging, but for implementing retries for requests and similar features that require access to the config or network transport object. Therefore, it cannot be simply removed or made private - what is the point of private properties if the user cannot get it from there?
If you do not need meta information in an Axios error, just remove it in the interceptor or inside the catch block. If you do not need output to the console or logging system, then make the properties non-enumerable in any way.
nikwen commentedon Mar 28, 2025
I think there are some valid cases:
Case 1
Case 2
In my opinion, axios should have a safe default where no potentially sensitive information is logged, and some mechanism (e.g. a config option, or an
enableExtendedLoggingfunction) that enables the current behavior.The default choice for any library should be the safe one in my opinion. It's hard for developers to foresee that secrets would be logged as part of error messages.
jasonsaayman commentedon Jul 30, 2025
Thanks for the issue, we are adding this as a potential feature to a mega-feature issue with many feature requests please see the motivation for doing this:
#6983
And the mega-feature issue here:
#6984