rules.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package main
  2. // 本机目录的访问范围(docs/design/21-knowledge-sources.md §4.3)。
  3. // 允许的根目录写死在代码中,不接受平台传来的配置。
  4. import (
  5. "os"
  6. "path/filepath"
  7. "sort"
  8. "strings"
  9. "golang.org/x/sys/unix"
  10. )
  11. var allowedRoots = []string{"/home", "/mnt", "/media"}
  12. // ruleProblem 按 §4.3 检查一个已解析符号链接的绝对路径,通过时返回空字符串。
  13. func ruleProblem(real string, roots []string) string {
  14. if !filepath.IsAbs(real) || filepath.Clean(real) != real {
  15. return "路径必须是规范的绝对路径"
  16. }
  17. root := ""
  18. for _, r := range roots {
  19. if real == r || strings.HasPrefix(real, r+"/") {
  20. root = r
  21. break
  22. }
  23. }
  24. if root == "" {
  25. return "只能选择 " + strings.Join(roots, "、") + " 下的目录"
  26. }
  27. if real == root {
  28. return "不能选择 " + root + " 本身,请选择其中的子目录"
  29. }
  30. rest := strings.Split(strings.TrimPrefix(real, root+"/"), "/")
  31. if root == "/home" && len(rest) == 1 {
  32. return "不能选择用户主目录本身,请选择其中的子目录"
  33. }
  34. if hasHidden(real) {
  35. return "不能选择隐藏目录(以 . 开头)或其中的目录"
  36. }
  37. return ""
  38. }
  39. func hasHidden(p string) bool {
  40. for _, seg := range strings.Split(p, "/") {
  41. if strings.HasPrefix(seg, ".") {
  42. return true
  43. }
  44. }
  45. return false
  46. }
  47. // inBrowseRange:逐级选择目录时可以进入的范围(允许的根目录本身与 home 目录本身也可以进入,但不能选择)
  48. func inBrowseRange(real string, roots []string) bool {
  49. if hasHidden(real) {
  50. return false
  51. }
  52. for _, r := range roots {
  53. if real == r || strings.HasPrefix(real, r+"/") {
  54. return true
  55. }
  56. }
  57. return false
  58. }
  59. // checkError 带状态码的检查失败
  60. type checkError struct {
  61. State string // not_allowed | path_missing
  62. Message string
  63. }
  64. func (e *checkError) Error() string { return e.Message }
  65. // checkSource 检查要登记(或解析出的)本机目录,返回解析符号链接后的真实路径
  66. func checkSource(p string, roots []string) (string, *checkError) {
  67. if !filepath.IsAbs(p) {
  68. return "", &checkError{"not_allowed", "请填写绝对路径"}
  69. }
  70. p = filepath.Clean(p)
  71. if hasHidden(p) {
  72. return "", &checkError{"not_allowed", "不能选择隐藏目录(以 . 开头)或其中的目录"}
  73. }
  74. real, err := filepath.EvalSymlinks(p)
  75. if err != nil {
  76. return "", &checkError{"path_missing", "目录不存在:" + p}
  77. }
  78. if msg := ruleProblem(real, roots); msg != "" {
  79. if real != p {
  80. msg += "(实际位置:" + real + ")"
  81. }
  82. return "", &checkError{"not_allowed", msg}
  83. }
  84. st, err := os.Stat(real)
  85. if err != nil {
  86. return "", &checkError{"path_missing", "目录不存在:" + p}
  87. }
  88. if !st.IsDir() {
  89. return "", &checkError{"not_allowed", "不是目录:" + p}
  90. }
  91. if unix.Access(real, unix.R_OK|unix.X_OK) != nil {
  92. return "", &checkError{"not_allowed", "挂载服务没有读取这个目录的权限:" + p}
  93. }
  94. return real, nil
  95. }
  96. type browseEntry struct {
  97. Name string `json:"name"`
  98. Path string `json:"path"`
  99. Selectable bool `json:"selectable"`
  100. Reason string `json:"reason,omitempty"`
  101. }
  102. type browseResult struct {
  103. Path string `json:"path"`
  104. Parent string `json:"parent"`
  105. Entries []browseEntry `json:"entries"`
  106. }
  107. const maxBrowseEntries = 2000
  108. // browse 列出可选择的子目录:隐藏以 . 开头的,标出不能选择的及原因。path 为空时列出允许的根目录。
  109. func browse(p string, roots []string) (*browseResult, *checkError) {
  110. if p == "" || p == "/" {
  111. res := &browseResult{Path: "", Parent: "", Entries: []browseEntry{}}
  112. for _, r := range roots {
  113. if st, err := os.Stat(r); err == nil && st.IsDir() {
  114. res.Entries = append(res.Entries, browseEntry{Name: r, Path: r, Reason: "请进入后选择子目录"})
  115. }
  116. }
  117. return res, nil
  118. }
  119. if !filepath.IsAbs(p) {
  120. return nil, &checkError{"not_allowed", "请填写绝对路径"}
  121. }
  122. p = filepath.Clean(p)
  123. real, err := filepath.EvalSymlinks(p)
  124. if err != nil {
  125. return nil, &checkError{"path_missing", "目录不存在:" + p}
  126. }
  127. if hasHidden(p) || !inBrowseRange(real, roots) {
  128. return nil, &checkError{"not_allowed", "只能浏览 " + strings.Join(roots, "、") + " 下的目录"}
  129. }
  130. f, err := os.Open(real)
  131. if err != nil {
  132. return nil, &checkError{"not_allowed", "没有读取权限:" + p}
  133. }
  134. names, err := f.Readdirnames(-1)
  135. f.Close()
  136. if err != nil {
  137. return nil, &checkError{"not_allowed", "无法读取目录:" + p}
  138. }
  139. sort.Strings(names)
  140. parent := filepath.Dir(p)
  141. isRoot := false
  142. for _, r := range roots {
  143. if p == r {
  144. isRoot = true
  145. }
  146. }
  147. if isRoot {
  148. parent = ""
  149. }
  150. res := &browseResult{Path: p, Parent: parent, Entries: []browseEntry{}}
  151. for _, name := range names {
  152. if strings.HasPrefix(name, ".") {
  153. continue
  154. }
  155. child := filepath.Join(p, name)
  156. st, err := os.Stat(child)
  157. if err != nil || !st.IsDir() {
  158. continue
  159. }
  160. e := browseEntry{Name: name, Path: child, Selectable: true}
  161. childReal, err := filepath.EvalSymlinks(child)
  162. if err != nil {
  163. continue
  164. }
  165. if msg := ruleProblem(childReal, roots); msg != "" {
  166. e.Selectable, e.Reason = false, msg
  167. } else if unix.Access(childReal, unix.R_OK|unix.X_OK) != nil {
  168. e.Selectable, e.Reason = false, "没有读取权限"
  169. }
  170. if childReal != child && e.Selectable {
  171. e.Reason = "链接到 " + childReal
  172. }
  173. res.Entries = append(res.Entries, e)
  174. if len(res.Entries) >= maxBrowseEntries {
  175. break
  176. }
  177. }
  178. return res, nil
  179. }