question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

instance.defaults changes global object

See original GitHub issue

If I have two instances of axios created with axios.create and I set a default header on one, it also changes it on all the other ones in existence.

var instance1 = this.oauthService = axios.create({});
var instance2 = this.oauthService = axios.create({});

instance2.defaults.headers.common['Authorization'] = 'askdjfaksdjf';

Both instance1 and 2 now have the Authorization header changed. I believe the problem is that the util.merge operation doesn’t do deep merging, though as you see below, only if the result[key] and val are both objects, does it do a recursive merge, otherwise it assigns the value by reference.

function merge(/\* obj1, obj2, obj3, ... */) {
  var result = {};
  function assignValue(val, key) {
    if (typeof result[key] === 'object' && typeof val === 'object') {
      result[key] = merge(result[key], val);
    } else {
      result[key] = val;
    }
  }

  for (var i = 0, l = arguments.length; i < l; i++) {
    forEach(arguments[i], assignValue);
  }
  return result;
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:23
  • Comments:32 (11 by maintainers)

github_iconTop GitHub Comments

26reactions
marbemaccommented, Oct 25, 2017

Using Axios in a server side rendering situation (next.js, meteor, any SSR focused library) leads to a huge security issue when using defaults.headers to set the currently logged in user token… this should be an urgent and critical bug IMHO. We only just discovered this, crazy its been open for over a year 😕.

13reactions
jonjaquescommented, Oct 6, 2016

I was able to work around this by adding a request interceptor. I have a custom class wrapping axios, so something like this:

    this.client.interceptors.request.use(request => {
      request.headers['Authorization'] = `Bearer ${this.token}`
      return request
    })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Global object - The Modern JavaScript Tutorial
The global object provides variables and functions that are available anywhere. By default, those that are built into the language or the ...
Read more >
c++ - How are local and global variables initialized by default?
Adding a default constructor changes how instances of A are initialized. Now both global_a and local_a are initialized by the default ...
Read more >
globalThis - JavaScript - MDN Web Docs
The globalThis property provides a standard way of accessing the global this value (and hence the global object itself) across environments.
Read more >
You Can Definitely Use Global Variables To Manage Global ...
Below is an example showing this problem. import React from 'react'; // use global variable to store global state let count = 0; ......
Read more >
User and Workspace Settings - Visual Studio Code
User Settings - Settings that apply globally to any instance of VS Code you open. ... Changes to settings are applied by VS...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found