connect-cors icon indicating copy to clipboard operation
connect-cors copied to clipboard

Input origins object is modified by library to RegExp object

Open mfrobben opened this issue 12 years ago • 0 comments

Hi, appreciate the library! We're using it with success, but one issue - the way you're using the input config object, config.origins is an input array that you're modifying by replacing the input strings with a RegExp object.

This would be fine except array objects are pass by reference, so if multiple node apps are sharing the same CORS settings object (we're doing this during unit tests)...once the next app tries to send the same settings object back into your library (where some of the strings have become RegExp objects), it results in this error:

TypeError: Object /^http:\/\/localhost:4003(:|$)/ has no method 'toLowerCase'
      at origin (/Users/mrobben/devproj/sold-backend/node_modules/connect-xcors/connect-cors.js:121:36)
      at Array.forEach (native)
      at create (/Users/mrobben/devproj/sold-backend/node_modules/connect-xcors/connect-cors.js:119:22)

I'm happy to fix this for you in one of 3 ways -

  1. duplicate the input settings object so you're always using your own object rather than what's passed
  2. use a different var for the regexp table of origins
  3. document this behavior so other people don't make the mistake of sending shared settings objects in expecting they won't get modified by the lib.

mfrobben avatar Aug 04 '13 18:08 mfrobben