Skip to content

Commit

Permalink
fix: 支持使用环境变量控制cors的origin #6948
Browse files Browse the repository at this point in the history
  • Loading branch information
baozhoutao committed Aug 10, 2024
1 parent 1b2dd79 commit 40ef460
Show file tree
Hide file tree
Showing 16 changed files with 6,198 additions and 5,084 deletions.
31 changes: 30 additions & 1 deletion creator/app/cors/server/cors.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
# Adding CORS headers so we can use CDNs for static content
cors = require("cors");
parseOrigin = (originEnv) ->
if originEnv is 'true'
true
else if originEnv is 'false'
false
else if /^\/.*\/$/.test(originEnv)
new RegExp(originEnv.slice(1, -1)) # 去掉两边的斜杠
else if originEnv.startsWith('[') && originEnv.endsWith(']')
originsArray = JSON.parse(originEnv)
originsArray.map (item) ->
if typeof item is 'string'
item
else if /^\/.*\/$/.test(item)
new RegExp(item.slice(1, -1))
else
throw new Error 'Invalid origin value in array'
else if typeof originEnv is 'string'
originEnv
else
throw new Error 'Invalid origin value'

originEnv = process.env.STEEDOS_CORS_ORIGIN
origin = true
try
origin = parseOrigin(originEnv)
console.log 'Parsed origin:', origin
catch error
console.error 'Error parsing origin:', error.message


# Try to parse all request bodies as JSON
WebApp.rawConnectHandlers.use (req, res, next) ->
Expand Down Expand Up @@ -27,7 +56,7 @@ WebApp.rawConnectHandlers.use (req, res, next) ->
req._body = true
next()

WebApp.rawConnectHandlers.use(cors({origin: true, credentials: true}));
WebApp.rawConnectHandlers.use(cors({origin: origin, credentials: true}));

WebApp.rawConnectHandlers.use (req, res, next) ->
#if /^\/(api|_timesync|sockjs|tap-i18n)(\/|$)/.test req.url
Expand Down
8,260 changes: 4,132 additions & 4,128 deletions creator/yarn.lock

Large diffs are not rendered by default.

42 changes: 39 additions & 3 deletions packages/router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,49 @@
* @Author: [email protected]
* @Date: 2022-11-15 14:48:43
* @LastEditors: [email protected]
* @LastEditTime: 2022-11-17 11:15:40
* @LastEditTime: 2024-08-10 15:25:45
* @Description:
*/
const express = require("express");
const cors = require('cors');
const compression = require('compression');
const session = require('express-session')
const session = require('express-session');

function parseOrigin(originEnv) {
if (originEnv === 'true') {
return true;
} else if (originEnv === 'false') {
return false;
} else if (/^\/.*\/$/.test(originEnv)) { // 正则表达式检查
return new RegExp(originEnv.slice(1, -1)); // 去掉两边的斜杠
} else if (originEnv.startsWith('[') && originEnv.endsWith(']')) { // 数组检查
const originsArray = JSON.parse(originEnv);
return originsArray.map(item => {
if (typeof item === 'string') {
return item;
} else if (/^\/.*\/$/.test(item)) {
return new RegExp(item.slice(1, -1));
} else {
throw new Error('Invalid origin value in array');
}
});
} else if (typeof originEnv === 'string') {
return originEnv;
} else {
throw new Error('Invalid origin value');
}
}

const originEnv = process.env.STEEDOS_CORS_ORIGIN;
let origin = true;
try {
origin = parseOrigin(originEnv);
console.log('Parsed origin:', origin);
} catch (error) {
console.error('Error parsing origin:', error.message);
}

console.log(`origin----<`, origin)

class ExpressAppStatic{
app = null;
Expand All @@ -19,7 +55,7 @@ class ExpressAppStatic{
this.router = express.Router();
// 读取环境变量、配置文件, 启动端口, 控制中间件
const app = express();
app.use(cors({origin: true, credentials: true}))
app.use(cors({origin: origin, credentials: true}))
app.use(compression());
app.use(session({
secret: process.env.STEEDOS_SESSION_SECRET || 'steedos',
Expand Down
49 changes: 44 additions & 5 deletions server/bundle/programs/server/app/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion server/bundle/programs/server/app/app.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 40ef460

Please sign in to comment.