Browse Source

fix: 限制非文本文件读取,限制大文件读取

pull/59/head
zhengkunwang223 2 years ago committed by zhengkunwang223
parent
commit
05ef2373ef
  1. 8
      backend/constant/errs.go
  2. 5
      backend/i18n/lang/en.yaml
  3. 5
      backend/i18n/lang/zh.yaml
  4. 32
      backend/utils/files/fileinfo.go
  5. 4
      frontend/src/views/host/file-management/index.vue

8
backend/constant/errs.go

@ -45,7 +45,9 @@ var (
// app
var (
ErrPortInUsed = "ErrPortInUsed"
ErrAppLimit = "ErrAppLimit"
ErrAppRequired = "ErrAppRequired"
ErrPortInUsed = "ErrPortInUsed"
ErrAppLimit = "ErrAppLimit"
ErrAppRequired = "ErrAppRequired"
ErrFileCanNotRead = "ErrFileCanNotRead"
ErrFileToLarge = "ErrFileToLarge"
)

5
backend/i18n/lang/en.yaml

@ -21,3 +21,8 @@ ErrNameIsExist: "Name is already exist"
ErrPortInUsed: "{{ .detail }} port already in use"
ErrAppLimit: "App exceeds install limit"
ErrAppRequired: "{{ .detail }} app is required"
#file
ErrFileCanNotRead: "File can not read"
ErrFileToLarge: "file is too large"

5
backend/i18n/lang/zh.yaml

@ -21,3 +21,8 @@ ErrNameIsExist: "名称已存在"
ErrPortInUsed: "{{ .detail }} 端口已被占用!"
ErrAppLimit: "应用超出安装数量限制"
ErrAppRequired: "请先安装 {{ .detail }} 应用"
#file
ErrFileCanNotRead: "文件不可读"
ErrFileToLarge: "文件超过10M,无法打开"

32
backend/utils/files/fileinfo.go

@ -2,13 +2,14 @@ package files
import (
"fmt"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
"os"
"path"
"path/filepath"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/spf13/afero"
)
@ -170,9 +171,36 @@ func (f *FileInfo) getContent() error {
if err != nil {
return nil
}
if detectBinary(cByte) {
return buserr.New(constant.ErrFileCanNotRead, "", nil)
}
f.Content = string(cByte)
return nil
} 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
}

4
frontend/src/views/host/file-management/index.vue

@ -96,7 +96,7 @@
<el-link :underline="false" @click="openMode(row)">{{ row.mode }}</el-link>
</template>
</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.size')" prop="size">
<template #default="{ row }">
@ -425,8 +425,8 @@ const openCodeEditor = (row: File.File) => {
codeReq.expand = true;
GetFileContent(codeReq).then((res) => {
editorPage.content = res.data.content;
editorPage.open = true;
});
editorPage.open = true;
};
const closeCodeEditor = () => {

Loading…
Cancel
Save