Open mihaa1 opened 5 years ago
@mihaa1 Thanks for posting! We'll take a look as soon as possible.
In the mean time, there are a few ways you can help speed things along:
Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.
For help with questions about Sails, click here.
Hi @mihaa1 I was looking into your issue but I need a little more info. Can you please provide your config/security.js
to see how your CORS is set up?
Here is the official docs on CORS if that is helpful.
@raqem thanks for the response.
Here is my security.js file: cors: { allRoutes: true, allowOrigins: ['http://localhost:4200'], allowCredentials: true, }, csrf: true
Hmm, different port with cookie? Typically an SSL cookie (443) cannot be read by a port 80 request as the 443 is designated as secure. As per the RFC https://tools.ietf.org/html/rfc6265, the behavior you "want" to occur is considered insecure and I consider the Sails framework to be secure with its implementation. Meaning, you are breaking the "8.5" confidentiality clause of the RFC. I consider this to be an as-expected behavior - not a bug.
@crh3675 so what I understand from you - is there is no way using the built in auth functionality in sails to authenticate a client which is hosted on another domain?
What do you suggest I do in this situation? Move to JWT perhaps?
What I typically do is use an
@crh3675 with this solution, the app would have to communicate with the iframe via post messages, and set the cookie manually - am I correct?
Been a while since using Angular so my example might be old but from the Angular code, you need to first request the "csrf" token from Sails. https://sailsjs.com/documentation/concepts/security/csrf
$http.get('http://localhost:1337/csrfToken').then(function(res) {
var csrf = res.body._csrf;
// target attribute says to operate in our <iframe>
var form = '<form method="post" target="auth" id="login" action="http://localhost:1337/authendpoint">';
form += '<input type="hidden" value="' + _csrf + '" name="_csrf">';
form += '<input type="hidden" value="username" name="user">';
form += '<input type="hidden" value="password" name="pass">';
form += '</form>
form += '<iframe name="auth"></iframe>';
var div = document.createElement('div');
div.innerHTML = form;
document.body.appendChild(div);
document.getElementById('login').submit();
setTimeout(function() {
div.parentNode.removeChild(div);
}, 150);
});
}
You need the
@mihaa1 any resolution? I think that example should give some guidance.
@crh3675 thanks! Seems good. Haven't got to it yet. Will try asap
Is there any new info on this? I have a very similar issue with the CORS settings on my application and am really hitting a wall with it. One thing I have noticed is that preflight requests work as expected and return 200. However, the associated GET (or other method) of the preflight gets blocked by CORS and returns 400. Thanks friends :)!
Are these cross-domain requests @colinmetcalf ? Any code samples you can provide?
Hello! Are there any news on this? I'm trying to build a separate front-end (using Nuxt.js), while using Sails for the backend through API's.
And that means that all comunications are through CORS, including login.
I would like to login through CORS, but for some reason Sails appears to not be receiving the Cookie request header? I will share here what I can with my debugging / personal case so far:
< Keep in mind that up to this point, the client browser has no cookies > < I'm running Sails server on port 1337 and my front-end app on port 3000 >
I call my Sails app for the first time, using a "login" API, that includes: < I don't know if this has anything to do: but I'm using a Controller for this function. Not an action2 >
req.session._ID_STORE = oExistingStore.id;
sails.log('Session _ID_STORE=', req.session._ID_STORE);
return res.status(200).json();
And I can see the following log on my console: "Session _ID_STORE= 5dd1cb201dc1718698ba6418"
Then I go to my mongodb sessions table and I, indeed, see a new session:
I then proceed to make a new API call, this new API call uses the "req.session._ID_STORE" I set up in the previous call:
const _ID_STORE = this.req.session._ID_STORE;
sails.log(`In view-orders: _ID_STORE="${_ID_STORE}"`);
But the log I see in the console returns: In view-orders: _ID_STORE="undefined" (And it throws a server error because that ID is needed)
And, back into the "sessions" table of mongodb: another session has been created upon this second API call:
It does look like the Cookie wasn't sent OR received on the second API call
To make sure that it has been sent to the server: I go to Chrome dev tools > Network > The second API Call and I see this:
Which makes me think that the cookie was indeed sent. But not properly received by the server / middleware / I don't know.
I (think) I did turn the appropiate cors options in the security file (below), so I'm pretty lost on what to do from here.
security file:
cors: {
allRoutes: true,
allowCredentials: true, // Allows cookies and session through CORS from here
allowOrigins: ['http://localhost:3000','http://localhost', 'http://127.0.0.1:3000', 'http://127.0.0.1'], // Allows these origins through CORS
allowResponseHeaders: 'set-cookie',
allowRequestHeaders: 'content-type,cookie,Cookie' // I don't think this is necessary but I'm going crazy
},
csrf: true
package.json:
"@sailshq/lodash": "^3.10.3",
"@sailshq/socket.io-redis": "^5.2.0",
"connect-mongo": "1.1.0",
"request-promise": "^4.2.5",
"sails": "^1.1.0",
"sails-hook-apianalytics": "^2.0.3",
"sails-hook-cron": "^3.0.1",
"sails-hook-organics": "^1.0.0",
"sails-hook-orm": "^2.1.1",
"sails-hook-sockets": "^2.0.0",
"sails-mongo": "^1.0.1",
session file:
secret: 'mydevsecret',
adapter: 'connect-mongo',
url: 'mongodb://localhost:27017/mydb',
And I'm running on windows, node v12.16.1.
Any help is desperetly needed and apreciated!
Hi! so I ran into the same issue and this is my solution
// IMPORTANT: the key is in setting credentials to 'include' and
// that will set cookies for your next request
let _csrf = '';
fetch(BACKEND_BASE_URL + '/csrfToken', { credentials: 'include' })
.then(response => response.json())
.then(data => {
_csrf = data._csrf
})```
I made a socket post request
let params = {}
params.append('_csrf', _csrf)
io.socket.post('/article', params , (body, JWR) => {
if (JWR.statusCode == 200) return console.log('Everything is good')
})
It worked for me!
Node version: 10.15.0 Sails version (sails):1.1.0 ORM hook version (sails-hook-orm):2.1.1 Sockets hook version (sails-hook-sockets): 1.5.5 Organics hook version (sails-hook-organics): 0.15.0 Grunt hook version (sails-hook-grunt): 3.1.0 Uploads hook version (sails-hook-uploads):? DB adapter & version : sails-postgresql@1.0.2 Skipper adapter & version : skipper-disk@0.5.6
I have an angular SPA with a Sails backend. The angular app is served from a separate port than the sails app (sails is runnnig on 1337 and angular on 4200). I am trying to do a login request, and save the session cookie. This works if I host the client app on the same port, but if I make the request from 4200 the cookie isn't registered automatically.
Tried enabling CORS, with credentials. Also tried to "allowResponseHeaders" - and set the cookie manually but with no luck.
What am I missing? Is this something on the client side? Do i need to enable something else in the sails configuration?