Skip to content

Remove API key from headers in error message  #4249

Closed

Description

@maggie44

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:

obj.config.headers.Authorization
obj.request._header
obj.response.request._header
obj.request._currentRequest._header

Likely more, but have stopped looking.

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]

Activity

  1. github-actions commented on Nov 5, 2021

    @github-actions
    Contributor

    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

  2. shawnsparks-work commented on Aug 15, 2022

    @shawnsparks-work
  3. jasonsaayman commented on Sep 5, 2022

    @jasonsaayman
    Member

    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.

  4. self-assigned this
    on Sep 5, 2022
  5. maggie44 commented on Sep 6, 2022

    @maggie44
    Author

    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.

  6. jasonsaayman commented on Sep 6, 2022

    @jasonsaayman
    Member

    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.

  7. kpkonghk01 commented on Jan 31, 2023

    @kpkonghk01

    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?

  8. nikwen commented on May 25, 2023

    @nikwen

    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.

  9. danielgblanco commented on Oct 6, 2023

    @danielgblanco

    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)?

  10. lmcarreiro commented on Oct 11, 2023

    @lmcarreiro

    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

  11. mro-rhansen2 commented on Mar 26, 2024

    @mro-rhansen2

    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.

  12. removed their assignment
    on Sep 26, 2024
  13. tomek3e commented on Nov 6, 2024

    @tomek3e

    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.

  14. 3repeating commented on Mar 26, 2025

    @3repeating

    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.

  15. DigitalBrainJS commented on Mar 27, 2025

    @DigitalBrainJS
    Collaborator

    What's stopping you from simply removing unnecessary fields from the error object rendering?

    const isProduction = process.env.NODE_ENV !== 'development';
    
    isProduction && axios.interceptors.response.use(null, (err) => {
      return Promise.reject(Object.assign(Object.create(err), {code: err.code}));
    })

    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.

  16. tomek3e commented on Mar 28, 2025

    @tomek3e

    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.

  17. DigitalBrainJS commented on Mar 28, 2025

    @DigitalBrainJS
    Collaborator

    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.

  18. nikwen commented on Mar 28, 2025

    @nikwen

    And where can this information leak from your process?

    I think there are some valid cases:

    Case 1

    1. axios is used client-side in a web app (or an Electron app) that makes authenticated requests.
    2. Errors are automatically sent to a logging backend.
    3. This will accidentally log the client's auth tokens.

    Case 2

    1. A Node.js app is run with production secrets in a production environment.
    2. The developer team doesn't have access to those production secrets except for a small number of admins / dev ops engineers.
    3. An error is thrown in the app. This error is logged.
    4. The error including the API key is written to the logs which the whole developer team has access to.
    5. 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.

  19. jasonsaayman commented on Jul 30, 2025

    @jasonsaayman
    Member

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions