editor.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. package repo
  2. import (
  3. "fmt"
  4. "net/http"
  5. "path"
  6. "slices"
  7. "strings"
  8. "github.com/cockroachdb/errors"
  9. log "unknwon.dev/clog/v2"
  10. "gogs.io/gogs/internal/conf"
  11. "gogs.io/gogs/internal/context"
  12. "gogs.io/gogs/internal/database"
  13. "gogs.io/gogs/internal/form"
  14. "gogs.io/gogs/internal/gitutil"
  15. "gogs.io/gogs/internal/pathutil"
  16. "gogs.io/gogs/internal/template"
  17. "gogs.io/gogs/internal/tool"
  18. )
  19. const (
  20. tmplEditorEdit = "repo/editor/edit"
  21. tmplEditorDiffPreview = "repo/editor/diff_preview"
  22. tmplEditorDelete = "repo/editor/delete"
  23. tmplEditorUpload = "repo/editor/upload"
  24. )
  25. var errInternalServerError = errors.New("internal server error")
  26. // getParentTreeFields returns list of parent tree names and corresponding tree paths
  27. // based on given tree path.
  28. func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
  29. if treePath == "" {
  30. return treeNames, treePaths
  31. }
  32. treeNames = strings.Split(treePath, "/")
  33. treePaths = make([]string, len(treeNames))
  34. for i := range treeNames {
  35. treePaths[i] = strings.Join(treeNames[:i+1], "/")
  36. }
  37. return treeNames, treePaths
  38. }
  39. func editFile(c *context.Context, isNewFile bool) {
  40. c.PageIs("Edit")
  41. c.RequireHighlightJS()
  42. c.RequireSimpleMDE()
  43. c.Data["IsNewFile"] = isNewFile
  44. treeNames, treePaths := getParentTreeFields(c.Repo.TreePath)
  45. if !isNewFile {
  46. entry, err := c.Repo.Commit.TreeEntry(c.Repo.TreePath)
  47. if err != nil {
  48. c.NotFoundOrError(gitutil.NewError(err), "get tree entry")
  49. return
  50. }
  51. // No way to edit a directory online.
  52. if entry.IsTree() {
  53. c.NotFound()
  54. return
  55. }
  56. blob := entry.Blob()
  57. p, err := blob.Bytes()
  58. if err != nil {
  59. c.Error(err, "get blob data")
  60. return
  61. }
  62. c.Data["FileSize"] = blob.Size()
  63. c.Data["FileName"] = blob.Name()
  64. // Only text file are editable online.
  65. if !tool.IsTextFile(p) {
  66. c.NotFound()
  67. return
  68. }
  69. if content, err := template.ToUTF8WithErr(p); err != nil {
  70. if err != nil {
  71. log.Error("Failed to convert encoding to UTF-8: %v", err)
  72. }
  73. c.Data["FileContent"] = string(p)
  74. } else {
  75. c.Data["FileContent"] = content
  76. }
  77. } else {
  78. treeNames = append(treeNames, "") // Append empty string to allow user name the new file.
  79. }
  80. c.Data["ParentTreePath"] = path.Dir(c.Repo.TreePath)
  81. c.Data["TreeNames"] = treeNames
  82. c.Data["TreePaths"] = treePaths
  83. c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + c.Repo.BranchName
  84. c.Data["commit_summary"] = ""
  85. c.Data["commit_message"] = ""
  86. c.Data["commit_choice"] = "direct"
  87. c.Data["new_branch_name"] = ""
  88. c.Data["last_commit"] = c.Repo.Commit.ID
  89. c.Data["MarkdownFileExts"] = strings.Join(conf.Markdown.FileExtensions, ",")
  90. c.Data["LineWrapExtensions"] = strings.Join(conf.Repository.Editor.LineWrapExtensions, ",")
  91. c.Data["PreviewableFileModes"] = strings.Join(conf.Repository.Editor.PreviewableFileModes, ",")
  92. c.Data["EditorconfigURLPrefix"] = fmt.Sprintf("%s/api/v1/repos/%s/editorconfig/", conf.Server.Subpath, c.Repo.Repository.FullName())
  93. c.Success(tmplEditorEdit)
  94. }
  95. func EditFile(c *context.Context) {
  96. editFile(c, false)
  97. }
  98. func NewFile(c *context.Context) {
  99. editFile(c, true)
  100. }
  101. func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
  102. c.PageIs("Edit")
  103. c.RequireHighlightJS()
  104. c.RequireSimpleMDE()
  105. c.Data["IsNewFile"] = isNewFile
  106. oldBranchName := c.Repo.BranchName
  107. branchName := oldBranchName
  108. oldTreePath := c.Repo.TreePath
  109. lastCommit := f.LastCommit
  110. f.LastCommit = c.Repo.Commit.ID.String()
  111. if f.IsNewBrnach() {
  112. branchName = f.NewBranchName
  113. }
  114. // 🚨 SECURITY: Prevent path traversal.
  115. f.TreePath = pathutil.Clean(f.TreePath)
  116. treeNames, treePaths := getParentTreeFields(f.TreePath)
  117. c.Data["ParentTreePath"] = path.Dir(c.Repo.TreePath)
  118. c.Data["TreePath"] = f.TreePath
  119. c.Data["TreeNames"] = treeNames
  120. c.Data["TreePaths"] = treePaths
  121. c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + branchName
  122. c.Data["FileContent"] = f.Content
  123. c.Data["commit_summary"] = f.CommitSummary
  124. c.Data["commit_message"] = f.CommitMessage
  125. c.Data["commit_choice"] = f.CommitChoice
  126. c.Data["new_branch_name"] = branchName
  127. c.Data["last_commit"] = f.LastCommit
  128. c.Data["MarkdownFileExts"] = strings.Join(conf.Markdown.FileExtensions, ",")
  129. c.Data["LineWrapExtensions"] = strings.Join(conf.Repository.Editor.LineWrapExtensions, ",")
  130. c.Data["PreviewableFileModes"] = strings.Join(conf.Repository.Editor.PreviewableFileModes, ",")
  131. if c.HasError() {
  132. c.HTML(http.StatusBadRequest, tmplEditorEdit)
  133. return
  134. }
  135. if f.TreePath == "" {
  136. c.FormErr("TreePath")
  137. c.RenderWithErr(c.Tr("repo.editor.filename_cannot_be_empty"), http.StatusBadRequest, tmplEditorEdit, &f)
  138. return
  139. }
  140. if oldBranchName != branchName {
  141. if _, err := c.Repo.Repository.GetBranch(branchName); err == nil {
  142. c.FormErr("NewBranchName")
  143. c.RenderWithErr(c.Tr("repo.editor.branch_already_exists", branchName), http.StatusUnprocessableEntity, tmplEditorEdit, &f)
  144. return
  145. }
  146. }
  147. var newTreePath string
  148. for index, part := range treeNames {
  149. newTreePath = path.Join(newTreePath, part)
  150. entry, err := c.Repo.Commit.TreeEntry(newTreePath)
  151. if err != nil {
  152. if gitutil.IsErrRevisionNotExist(err) {
  153. // Means there is no item with that name, so we're good
  154. break
  155. }
  156. c.Error(err, "get tree entry")
  157. return
  158. }
  159. if index != len(treeNames)-1 {
  160. if !entry.IsTree() {
  161. c.FormErr("TreePath")
  162. c.RenderWithErr(c.Tr("repo.editor.directory_is_a_file", part), http.StatusUnprocessableEntity, tmplEditorEdit, &f)
  163. return
  164. }
  165. } else {
  166. // 🚨 SECURITY: Do not allow editing if the target file is a symlink.
  167. if entry.IsSymlink() {
  168. c.FormErr("TreePath")
  169. c.RenderWithErr(c.Tr("repo.editor.file_is_a_symlink", part), http.StatusUnprocessableEntity, tmplEditorEdit, &f)
  170. return
  171. } else if entry.IsTree() {
  172. c.FormErr("TreePath")
  173. c.RenderWithErr(c.Tr("repo.editor.filename_is_a_directory", part), http.StatusUnprocessableEntity, tmplEditorEdit, &f)
  174. return
  175. }
  176. }
  177. }
  178. if !isNewFile {
  179. entry, err := c.Repo.Commit.TreeEntry(oldTreePath)
  180. if err != nil {
  181. if gitutil.IsErrRevisionNotExist(err) {
  182. c.FormErr("TreePath")
  183. c.RenderWithErr(c.Tr("repo.editor.file_editing_no_longer_exists", oldTreePath), http.StatusNotFound, tmplEditorEdit, &f)
  184. } else {
  185. c.Error(err, "get tree entry")
  186. }
  187. return
  188. }
  189. // 🚨 SECURITY: Do not allow editing if the old file is a symlink.
  190. if entry.IsSymlink() {
  191. c.FormErr("TreePath")
  192. c.RenderWithErr(c.Tr("repo.editor.file_is_a_symlink", oldTreePath), http.StatusUnprocessableEntity, tmplEditorEdit, &f)
  193. return
  194. }
  195. if lastCommit != c.Repo.CommitID {
  196. files, err := c.Repo.Commit.FilesChangedAfter(lastCommit)
  197. if err != nil {
  198. c.Error(err, "get changed files")
  199. return
  200. }
  201. if slices.Contains(files, f.TreePath) {
  202. c.RenderWithErr(c.Tr("repo.editor.file_changed_while_editing", c.Repo.RepoLink+"/compare/"+lastCommit+"..."+c.Repo.CommitID), http.StatusConflict, tmplEditorEdit, &f)
  203. return
  204. }
  205. }
  206. }
  207. if oldTreePath != f.TreePath {
  208. // We have a new filename (rename or completely new file) so we need to make sure it doesn't already exist, can't clobber.
  209. entry, err := c.Repo.Commit.TreeEntry(f.TreePath)
  210. if err != nil {
  211. if !gitutil.IsErrRevisionNotExist(err) {
  212. c.Error(err, "get tree entry")
  213. return
  214. }
  215. }
  216. if entry != nil {
  217. c.FormErr("TreePath")
  218. c.RenderWithErr(c.Tr("repo.editor.file_already_exists", f.TreePath), http.StatusUnprocessableEntity, tmplEditorEdit, &f)
  219. return
  220. }
  221. }
  222. message := strings.TrimSpace(f.CommitSummary)
  223. if message == "" {
  224. if isNewFile {
  225. message = c.Tr("repo.editor.add", f.TreePath)
  226. } else {
  227. message = c.Tr("repo.editor.update", f.TreePath)
  228. }
  229. }
  230. f.CommitMessage = strings.TrimSpace(f.CommitMessage)
  231. if len(f.CommitMessage) > 0 {
  232. message += "\n\n" + f.CommitMessage
  233. }
  234. if err := c.Repo.Repository.UpdateRepoFile(c.User, database.UpdateRepoFileOptions{
  235. OldBranch: oldBranchName,
  236. NewBranch: branchName,
  237. OldTreeName: oldTreePath,
  238. NewTreeName: f.TreePath,
  239. Message: message,
  240. Content: strings.ReplaceAll(f.Content, "\r", ""),
  241. IsNewFile: isNewFile,
  242. }); err != nil {
  243. log.Error("Failed to update repo file: %v", err)
  244. c.FormErr("TreePath")
  245. c.RenderWithErr(c.Tr("repo.editor.fail_to_update_file", f.TreePath, errInternalServerError), http.StatusInternalServerError, tmplEditorEdit, &f)
  246. return
  247. }
  248. if f.IsNewBrnach() && c.Repo.PullRequest.Allowed {
  249. c.Redirect(c.Repo.PullRequestURL(oldBranchName, f.NewBranchName))
  250. } else {
  251. c.Redirect(c.Repo.RepoLink + "/src/" + branchName + "/" + f.TreePath)
  252. }
  253. }
  254. func EditFilePost(c *context.Context, f form.EditRepoFile) {
  255. editFilePost(c, f, false)
  256. }
  257. func NewFilePost(c *context.Context, f form.EditRepoFile) {
  258. editFilePost(c, f, true)
  259. }
  260. func DiffPreviewPost(c *context.Context, f form.EditPreviewDiff) {
  261. // 🚨 SECURITY: Prevent path traversal.
  262. treePath := pathutil.Clean(c.Repo.TreePath)
  263. entry, err := c.Repo.Commit.TreeEntry(treePath)
  264. if err != nil {
  265. c.Error(err, "get tree entry")
  266. return
  267. } else if entry.IsTree() {
  268. c.Status(http.StatusUnprocessableEntity)
  269. return
  270. }
  271. diff, err := c.Repo.Repository.GetDiffPreview(c.Repo.BranchName, treePath, f.Content)
  272. if err != nil {
  273. c.Error(err, "get diff preview")
  274. return
  275. }
  276. if diff.NumFiles() == 0 {
  277. c.PlainText(http.StatusOK, c.Tr("repo.editor.no_changes_to_show"))
  278. return
  279. }
  280. c.Data["File"] = diff.Files[0]
  281. c.Success(tmplEditorDiffPreview)
  282. }
  283. func DeleteFile(c *context.Context) {
  284. c.PageIs("Delete")
  285. c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + c.Repo.BranchName
  286. c.Data["TreePath"] = c.Repo.TreePath
  287. c.Data["commit_summary"] = ""
  288. c.Data["commit_message"] = ""
  289. c.Data["commit_choice"] = "direct"
  290. c.Data["new_branch_name"] = ""
  291. c.Success(tmplEditorDelete)
  292. }
  293. func DeleteFilePost(c *context.Context, f form.DeleteRepoFile) {
  294. c.PageIs("Delete")
  295. c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + c.Repo.BranchName
  296. // 🚨 SECURITY: Prevent path traversal.
  297. c.Repo.TreePath = pathutil.Clean(c.Repo.TreePath)
  298. c.Data["TreePath"] = c.Repo.TreePath
  299. oldBranchName := c.Repo.BranchName
  300. branchName := oldBranchName
  301. if f.IsNewBrnach() {
  302. branchName = f.NewBranchName
  303. }
  304. c.Data["commit_summary"] = f.CommitSummary
  305. c.Data["commit_message"] = f.CommitMessage
  306. c.Data["commit_choice"] = f.CommitChoice
  307. c.Data["new_branch_name"] = branchName
  308. if c.HasError() {
  309. c.HTML(http.StatusBadRequest, tmplEditorDelete)
  310. return
  311. }
  312. if oldBranchName != branchName {
  313. if _, err := c.Repo.Repository.GetBranch(branchName); err == nil {
  314. c.FormErr("NewBranchName")
  315. c.RenderWithErr(c.Tr("repo.editor.branch_already_exists", branchName), http.StatusUnprocessableEntity, tmplEditorDelete, &f)
  316. return
  317. }
  318. }
  319. message := strings.TrimSpace(f.CommitSummary)
  320. if message == "" {
  321. message = c.Tr("repo.editor.delete", c.Repo.TreePath)
  322. }
  323. f.CommitMessage = strings.TrimSpace(f.CommitMessage)
  324. if len(f.CommitMessage) > 0 {
  325. message += "\n\n" + f.CommitMessage
  326. }
  327. if err := c.Repo.Repository.DeleteRepoFile(c.User, database.DeleteRepoFileOptions{
  328. LastCommitID: c.Repo.CommitID,
  329. OldBranch: oldBranchName,
  330. NewBranch: branchName,
  331. TreePath: c.Repo.TreePath,
  332. Message: message,
  333. }); err != nil {
  334. log.Error("Failed to delete repo file: %v", err)
  335. c.RenderWithErr(c.Tr("repo.editor.fail_to_delete_file", c.Repo.TreePath, errInternalServerError), http.StatusInternalServerError, tmplEditorDelete, &f)
  336. return
  337. }
  338. if f.IsNewBrnach() && c.Repo.PullRequest.Allowed {
  339. c.Redirect(c.Repo.PullRequestURL(oldBranchName, f.NewBranchName))
  340. } else {
  341. c.Flash.Success(c.Tr("repo.editor.file_delete_success", c.Repo.TreePath))
  342. c.Redirect(c.Repo.RepoLink + "/src/" + branchName)
  343. }
  344. }
  345. func renderUploadSettings(c *context.Context) {
  346. c.RequireDropzone()
  347. c.Data["UploadAllowedTypes"] = strings.Join(conf.Repository.Upload.AllowedTypes, ",")
  348. c.Data["UploadMaxSize"] = conf.Repository.Upload.FileMaxSize
  349. c.Data["UploadMaxFiles"] = conf.Repository.Upload.MaxFiles
  350. }
  351. func UploadFile(c *context.Context) {
  352. c.PageIs("Upload")
  353. renderUploadSettings(c)
  354. treeNames, treePaths := getParentTreeFields(c.Repo.TreePath)
  355. if len(treeNames) == 0 {
  356. // We must at least have one element for user to input.
  357. treeNames = []string{""}
  358. }
  359. c.Data["TreeNames"] = treeNames
  360. c.Data["TreePaths"] = treePaths
  361. c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + c.Repo.BranchName
  362. c.Data["commit_summary"] = ""
  363. c.Data["commit_message"] = ""
  364. c.Data["commit_choice"] = "direct"
  365. c.Data["new_branch_name"] = ""
  366. c.Success(tmplEditorUpload)
  367. }
  368. func UploadFilePost(c *context.Context, f form.UploadRepoFile) {
  369. c.PageIs("Upload")
  370. renderUploadSettings(c)
  371. oldBranchName := c.Repo.BranchName
  372. branchName := oldBranchName
  373. if f.IsNewBrnach() {
  374. branchName = f.NewBranchName
  375. }
  376. // 🚨 SECURITY: Prevent path traversal.
  377. f.TreePath = pathutil.Clean(f.TreePath)
  378. treeNames, treePaths := getParentTreeFields(f.TreePath)
  379. if len(treeNames) == 0 {
  380. // We must at least have one element for user to input.
  381. treeNames = []string{""}
  382. }
  383. c.Data["TreePath"] = f.TreePath
  384. c.Data["TreeNames"] = treeNames
  385. c.Data["TreePaths"] = treePaths
  386. c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + branchName
  387. c.Data["commit_summary"] = f.CommitSummary
  388. c.Data["commit_message"] = f.CommitMessage
  389. c.Data["commit_choice"] = f.CommitChoice
  390. c.Data["new_branch_name"] = branchName
  391. if c.HasError() {
  392. c.HTML(http.StatusBadRequest, tmplEditorUpload)
  393. return
  394. }
  395. if oldBranchName != branchName {
  396. if _, err := c.Repo.Repository.GetBranch(branchName); err == nil {
  397. c.FormErr("NewBranchName")
  398. c.RenderWithErr(c.Tr("repo.editor.branch_already_exists", branchName), http.StatusUnprocessableEntity, tmplEditorUpload, &f)
  399. return
  400. }
  401. }
  402. var newTreePath string
  403. for _, part := range treeNames {
  404. newTreePath = path.Join(newTreePath, part)
  405. entry, err := c.Repo.Commit.TreeEntry(newTreePath)
  406. if err != nil {
  407. if gitutil.IsErrRevisionNotExist(err) {
  408. // Means there is no item with that name, so we're good
  409. break
  410. }
  411. c.Error(err, "get tree entry")
  412. return
  413. }
  414. // User can only upload files to a directory.
  415. if !entry.IsTree() {
  416. c.FormErr("TreePath")
  417. c.RenderWithErr(c.Tr("repo.editor.directory_is_a_file", part), http.StatusUnprocessableEntity, tmplEditorUpload, &f)
  418. return
  419. }
  420. }
  421. message := strings.TrimSpace(f.CommitSummary)
  422. if message == "" {
  423. message = c.Tr("repo.editor.upload_files_to_dir", f.TreePath)
  424. }
  425. f.CommitMessage = strings.TrimSpace(f.CommitMessage)
  426. if len(f.CommitMessage) > 0 {
  427. message += "\n\n" + f.CommitMessage
  428. }
  429. if err := c.Repo.Repository.UploadRepoFiles(c.User, database.UploadRepoFileOptions{
  430. LastCommitID: c.Repo.CommitID,
  431. OldBranch: oldBranchName,
  432. NewBranch: branchName,
  433. TreePath: f.TreePath,
  434. Message: message,
  435. Files: f.Files,
  436. }); err != nil {
  437. log.Error("Failed to upload files: %v", err)
  438. c.FormErr("TreePath")
  439. c.RenderWithErr(c.Tr("repo.editor.unable_to_upload_files", f.TreePath, errInternalServerError), http.StatusInternalServerError, tmplEditorUpload, &f)
  440. return
  441. }
  442. if f.IsNewBrnach() && c.Repo.PullRequest.Allowed {
  443. c.Redirect(c.Repo.PullRequestURL(oldBranchName, f.NewBranchName))
  444. } else {
  445. c.Redirect(c.Repo.RepoLink + "/src/" + branchName + "/" + f.TreePath)
  446. }
  447. }
  448. func UploadFileToServer(c *context.Context) {
  449. file, header, err := c.Req.FormFile("file")
  450. if err != nil {
  451. c.Error(err, "get file")
  452. return
  453. }
  454. defer file.Close()
  455. buf := make([]byte, 1024)
  456. n, _ := file.Read(buf)
  457. if n > 0 {
  458. buf = buf[:n]
  459. }
  460. fileType := http.DetectContentType(buf)
  461. if len(conf.Repository.Upload.AllowedTypes) > 0 {
  462. allowed := false
  463. for _, t := range conf.Repository.Upload.AllowedTypes {
  464. t := strings.Trim(t, " ")
  465. if t == "*/*" || t == fileType {
  466. allowed = true
  467. break
  468. }
  469. }
  470. if !allowed {
  471. c.PlainText(http.StatusBadRequest, ErrFileTypeForbidden.Error())
  472. return
  473. }
  474. }
  475. upload, err := database.NewUpload(header.Filename, buf, file)
  476. if err != nil {
  477. c.Error(err, "new upload")
  478. return
  479. }
  480. log.Trace("New file uploaded by user[%d]: %s", c.UserID(), upload.UUID)
  481. c.JSONSuccess(map[string]string{
  482. "uuid": upload.UUID,
  483. })
  484. }
  485. func RemoveUploadFileFromServer(c *context.Context, f form.RemoveUploadFile) {
  486. if f.File == "" {
  487. c.Status(http.StatusNoContent)
  488. return
  489. }
  490. if err := database.DeleteUploadByUUID(f.File); err != nil {
  491. c.Error(err, "delete upload by UUID")
  492. return
  493. }
  494. log.Trace("Upload file removed: %s", f.File)
  495. c.Status(http.StatusNoContent)
  496. }