Skip to content

Commit

Permalink
修复文件越权访问
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Sep 4, 2024
1 parent 8c0f464 commit a0613d0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 36 deletions.
1 change: 0 additions & 1 deletion back/api/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getFileContentByName, getLastModifyFilePath } from '../config/util';
import { Router, Request, Response, NextFunction } from 'express';
import { Container } from 'typedi';
import { Logger } from 'winston';
Expand Down
38 changes: 22 additions & 16 deletions back/api/log.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Router, Request, Response, NextFunction } from 'express';
import { Container } from 'typedi';
import { Logger } from 'winston';
import * as fs from 'fs';
import config from '../config';
import { getFileContentByName, readDirs, rmPath } from '../config/util';
import { join } from 'path';
import { join, resolve } from 'path';
import { celebrate, Joi } from 'celebrate';
const route = Router();
const blacklist = ['.tmp'];
Expand All @@ -30,15 +29,19 @@ export default (app: Router) => {
'/detail',
async (req: Request, res: Response, next: NextFunction) => {
try {
if (blacklist.includes(req.path)) {
return res.send({ code: 403, message: '暂无权限' });
}
const filePath = join(
const finalPath = resolve(
config.logPath,
(req.query.path || '') as string,
req.query.file as string,
(req.query.path as string) || '',
(req.query.file as string) || '',
);
const content = await getFileContentByName(filePath);

if (
blacklist.includes(req.query.path as string) ||
!finalPath.startsWith(config.logPath)
) {
return res.send({ code: 403, message: '暂无权限' });
}
const content = await getFileContentByName(finalPath);
res.send({ code: 200, data: content });
} catch (e) {
return next(e);
Expand All @@ -50,15 +53,18 @@ export default (app: Router) => {
'/:file',
async (req: Request, res: Response, next: NextFunction) => {
try {
if (blacklist.includes(req.path)) {
return res.send({ code: 403, message: '暂无权限' });
}
const filePath = join(
const finalPath = resolve(
config.logPath,
(req.query.path || '') as string,
req.params.file,
(req.query.path as string) || '',
(req.params.file as string) || '',
);
const content = await getFileContentByName(filePath);
if (
blacklist.includes(req.path) ||
!finalPath.startsWith(config.logPath)
) {
return res.send({ code: 403, message: '暂无权限' });
}
const content = await getFileContentByName(finalPath);
res.send({ code: 200, data: content });
} catch (e) {
return next(e);
Expand Down
9 changes: 1 addition & 8 deletions back/api/script.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
fileExist,
getFileContentByName,
readDirs,
getLastModifyFilePath,
readDir,
rmPath,
} from '../config/util';
import { fileExist, readDirs, readDir, rmPath } from '../config/util';
import { Router, Request, Response, NextFunction } from 'express';
import { Container } from 'typedi';
import { Logger } from 'winston';
Expand Down
6 changes: 3 additions & 3 deletions back/config/serverEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ let pickedEnv: Record<string, string>;

function getPickedEnv() {
if (pickedEnv) return pickedEnv;
const picked = pick(process.env, ['QlBaseUrl', 'DeployEnv']);
const picked = pick(process.env, ['QlBaseUrl', 'DeployEnv', 'QL_DIR']);
if (picked.QlBaseUrl) {
if (!picked.QlBaseUrl.startsWith('/')) {
picked.QlBaseUrl = `/${picked.QlBaseUrl}`
picked.QlBaseUrl = `/${picked.QlBaseUrl}`;
}
if (!picked.QlBaseUrl.endsWith('/')) {
picked.QlBaseUrl = `${picked.QlBaseUrl}/`
picked.QlBaseUrl = `${picked.QlBaseUrl}/`;
}
}
pickedEnv = picked as Record<string, string>;
Expand Down
16 changes: 14 additions & 2 deletions back/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@ export default class ConfigService {

public async getFile(filePath: string, res: Response) {
let content = '';
if (config.blackFileList.includes(filePath) || !filePath) {
res.send({ code: 403, message: '文件无法访问' });
const avaliablePath = [config.rootPath, config.configPath].map((x) =>
path.resolve(x, filePath),
);

if (
config.blackFileList.includes(filePath) ||
avaliablePath.every(
(x) =>
!x.startsWith(config.scriptPath) && !x.startsWith(config.configPath),
) ||
!filePath
) {
return res.send({ code: 403, message: '文件无法访问' });
}

if (filePath.startsWith('sample/')) {
const res = await got.get(
`https://gitlab.com/whyour/qinglong/-/raw/master/${filePath}`,
Expand Down
10 changes: 6 additions & 4 deletions back/services/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ export default class ScriptService {
}

public async getFile(filePath: string, fileName: string) {
let _filePath = join(config.scriptPath, filePath, fileName);
if (filePath.startsWith(config.dataPath)) {
_filePath = join(filePath, fileName);
const finalPath = path.resolve(config.scriptPath, filePath, fileName);

if (!finalPath.startsWith(config.scriptPath)) {
return '';
}
const content = await getFileContentByName(_filePath);

const content = await getFileContentByName(finalPath);
return content;
}
}
5 changes: 3 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,9 @@ export function getCommandScript(
['.js', '.ts', '.sh', '.py'].some((y) => x.endsWith(y)),
);
if (!scriptsPart) return;
if (scriptsPart.startsWith('/ql/data/scripts')) {
scriptsPart = scriptsPart.replace('/ql/data/scripts/', '');
const scriptDir = `${window.__ENV__QL_DIR}/data/scripts`;
if (scriptsPart.startsWith(scriptDir)) {
scriptsPart = scriptsPart.replace(scriptDir, '');
}

let p: string, s: string;
Expand Down
1 change: 1 addition & 0 deletions typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ declare module '*.svg' {
interface Window {
__ENV__QlBaseUrl: string;
__ENV__DeployEnv: string;
__ENV__QL_DIR: string;
}

0 comments on commit a0613d0

Please sign in to comment.