Loopback: Setting cors:true in config.json throws exception

Created on 11 Nov 2015  ยท  18Comments  ยท  Source: strongloop/loopback

node_modules\cors\libindex.js:188
o.origin = defaults.origin;
^
TypeError: Cannot assign to read only property 'origin' of true
at middlewareWrapper (node_modules\cors\libindex.js:188:16)
at setupCors (node_modulesloopback-explorer\lib\swagger.js:145:18)
[..]

"loopback-boot": "^2.6.5",
"loopback-component-passport": "^1.5.0",
"loopback-connector-mongodb": "1.13.0",
"loopback-datasource-juggler": "^2.41.1",
blocked triaging

Most helpful comment

in config.json, instead of setting cors to true, you can set ;
"cors": { "origin": true },
which will make the app run fine. you can still set the value of methods and preflightContinue but it is not necessary in config.json.
can you guys try it in your app?
Thanks.

All 18 comments

so what led to that error? can you provide the steps in order to reproduce the error?
Any repo to be used as well to reproduce the error? thanks.

Simply node . started the app and it threw error right away. Only this parameter was changed in the config file. Here is the full stack trace
TypeError: Cannot assign to read only property 'origin' of true
at middlewareWrapper (\testnode_modules\cors\libindex.js:188:16)
at setupCors (\testnode_modulesloopback-explorer\lib\swagger.js:145:18)
at Swagger (\testnode_modulesloopback-explorer\lib\swagger.js:55:3)
at explorer (\testnode_modulesloopback-explorerindex.js:31:3)
at Object.mountLoopBackExplorer as func
at \testnode_modulesloopback-boot\lib\executor.js:287:9
at iterate (\testnode_modulesloopback-bootnode_modules\async\lib\async.js:146:13)
at \testnode_modulesloopback-bootnode_modules\async\lib\async.js:157:25
at \testnode_modulesloopback-boot\lib\executor.js:289:7
at iterate (\testnode_modulesloopback-bootnode_modules\async\lib\async.js:146:13)

Additionally for which I am confused in the middleware.json cors is enabled
"cors": {
"params": {
"origin": true,
"credentials": true,
"maxAge": 86400
}

i used a sample project and I could reproduce the same error when I do node .

for lines 188 and 191
if you use

o.origin == defaults.origin;

instead of

o.origin = defaults.origin;

and in line 194, if you use

o.preflightContinue == defaults.preflightContinue;

instead of

o.preflightContinue = defaults.preflightContinue;

That should fix the issue in your project and run the node server fine. If that doesn't work, can you share your project repo? Thanks.

@barocsi Still having an issue with that?
Thanks

Please let me know if the problem is still persisting .. we will re-open the ticket.
closes #1810

I am having this issue as well and editing the package directly doesn't feel like a permanent solution... What do you need to help debugging ?

I am having the same issue. I can't set cors: true in the config.json

.../strongloop-app/node_modules/cors/lib/index.js:188
      o.origin = defaults.origin;
               ^

TypeError: Cannot assign to read only property 'origin' of true

@kartsims @moklick

Hey @loay,

sorry the fix doesn't work for me.. that is nothing I can do in a production app.
You can easily reproduce it by creating a new project:

slc loopback
... create empty-server
... set cors: true in config.json
node . 

Same here, that is not an acceptable solution to the issue... Doesn't look "fixed" if it needs such a hack.

I did as @moklick said and did get the error :

/Users/simon/dev/loopback/loopback-issue-1810/node_modules/cors/lib/index.js:188
      o.origin = defaults.origin;
               ^

TypeError: Cannot assign to read only property 'origin' of true
    at middlewareWrapper (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/cors/lib/index.js:188:16)
    at setupCors (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-component-explorer/index.js:123:18)
    at mountSwagger (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-component-explorer/index.js:111:3)
    at routes (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-component-explorer/index.js:47:3)
    at explorer (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-component-explorer/index.js:26:46)
    at /Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-boot/lib/executor.js:398:5
    at Array.forEach (native)
    at setupComponents (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-boot/lib/executor.js:394:27)
    at execute (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-boot/lib/executor.js:35:3)
    at bootLoopBackApp (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-boot/index.js:140:3)

I uploaded the code I got, just in case, here : https://github.com/kartsims/loopback-issue-1810

Also I am running the code on OSX with the following versions

MacBook-Air:~ simon$ node -v
v5.4.1
MacBook-Air:~ simon$ slc -v
strongloop v6.0.0 (node v5.4.1)
โ”œโ”€โ”€ [email protected] (fb756f2)
โ”œโ”€โ”€ [email protected] (47dd24a)
โ”œโ”€โ”€ [email protected] (be6180a)
โ”œโ”€โ”€ [email protected] (62e539b)
โ”œโ”€โ”€ [email protected] (4197516)
โ”œโ”€โ”€ [email protected] (f46e58f)
โ”œโ”€โ”€ [email protected] (1327018)
โ”œโ”€โ”ฌ [email protected] (1e39220)
โ”‚ โ””โ”€โ”€ [email protected] (4ea7ee9)
โ”œโ”€โ”€ [email protected] (6440776)
โ”œโ”€โ”€ [email protected]
โ””โ”€โ”€ [email protected]

MacBook-Air:~ simon$ npm -v
3.5.3

Thanks. Having a look. Re-opened issue.

in config.json, instead of setting cors to true, you can set ;
"cors": { "origin": true },
which will make the app run fine. you can still set the value of methods and preflightContinue but it is not necessary in config.json.
can you guys try it in your app?
Thanks.

Works for me :sparkles:

After looking back into the documentation I didn't find where I got the idea that setting it to true would do the trick but it has to be in there, somewhere, I don't think I made this up! If I ever find it again, I'll let you know.

Thanks for the reply @loay

Glad I can help :)

Now this is a good fix. Thanks a lot @loay

I had same error but that was because my jenkin server was already running on 8080 and My node server also trying to create server using 8080 port please check could be same for you too.

this changed :(

  throw new Error(g.f(
    ^

Error: The REST adapter no longer comes with a built-in CORS middleware, the config option "remoting.cors" is no longer available.See https://docs.strongloop.com/display/public/LB/Security+considerations for more details.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Overdrivr picture Overdrivr  ยท  3Comments

htmlauthor picture htmlauthor  ยท  3Comments

Overdrivr picture Overdrivr  ยท  4Comments

bajtos picture bajtos  ยท  3Comments

bryannaegele picture bryannaegele  ยท  4Comments