He tenido algunos problemas con mi servidor node.js. Lo lancé usando heroku y ahora quiero que mi extensión de Chrome envíe solicitudes http al servidor (usando cookies).
Estoy usando express.js como backend y tengo problemas para configurar CORS de la manera correcta.
Esto es lo que tengo en app.js:
if (process.env.NODE_ENV !== 'production') { app.use(cors({ credentials: true, origin: ['http://localhost:4200', 'http://localhost:8000', 'chrome-extension://kdfekbilmpafgiocanhihiogknfilcio'] })); } else { app.use(cors({ credentials: true, origin: ['chrome-extension://kdfekbilmpafgiocanhihiogknfilcio'] })); } app.use(session({ secret: 'angular shhh secret auth', resave: true, saveUninitialized: true, cookie : { httpOnly: true, expires: new Date(253402300000000) } }));y estoy haciendo solicitudes de mi extensión de Chrome (cliente) como esta:
signUp(user) { const options = { withCredentials: true }; return this.myHttp.post(`${this.BASE_URL}/signup`, user, options) .toPromise() .then((result) => { result.json() }); }Y este es mi error:
XMLHttpRequest cannot load https://moodflowextension.herokuapp.com/loggedin. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.Sé que debería pasar la cookie en el encabezado de alguna manera...
Las sesiones y las credenciales requieren una mayor seguridad en la forma de no tener comodines en su encabezado.
Afortunadamente, hay una forma de evitar esto.
var whitelist = [/yourDomain/]; var corsOptions = { origin: whitelist }; app.use(cors(corsOptions));
Esto hará que CORS solo permita orígenes que contengan su Dominio (que es una expresión regular por cierto, no una cadena), sin interrumpir las credenciales y las sesiones.