-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1478 from SignalK/fix-cors
fix: CORS: security routes and origin handling
- Loading branch information
Showing
7 changed files
with
129 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Login form</title> | ||
</head> | ||
|
||
<body> | ||
<form id="loginForm"> | ||
<input type="text" id="username" /><br /> | ||
<input type="password" id="password" /><br /> | ||
<button type="submit">Submit</button> | ||
<p id="result"></p> | ||
</form> | ||
</body> | ||
<script> | ||
window.onload = function () { | ||
document.getElementById('loginForm').onsubmit = function () { | ||
document.getElementById('result').innerHTML = '' | ||
const username = document.getElementById('username').value | ||
const password = document.getElementById('password').value | ||
const body = JSON.stringify({ username, password }) | ||
fetch('http://localhost:3000/signalk/v1/auth/login', { | ||
method: 'POST', | ||
withCredentials: true, | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
}, | ||
body | ||
}) | ||
.then(r => r.json()) | ||
.then(r => { | ||
document.getElementById('result').innerHTML = JSON.stringify(r) | ||
}) | ||
return false; | ||
} | ||
} | ||
</script> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { IRouter } from 'express' | ||
import { createDebug } from './debug' | ||
import { SecurityConfig } from './security' | ||
import cors, { CorsOptions } from 'cors' | ||
|
||
type OriginCallback = (err: Error | null, origin?: string) => void | ||
|
||
export function setupCors( | ||
app: IRouter, | ||
{ allowedCorsOrigins }: SecurityConfig | ||
) { | ||
const corsDebug = createDebug('signalk-server:cors') | ||
|
||
const corsOrigins = allowedCorsOrigins | ||
? allowedCorsOrigins | ||
.split(',') | ||
.map((s: string) => s.trim().replace(/\/*$/, '')) | ||
: [] | ||
corsDebug(`corsOrigins:${corsOrigins.toString()}`) | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const corsOptions: CorsOptions = { | ||
credentials: true | ||
} | ||
if (corsOrigins.length) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
corsOptions.origin = (origin: string | undefined, cb: OriginCallback) => { | ||
if (origin === undefined || corsOrigins.indexOf(origin) >= 0) { | ||
corsDebug(`${origin} OK`) | ||
cb(null, origin) | ||
} else { | ||
const errorMsg = `${origin} not allowed` | ||
corsDebug(errorMsg) | ||
cb(new Error(errorMsg)) | ||
} | ||
} | ||
} | ||
app.use(cors(corsOptions)) | ||
} | ||
|
||
export const handleAdminUICORSOrigin = ( | ||
securityConfig: SecurityConfig & { adminUIOrigin: string } | ||
) => { | ||
let allowedCorsOrigins: string[] = [] | ||
if ( | ||
securityConfig.adminUIOrigin && | ||
securityConfig.allowedCorsOrigins && | ||
securityConfig.allowedCorsOrigins.length > 0 | ||
) { | ||
allowedCorsOrigins = securityConfig.allowedCorsOrigins?.split(',') | ||
if (allowedCorsOrigins.indexOf(securityConfig.adminUIOrigin) === -1) { | ||
allowedCorsOrigins.push(securityConfig.adminUIOrigin) | ||
} | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { adminUIOrigin, ...configWithoutAdminUIOrigin } = securityConfig | ||
return { | ||
...configWithoutAdminUIOrigin, | ||
allowedCorsOrigins: allowedCorsOrigins.join(',') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters