Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cors with credentials fix #934

Merged
merged 5 commits into from
Mar 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,11 @@ export default class HttpServer implements ProtocolServer {
});

// Handle requests where the path is correct and the HTTP method is not allowed.
function respondUnallowedMethod(res: http.ServerResponse, allowed: string): void {
function respondUnallowedMethod(
res: http.ServerResponse,
allowed: string,
corsPreflightWithCredentials = false
): void {
// Always allow OPTIONS to handle CORS pre-flight requests
if (!allowed.includes("OPTIONS")) {
allowed += ", OPTIONS";
Expand All @@ -620,6 +624,12 @@ export default class HttpServer implements ProtocolServer {
req.socket.remoteAddress
)}:${req.socket.remotePort}`
);
if (corsPreflightWithCredentials) {
res.setHeader("Access-Control-Allow-Origin", req.headers.origin);
res.setHeader("Access-Control-Allow-Credentials", "true");
} else {
res.setHeader("Access-Control-Allow-Origin", "*");
}
res.setHeader("Access-Control-Allow-Methods", allowed);
res.setHeader("Access-Control-Allow-Headers", "content-type, authorization, *");
res.writeHead(200);
Expand All @@ -632,7 +642,12 @@ export default class HttpServer implements ProtocolServer {
}

// Set CORS headers
res.setHeader("Access-Control-Allow-Origin", "*");
if (this.httpSecurityScheme !== "NoSec" && req.headers.origin) {
res.setHeader("Access-Control-Allow-Origin", req.headers.origin);
res.setHeader("Access-Control-Allow-Credentials", "true");
} else {
res.setHeader("Access-Control-Allow-Origin", "*");
}

const contentTypeHeader: string | string[] = req.headers["content-type"];
let contentType: string = Array.isArray(contentTypeHeader) ? contentTypeHeader[0] : contentTypeHeader;
Expand Down Expand Up @@ -722,12 +737,17 @@ export default class HttpServer implements ProtocolServer {
// resource found and response sent
return;
} else {
let corsPreflightWithCredentials = false;
// Thing Interaction - Access Control
if (this.httpSecurityScheme !== "NoSec" && !(await this.checkCredentials(thing, req))) {
res.setHeader("WWW-Authenticate", `${this.httpSecurityScheme} realm="${thing.id}"`);
res.writeHead(401);
res.end();
return;
if (req.method === "OPTIONS" && req.headers.origin) {
corsPreflightWithCredentials = true;
} else {
res.setHeader("WWW-Authenticate", `${this.httpSecurityScheme} realm="${thing.id}"`);
res.writeHead(401);
res.end();
return;
}
}

if (segments[2] === this.PROPERTY_DIR) {
Expand Down Expand Up @@ -760,7 +780,9 @@ export default class HttpServer implements ProtocolServer {
res.writeHead(202);
res.end();
} else {
respondUnallowedMethod(res, "GET");
// may have been OPTIONS that failed the credentials check
// as a result, we pass corsPreflightWithCredentials
respondUnallowedMethod(res, "GET", corsPreflightWithCredentials);
}
// resource found and response sent
return;
Expand Down Expand Up @@ -864,7 +886,9 @@ export default class HttpServer implements ProtocolServer {
res.end();
return;
} else {
respondUnallowedMethod(res, "GET, PUT");
// may have been OPTIONS that failed the credentials check
// as a result, we pass corsPreflightWithCredentials
respondUnallowedMethod(res, "GET, PUT", corsPreflightWithCredentials);
return;
} // Property exists?
}
Expand Down Expand Up @@ -914,7 +938,9 @@ export default class HttpServer implements ProtocolServer {
res.end(err.message);
}
} else {
respondUnallowedMethod(res, "POST");
// may have been OPTIONS that failed the credentials check
// as a result, we pass corsPreflightWithCredentials
respondUnallowedMethod(res, "POST", corsPreflightWithCredentials);
}
// resource found and response sent
return;
Expand Down Expand Up @@ -975,7 +1001,9 @@ export default class HttpServer implements ProtocolServer {
res.writeHead(202);
res.end();
} else {
respondUnallowedMethod(res, "GET");
// may have been OPTIONS that failed the credentials check
// as a result, we pass corsPreflightWithCredentials
respondUnallowedMethod(res, "GET", corsPreflightWithCredentials);
}
// resource found and response sent
return;
Expand Down