mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-03-14 01:34:47 +08:00
fix: 限制非文本文件读取,限制大文件读取
This commit is contained in:
parent
2add7312e6
commit
05ef2373ef
@ -45,7 +45,9 @@ var (
|
|||||||
|
|
||||||
// app
|
// app
|
||||||
var (
|
var (
|
||||||
ErrPortInUsed = "ErrPortInUsed"
|
ErrPortInUsed = "ErrPortInUsed"
|
||||||
ErrAppLimit = "ErrAppLimit"
|
ErrAppLimit = "ErrAppLimit"
|
||||||
ErrAppRequired = "ErrAppRequired"
|
ErrAppRequired = "ErrAppRequired"
|
||||||
|
ErrFileCanNotRead = "ErrFileCanNotRead"
|
||||||
|
ErrFileToLarge = "ErrFileToLarge"
|
||||||
)
|
)
|
||||||
|
@ -20,4 +20,9 @@ ErrNameIsExist: "Name is already exist"
|
|||||||
#app
|
#app
|
||||||
ErrPortInUsed: "{{ .detail }} port already in use"
|
ErrPortInUsed: "{{ .detail }} port already in use"
|
||||||
ErrAppLimit: "App exceeds install limit"
|
ErrAppLimit: "App exceeds install limit"
|
||||||
ErrAppRequired: "{{ .detail }} app is required"
|
ErrAppRequired: "{{ .detail }} app is required"
|
||||||
|
|
||||||
|
|
||||||
|
#file
|
||||||
|
ErrFileCanNotRead: "File can not read"
|
||||||
|
ErrFileToLarge: "file is too large"
|
@ -20,4 +20,9 @@ ErrNameIsExist: "名称已存在"
|
|||||||
#app
|
#app
|
||||||
ErrPortInUsed: "{{ .detail }} 端口已被占用!"
|
ErrPortInUsed: "{{ .detail }} 端口已被占用!"
|
||||||
ErrAppLimit: "应用超出安装数量限制"
|
ErrAppLimit: "应用超出安装数量限制"
|
||||||
ErrAppRequired: "请先安装 {{ .detail }} 应用"
|
ErrAppRequired: "请先安装 {{ .detail }} 应用"
|
||||||
|
|
||||||
|
|
||||||
|
#file
|
||||||
|
ErrFileCanNotRead: "文件不可读"
|
||||||
|
ErrFileToLarge: "文件超过10M,无法打开"
|
@ -2,13 +2,14 @@ package files
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
||||||
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -170,9 +171,36 @@ func (f *FileInfo) getContent() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if detectBinary(cByte) {
|
||||||
|
return buserr.New(constant.ErrFileCanNotRead, "", nil)
|
||||||
|
}
|
||||||
f.Content = string(cByte)
|
f.Content = string(cByte)
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
return errors.New("file is too large!")
|
return buserr.New(constant.ErrFileToLarge, "", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func detectBinary(buf []byte) bool {
|
||||||
|
var whiteByte int = 0
|
||||||
|
n := min(1024, len(buf))
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
if (buf[i] >= 0x20) || buf[i] == 9 || buf[i] == 10 || buf[i] == 13 {
|
||||||
|
whiteByte++
|
||||||
|
} else if buf[i] <= 6 || (buf[i] >= 14 && buf[i] <= 31) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if whiteByte >= 1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(x, y int) int {
|
||||||
|
if x < y {
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
return y
|
||||||
|
}
|
||||||
|
@ -96,7 +96,7 @@
|
|||||||
<el-link :underline="false" @click="openMode(row)">{{ row.mode }}</el-link>
|
<el-link :underline="false" @click="openMode(row)">{{ row.mode }}</el-link>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column :label="$t('file.user')" prop="user"></el-table-column>
|
<el-table-column :label="$t('file.user')" prop="user" show-overflow-tooltip></el-table-column>
|
||||||
<el-table-column :label="$t('file.group')" prop="group"></el-table-column>
|
<el-table-column :label="$t('file.group')" prop="group"></el-table-column>
|
||||||
<el-table-column :label="$t('file.size')" prop="size">
|
<el-table-column :label="$t('file.size')" prop="size">
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
@ -425,8 +425,8 @@ const openCodeEditor = (row: File.File) => {
|
|||||||
codeReq.expand = true;
|
codeReq.expand = true;
|
||||||
GetFileContent(codeReq).then((res) => {
|
GetFileContent(codeReq).then((res) => {
|
||||||
editorPage.content = res.data.content;
|
editorPage.content = res.data.content;
|
||||||
|
editorPage.open = true;
|
||||||
});
|
});
|
||||||
editorPage.open = true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const closeCodeEditor = () => {
|
const closeCodeEditor = () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user