mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 09:45:36 +08:00
fix(security): protect first-run notifications and server errors (#3126)
* fix(security): close anonymous notification setup and mask server errors * fix: hide internal errors in default deployment * fix: route HTTP errors through application logger * test: mock logger in API fallback regression * fix: avoid sensitive SQL details in HTTP error logs
This commit is contained in:
1 parent
cae580e02f
commit
1cfc0c8e2e
11 files changed
+104
-146
No files matched your search
+36
-37
@@ -16,6 +16,7 @@ import { t } from '../shared/i18n';
|
||||
import { AppScope } from '../data/open';
|
||||
import uniqueConstraintError from '../middlewares/uniqueConstraintError';
|
||||
import protectedPathCase from '../middlewares/protectedPathCase';
|
||||
import Logger from './logger';
|
||||
|
||||
function resolveTrustProxy(value = process.env.QL_TRUST_PROXY) {
|
||||
const setting = value?.trim();
|
||||
@@ -191,14 +192,7 @@ export default ({ app }: { app: Application }) => {
|
||||
|
||||
app.use(async (req, res, next) => {
|
||||
const pathLower = req.path.toLowerCase();
|
||||
if (
|
||||
![
|
||||
'/api/user/init',
|
||||
'/api/user/notification/init',
|
||||
'/open/user/init',
|
||||
'/open/user/notification/init',
|
||||
].includes(pathLower)
|
||||
) {
|
||||
if (!['/api/user/init', '/open/user/init'].includes(pathLower)) {
|
||||
return next();
|
||||
}
|
||||
const authInfo = (await shareStore.getAuthInfo()) || ({} as AuthInfo);
|
||||
@@ -238,37 +232,23 @@ export default ({ app }: { app: Application }) => {
|
||||
|
||||
app.use(
|
||||
(
|
||||
err: Error & { status: number },
|
||||
err: Error & { status: number; code?: string },
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) => {
|
||||
if (err.name === 'UnauthorizedError') {
|
||||
return res
|
||||
.status(err.status)
|
||||
.send({ code: 401, message: err.message })
|
||||
.end();
|
||||
}
|
||||
return next(err);
|
||||
},
|
||||
);
|
||||
|
||||
app.use(
|
||||
(
|
||||
err: Error & { errors: any[] },
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) => {
|
||||
if (err.name.includes('Sequelize')) {
|
||||
return res
|
||||
.status(500)
|
||||
.send({
|
||||
code: 400,
|
||||
message: `${err.message}`,
|
||||
errors: err.errors,
|
||||
})
|
||||
.end();
|
||||
let message = err.message;
|
||||
if (!config.isDevelopment) {
|
||||
if (err.code === 'credentials_bad_scheme') {
|
||||
message = t('暂无权限');
|
||||
} else if (err.code === 'credentials_required') {
|
||||
message = t('请先登录');
|
||||
} else {
|
||||
message = t('Token 已失效');
|
||||
}
|
||||
}
|
||||
return res.status(401).send({ code: 401, message }).end();
|
||||
}
|
||||
return next(err);
|
||||
},
|
||||
@@ -281,10 +261,29 @@ export default ({ app }: { app: Application }) => {
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) => {
|
||||
res.status(err.status || 500);
|
||||
const status =
|
||||
Number.isInteger(err.status) && err.status >= 400 && err.status < 600
|
||||
? err.status
|
||||
: 500;
|
||||
if (status >= 500) {
|
||||
const route =
|
||||
typeof req.route?.path === 'string' ? req.route.path : '?';
|
||||
const errorName = /^[A-Za-z][A-Za-z0-9]{0,63}$/.test(err.name)
|
||||
? err.name
|
||||
: 'Error';
|
||||
Logger.error(
|
||||
'HTTP request failed: status=%d method=%s route=%s type=%s',
|
||||
status,
|
||||
req.method,
|
||||
route,
|
||||
errorName,
|
||||
);
|
||||
}
|
||||
res.status(status);
|
||||
res.json({
|
||||
code: err.status || 500,
|
||||
message: err.message,
|
||||
code: status,
|
||||
message:
|
||||
!config.isDevelopment && status >= 500 ? t('未知错误') : err.message,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user