forked from bogeeee/restfuncs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
executable file
·133 lines (107 loc) · 4.57 KB
/
Copy pathserver.ts
File metadata and controls
executable file
·133 lines (107 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import express, {Express} from "express"
import {createServer} from "vite"
import {ServerSessionOptions, ServerSession, restfuncsExpress} from "restfuncs-server"
import {MainframeService} from "./MainframeService.js"
import session from "express-session";
import crypto from "node:crypto";
import {TestsService} from "./TestsService.js";
import {ControlService} from "./ControlService.js";
import helmet from "helmet"
(async () => {
{
// *** Main site: ****
const port = 3000 // Adjust this in clientTests.ts also
const app = restfuncsExpress()
const commonOptions: ServerSessionOptions = {
devDisableSecurity: false,
exposeErrors: true,
logErrors: false
}
MainframeService.options = commonOptions;
TestsService.options = commonOptions;
//app.use(helmet({referrerPolicy: {policy: "strict-origin-when-cross-origin"}})); // Test that this does not corrupt stuff, also see below in front of the served web content
app.use("/MainframeService", MainframeService.createExpressHandler());
app.use("/TestsService", TestsService.createExpressHandler());
class AllowedTestsService extends TestsService {
static options = {...commonOptions, allowedOrigins: ["http://localhost:3666"]}
}
app.use("/AllowedTestsService", AllowedTestsService.createExpressHandler());
class ForceTokenCheckService extends TestsService {
static options = { ...commonOptions, devForceTokenCheck: true }
}
app.use("/ForceTokenCheckService", ForceTokenCheckService.createExpressHandler());
class AllowedForceTokenCheckService extends TestsService {
static options = { ...commonOptions, allowedOrigins: ["http://localhost:3666"], devForceTokenCheck: true }
}
app.use("/AllowedForceTokenCheckService", AllowedForceTokenCheckService.createExpressHandler());
app.use("/ControlService", ControlService.createExpressHandler())
// Pretend the browser does not send an origin:
class TestsService_eraseOrigin extends TestsService {
static options: ServerSessionOptions = {...commonOptions}
}
app.use("/TestsService_eraseOrigin", eraseOrigin, TestsService_eraseOrigin.createExpressHandler())
// Pretend the browser does not send an origin:
class AllowedTestsService_eraseOrigin extends TestsService {
static options: ServerSessionOptions = {...commonOptions, allowedOrigins: ["http://localhost:3666"]}
}
app.use("/AllowedTestsService_eraseOrigin", eraseOrigin, AllowedTestsService_eraseOrigin.createExpressHandler())
await serveClientWeb(app,4000);
app.listen(port)
console.log("Main app running on: http://localhost:" + port)
}
// ***** Foreign site: ****
{
const port = 3666
const app = express()
await serveClientWeb(app, 4666);
app.listen(port)
console.log("Foreign site app (some services CORS allowed to it) running on: http://localhost:" + port)
}
// ***** Evil site2: ****
{
const port = 3667
const app = express()
await serveClientWeb(app, 4667);
app.listen(port)
console.log("Evil site app (no services CORS allowed to it) running on: http://localhost:" + port)
}
})()
function eraseOrigin (req: any, res: any, next: any) {
if(req.method !== "OPTIONS") { // Only erase for regular methods, cause we want to test if the preflight works properly
req.headers.origin = undefined
req.headers.referer = undefined
if (req.header("Origin")) {
throw "Unexpected"
}
}
next()
};
async function serveClientWeb(app: Express, hmrPort: number) {
/*
// Test with helmet, that this does not corrupt stuff:
app.use(helmet({
contentSecurityPolicy: {directives: {
"connect-src": "*"
}},
referrerPolicy: {policy: "strict-origin-when-cross-origin"
}}));
*/
// Client web:
if (process.env.NODE_ENV === 'development') {
// Serve client web through vite dev server:
const viteDevServer = await createServer({
cacheDir: `.vite/${hmrPort}`,
server: {
hmr: {
port: hmrPort
},
middlewareMode: true
},
root: "client",
base: "/",
});
app.use(viteDevServer.middlewares)
} else {
app.use(express.static('client/dist')) // Serve pre-built web (npm run build)
}
}