repo_editor.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "fmt"
  7. "io"
  8. "mime/multipart"
  9. "os"
  10. "os/exec"
  11. "path"
  12. "path/filepath"
  13. "strings"
  14. "time"
  15. "github.com/pkg/errors"
  16. gouuid "github.com/satori/go.uuid"
  17. "github.com/unknwon/com"
  18. "github.com/gogs/git-module"
  19. "gogs.io/gogs/internal/conf"
  20. "gogs.io/gogs/internal/cryptoutil"
  21. dberrors "gogs.io/gogs/internal/db/errors"
  22. "gogs.io/gogs/internal/gitutil"
  23. "gogs.io/gogs/internal/osutil"
  24. "gogs.io/gogs/internal/pathutil"
  25. "gogs.io/gogs/internal/process"
  26. "gogs.io/gogs/internal/tool"
  27. )
  28. const (
  29. ENV_AUTH_USER_ID = "GOGS_AUTH_USER_ID"
  30. ENV_AUTH_USER_NAME = "GOGS_AUTH_USER_NAME"
  31. ENV_AUTH_USER_EMAIL = "GOGS_AUTH_USER_EMAIL"
  32. ENV_REPO_OWNER_NAME = "GOGS_REPO_OWNER_NAME"
  33. ENV_REPO_OWNER_SALT_MD5 = "GOGS_REPO_OWNER_SALT_MD5"
  34. ENV_REPO_ID = "GOGS_REPO_ID"
  35. ENV_REPO_NAME = "GOGS_REPO_NAME"
  36. ENV_REPO_CUSTOM_HOOKS_PATH = "GOGS_REPO_CUSTOM_HOOKS_PATH"
  37. )
  38. type ComposeHookEnvsOptions struct {
  39. AuthUser *User
  40. OwnerName string
  41. OwnerSalt string
  42. RepoID int64
  43. RepoName string
  44. RepoPath string
  45. }
  46. func ComposeHookEnvs(opts ComposeHookEnvsOptions) []string {
  47. envs := []string{
  48. "SSH_ORIGINAL_COMMAND=1",
  49. ENV_AUTH_USER_ID + "=" + com.ToStr(opts.AuthUser.ID),
  50. ENV_AUTH_USER_NAME + "=" + opts.AuthUser.Name,
  51. ENV_AUTH_USER_EMAIL + "=" + opts.AuthUser.Email,
  52. ENV_REPO_OWNER_NAME + "=" + opts.OwnerName,
  53. ENV_REPO_OWNER_SALT_MD5 + "=" + cryptoutil.MD5(opts.OwnerSalt),
  54. ENV_REPO_ID + "=" + com.ToStr(opts.RepoID),
  55. ENV_REPO_NAME + "=" + opts.RepoName,
  56. ENV_REPO_CUSTOM_HOOKS_PATH + "=" + filepath.Join(opts.RepoPath, "custom_hooks"),
  57. }
  58. return envs
  59. }
  60. // ___________ .___.__ __ ___________.__.__
  61. // \_ _____/ __| _/|__|/ |_ \_ _____/|__| | ____
  62. // | __)_ / __ | | \ __\ | __) | | | _/ __ \
  63. // | \/ /_/ | | || | | \ | | |_\ ___/
  64. // /_______ /\____ | |__||__| \___ / |__|____/\___ >
  65. // \/ \/ \/ \/
  66. // discardLocalRepoBranchChanges discards local commits/changes of
  67. // given branch to make sure it is even to remote branch.
  68. func discardLocalRepoBranchChanges(localPath, branch string) error {
  69. if !com.IsExist(localPath) {
  70. return nil
  71. }
  72. // No need to check if nothing in the repository.
  73. if !git.RepoHasBranch(localPath, branch) {
  74. return nil
  75. }
  76. rev := "origin/" + branch
  77. if err := git.Reset(localPath, rev, git.ResetOptions{Hard: true}); err != nil {
  78. return fmt.Errorf("reset [revision: %s]: %v", rev, err)
  79. }
  80. return nil
  81. }
  82. func (repo *Repository) DiscardLocalRepoBranchChanges(branch string) error {
  83. return discardLocalRepoBranchChanges(repo.LocalCopyPath(), branch)
  84. }
  85. // CheckoutNewBranch checks out to a new branch from the a branch name.
  86. func (repo *Repository) CheckoutNewBranch(oldBranch, newBranch string) error {
  87. if err := git.Checkout(repo.LocalCopyPath(), newBranch, git.CheckoutOptions{
  88. BaseBranch: oldBranch,
  89. Timeout: time.Duration(conf.Git.Timeout.Pull) * time.Second,
  90. }); err != nil {
  91. return fmt.Errorf("checkout [base: %s, new: %s]: %v", oldBranch, newBranch, err)
  92. }
  93. return nil
  94. }
  95. type UpdateRepoFileOptions struct {
  96. OldBranch string
  97. NewBranch string
  98. OldTreeName string
  99. NewTreeName string
  100. Message string
  101. Content string
  102. IsNewFile bool
  103. }
  104. // UpdateRepoFile adds or updates a file in repository.
  105. func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (err error) {
  106. // 🚨 SECURITY: Prevent uploading files into the ".git" directory.
  107. if isRepositoryGitPath(opts.NewTreeName) {
  108. return errors.Errorf("bad tree path %q", opts.NewTreeName)
  109. }
  110. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  111. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  112. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  113. return fmt.Errorf("discard local repo branch[%s] changes: %v", opts.OldBranch, err)
  114. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  115. return fmt.Errorf("update local copy branch[%s]: %v", opts.OldBranch, err)
  116. }
  117. repoPath := repo.RepoPath()
  118. localPath := repo.LocalCopyPath()
  119. if opts.OldBranch != opts.NewBranch {
  120. // Directly return error if new branch already exists in the server
  121. if git.RepoHasBranch(repoPath, opts.NewBranch) {
  122. return dberrors.BranchAlreadyExists{Name: opts.NewBranch}
  123. }
  124. // Otherwise, delete branch from local copy in case out of sync
  125. if git.RepoHasBranch(localPath, opts.NewBranch) {
  126. if err = git.DeleteBranch(localPath, opts.NewBranch, git.DeleteBranchOptions{
  127. Force: true,
  128. }); err != nil {
  129. return fmt.Errorf("delete branch %q: %v", opts.NewBranch, err)
  130. }
  131. }
  132. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  133. return fmt.Errorf("checkout new branch[%s] from old branch[%s]: %v", opts.NewBranch, opts.OldBranch, err)
  134. }
  135. }
  136. oldFilePath := path.Join(localPath, opts.OldTreeName)
  137. filePath := path.Join(localPath, opts.NewTreeName)
  138. if err = os.MkdirAll(path.Dir(filePath), os.ModePerm); err != nil {
  139. return err
  140. }
  141. // If it's meant to be a new file, make sure it doesn't exist.
  142. if opts.IsNewFile {
  143. // 🚨 SECURITY: Prevent updating files in surprising place, check if the file is
  144. // a symlink.
  145. if osutil.IsSymlink(filePath) {
  146. return fmt.Errorf("cannot update symbolic link: %s", opts.NewTreeName)
  147. }
  148. if osutil.IsExist(filePath) {
  149. return ErrRepoFileAlreadyExist{filePath}
  150. }
  151. }
  152. // Ignore move step if it's a new file under a directory.
  153. // Otherwise, move the file when name changed.
  154. if osutil.IsFile(oldFilePath) && opts.OldTreeName != opts.NewTreeName {
  155. // 🚨 SECURITY: Prevent updating files in surprising place, check if the file is
  156. // a symlink.
  157. if osutil.IsSymlink(oldFilePath) {
  158. return fmt.Errorf("cannot move symbolic link: %s", opts.OldTreeName)
  159. }
  160. if err = git.Move(localPath, opts.OldTreeName, opts.NewTreeName); err != nil {
  161. return fmt.Errorf("git mv %q %q: %v", opts.OldTreeName, opts.NewTreeName, err)
  162. }
  163. }
  164. if err = os.WriteFile(filePath, []byte(opts.Content), 0600); err != nil {
  165. return fmt.Errorf("write file: %v", err)
  166. }
  167. if err = git.Add(localPath, git.AddOptions{All: true}); err != nil {
  168. return fmt.Errorf("git add --all: %v", err)
  169. }
  170. err = git.CreateCommit(
  171. localPath,
  172. &git.Signature{
  173. Name: doer.DisplayName(),
  174. Email: doer.Email,
  175. When: time.Now(),
  176. },
  177. opts.Message,
  178. )
  179. if err != nil {
  180. return fmt.Errorf("commit changes on %q: %v", localPath, err)
  181. }
  182. err = git.Push(localPath, "origin", opts.NewBranch,
  183. git.PushOptions{
  184. CommandOptions: git.CommandOptions{
  185. Envs: ComposeHookEnvs(ComposeHookEnvsOptions{
  186. AuthUser: doer,
  187. OwnerName: repo.MustOwner().Name,
  188. OwnerSalt: repo.MustOwner().Salt,
  189. RepoID: repo.ID,
  190. RepoName: repo.Name,
  191. RepoPath: repo.RepoPath(),
  192. }),
  193. },
  194. },
  195. )
  196. if err != nil {
  197. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  198. }
  199. return nil
  200. }
  201. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
  202. func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *gitutil.Diff, err error) {
  203. // 🚨 SECURITY: Prevent uploading files into the ".git" directory.
  204. if isRepositoryGitPath(treePath) {
  205. return nil, errors.Errorf("bad tree path %q", treePath)
  206. }
  207. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  208. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  209. if err = repo.DiscardLocalRepoBranchChanges(branch); err != nil {
  210. return nil, fmt.Errorf("discard local repo branch[%s] changes: %v", branch, err)
  211. } else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
  212. return nil, fmt.Errorf("update local copy branch[%s]: %v", branch, err)
  213. }
  214. localPath := repo.LocalCopyPath()
  215. filePath := path.Join(localPath, treePath)
  216. // 🚨 SECURITY: Prevent updating files in surprising place, check if the target is
  217. // a symlink.
  218. if osutil.IsSymlink(filePath) {
  219. return nil, fmt.Errorf("cannot get diff preview for symbolic link: %s", treePath)
  220. }
  221. if err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
  222. return nil, err
  223. } else if err = os.WriteFile(filePath, []byte(content), 0600); err != nil {
  224. return nil, fmt.Errorf("write file: %v", err)
  225. }
  226. // 🚨 SECURITY: Prevent including unintended options in the path to the Git command.
  227. cmd := exec.Command("git", "diff", "--end-of-options", treePath)
  228. cmd.Dir = localPath
  229. cmd.Stderr = os.Stderr
  230. stdout, err := cmd.StdoutPipe()
  231. if err != nil {
  232. return nil, fmt.Errorf("get stdout pipe: %v", err)
  233. }
  234. if err = cmd.Start(); err != nil {
  235. return nil, fmt.Errorf("start: %v", err)
  236. }
  237. pid := process.Add(fmt.Sprintf("GetDiffPreview [repo_path: %s]", repo.RepoPath()), cmd)
  238. defer process.Remove(pid)
  239. diff, err = gitutil.ParseDiff(stdout, conf.Git.MaxDiffFiles, conf.Git.MaxDiffLines, conf.Git.MaxDiffLineChars)
  240. if err != nil {
  241. return nil, fmt.Errorf("parse diff: %v", err)
  242. }
  243. if err = cmd.Wait(); err != nil {
  244. return nil, fmt.Errorf("wait: %v", err)
  245. }
  246. return diff, nil
  247. }
  248. // ________ .__ __ ___________.__.__
  249. // \______ \ ____ | | _____/ |_ ____ \_ _____/|__| | ____
  250. // | | \_/ __ \| | _/ __ \ __\/ __ \ | __) | | | _/ __ \
  251. // | ` \ ___/| |_\ ___/| | \ ___/ | \ | | |_\ ___/
  252. // /_______ /\___ >____/\___ >__| \___ > \___ / |__|____/\___ >
  253. // \/ \/ \/ \/ \/ \/
  254. //
  255. type DeleteRepoFileOptions struct {
  256. LastCommitID string
  257. OldBranch string
  258. NewBranch string
  259. TreePath string
  260. Message string
  261. }
  262. func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (err error) {
  263. // 🚨 SECURITY: Prevent uploading files into the ".git" directory.
  264. if isRepositoryGitPath(opts.TreePath) {
  265. return errors.Errorf("bad tree path %q", opts.TreePath)
  266. }
  267. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  268. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  269. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  270. return fmt.Errorf("discard local repo branch[%s] changes: %v", opts.OldBranch, err)
  271. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  272. return fmt.Errorf("update local copy branch[%s]: %v", opts.OldBranch, err)
  273. }
  274. if opts.OldBranch != opts.NewBranch {
  275. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  276. return fmt.Errorf("checkout new branch[%s] from old branch[%s]: %v", opts.NewBranch, opts.OldBranch, err)
  277. }
  278. }
  279. localPath := repo.LocalCopyPath()
  280. filePath := path.Join(localPath, opts.TreePath)
  281. // 🚨 SECURITY: Prevent updating files in surprising place, check if the file is
  282. // a symlink.
  283. if osutil.IsSymlink(filePath) {
  284. return fmt.Errorf("cannot delete symbolic link: %s", opts.TreePath)
  285. }
  286. if err = os.Remove(filePath); err != nil {
  287. return fmt.Errorf("remove file %q: %v", opts.TreePath, err)
  288. }
  289. if err = git.Add(localPath, git.AddOptions{All: true}); err != nil {
  290. return fmt.Errorf("git add --all: %v", err)
  291. }
  292. err = git.CreateCommit(
  293. localPath,
  294. &git.Signature{
  295. Name: doer.DisplayName(),
  296. Email: doer.Email,
  297. When: time.Now(),
  298. },
  299. opts.Message,
  300. )
  301. if err != nil {
  302. return fmt.Errorf("commit changes to %q: %v", localPath, err)
  303. }
  304. err = git.Push(localPath, "origin", opts.NewBranch,
  305. git.PushOptions{
  306. CommandOptions: git.CommandOptions{
  307. Envs: ComposeHookEnvs(ComposeHookEnvsOptions{
  308. AuthUser: doer,
  309. OwnerName: repo.MustOwner().Name,
  310. OwnerSalt: repo.MustOwner().Salt,
  311. RepoID: repo.ID,
  312. RepoName: repo.Name,
  313. RepoPath: repo.RepoPath(),
  314. }),
  315. },
  316. },
  317. )
  318. if err != nil {
  319. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  320. }
  321. return nil
  322. }
  323. // ____ ___ .__ .___ ___________.___.__
  324. // | | \______ | | _________ __| _/ \_ _____/| | | ____ ______
  325. // | | /\____ \| | / _ \__ \ / __ | | __) | | | _/ __ \ / ___/
  326. // | | / | |_> > |_( <_> ) __ \_/ /_/ | | \ | | |_\ ___/ \___ \
  327. // |______/ | __/|____/\____(____ /\____ | \___ / |___|____/\___ >____ >
  328. // |__| \/ \/ \/ \/ \/
  329. //
  330. // Upload represent a uploaded file to a repo to be deleted when moved
  331. type Upload struct {
  332. ID int64
  333. UUID string `xorm:"uuid UNIQUE"`
  334. Name string
  335. }
  336. // UploadLocalPath returns where uploads is stored in local file system based on given UUID.
  337. func UploadLocalPath(uuid string) string {
  338. return path.Join(conf.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
  339. }
  340. // LocalPath returns where uploads are temporarily stored in local file system.
  341. func (upload *Upload) LocalPath() string {
  342. return UploadLocalPath(upload.UUID)
  343. }
  344. // NewUpload creates a new upload object.
  345. func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err error) {
  346. if tool.IsMaliciousPath(name) {
  347. return nil, fmt.Errorf("malicious path detected: %s", name)
  348. }
  349. upload := &Upload{
  350. UUID: gouuid.NewV4().String(),
  351. Name: name,
  352. }
  353. localPath := upload.LocalPath()
  354. if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
  355. return nil, fmt.Errorf("mkdir all: %v", err)
  356. }
  357. fw, err := os.Create(localPath)
  358. if err != nil {
  359. return nil, fmt.Errorf("create: %v", err)
  360. }
  361. defer func() { _ = fw.Close() }()
  362. if _, err = fw.Write(buf); err != nil {
  363. return nil, fmt.Errorf("write: %v", err)
  364. } else if _, err = io.Copy(fw, file); err != nil {
  365. return nil, fmt.Errorf("copy: %v", err)
  366. }
  367. if _, err := x.Insert(upload); err != nil {
  368. return nil, err
  369. }
  370. return upload, nil
  371. }
  372. func GetUploadByUUID(uuid string) (*Upload, error) {
  373. upload := &Upload{UUID: uuid}
  374. has, err := x.Get(upload)
  375. if err != nil {
  376. return nil, err
  377. } else if !has {
  378. return nil, ErrUploadNotExist{0, uuid}
  379. }
  380. return upload, nil
  381. }
  382. func GetUploadsByUUIDs(uuids []string) ([]*Upload, error) {
  383. if len(uuids) == 0 {
  384. return []*Upload{}, nil
  385. }
  386. // Silently drop invalid uuids.
  387. uploads := make([]*Upload, 0, len(uuids))
  388. return uploads, x.In("uuid", uuids).Find(&uploads)
  389. }
  390. func DeleteUploads(uploads ...*Upload) (err error) {
  391. if len(uploads) == 0 {
  392. return nil
  393. }
  394. sess := x.NewSession()
  395. defer sess.Close()
  396. if err = sess.Begin(); err != nil {
  397. return err
  398. }
  399. ids := make([]int64, len(uploads))
  400. for i := 0; i < len(uploads); i++ {
  401. ids[i] = uploads[i].ID
  402. }
  403. if _, err = sess.In("id", ids).Delete(new(Upload)); err != nil {
  404. return fmt.Errorf("delete uploads: %v", err)
  405. }
  406. for _, upload := range uploads {
  407. localPath := upload.LocalPath()
  408. if !osutil.IsFile(localPath) {
  409. continue
  410. }
  411. if err := os.Remove(localPath); err != nil {
  412. return fmt.Errorf("remove upload: %v", err)
  413. }
  414. }
  415. return sess.Commit()
  416. }
  417. func DeleteUpload(u *Upload) error {
  418. return DeleteUploads(u)
  419. }
  420. func DeleteUploadByUUID(uuid string) error {
  421. upload, err := GetUploadByUUID(uuid)
  422. if err != nil {
  423. if IsErrUploadNotExist(err) {
  424. return nil
  425. }
  426. return fmt.Errorf("get upload by UUID[%s]: %v", uuid, err)
  427. }
  428. if err := DeleteUpload(upload); err != nil {
  429. return fmt.Errorf("delete upload: %v", err)
  430. }
  431. return nil
  432. }
  433. type UploadRepoFileOptions struct {
  434. LastCommitID string
  435. OldBranch string
  436. NewBranch string
  437. TreePath string
  438. Message string
  439. Files []string // In UUID format
  440. }
  441. // isRepositoryGitPath returns true if given path is or resides inside ".git"
  442. // path of the repository.
  443. //
  444. // TODO(unknwon): Move to repoutil during refactoring for this file.
  445. func isRepositoryGitPath(path string) bool {
  446. path = strings.ToLower(path)
  447. return strings.HasSuffix(path, ".git") ||
  448. strings.Contains(path, ".git/") ||
  449. strings.Contains(path, `.git\`) ||
  450. // Windows treats ".git." the same as ".git"
  451. strings.HasSuffix(path, ".git.") ||
  452. strings.Contains(path, ".git./") ||
  453. strings.Contains(path, `.git.\`)
  454. }
  455. func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions) error {
  456. if len(opts.Files) == 0 {
  457. return nil
  458. }
  459. // 🚨 SECURITY: Prevent uploading files into the ".git" directory.
  460. if isRepositoryGitPath(opts.TreePath) {
  461. return errors.Errorf("bad tree path %q", opts.TreePath)
  462. }
  463. uploads, err := GetUploadsByUUIDs(opts.Files)
  464. if err != nil {
  465. return fmt.Errorf("get uploads by UUIDs[%v]: %v", opts.Files, err)
  466. }
  467. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  468. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  469. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  470. return fmt.Errorf("discard local repo branch[%s] changes: %v", opts.OldBranch, err)
  471. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  472. return fmt.Errorf("update local copy branch[%s]: %v", opts.OldBranch, err)
  473. }
  474. if opts.OldBranch != opts.NewBranch {
  475. if err = repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  476. return fmt.Errorf("checkout new branch[%s] from old branch[%s]: %v", opts.NewBranch, opts.OldBranch, err)
  477. }
  478. }
  479. localPath := repo.LocalCopyPath()
  480. dirPath := path.Join(localPath, opts.TreePath)
  481. if err = os.MkdirAll(dirPath, os.ModePerm); err != nil {
  482. return err
  483. }
  484. // Copy uploaded files into repository
  485. for _, upload := range uploads {
  486. tmpPath := upload.LocalPath()
  487. if !osutil.IsFile(tmpPath) {
  488. continue
  489. }
  490. // 🚨 SECURITY: Prevent path traversal.
  491. upload.Name = pathutil.Clean(upload.Name)
  492. // 🚨 SECURITY: Prevent uploading files into the ".git" directory.
  493. if isRepositoryGitPath(upload.Name) {
  494. continue
  495. }
  496. targetPath := path.Join(dirPath, upload.Name)
  497. // 🚨 SECURITY: Prevent updating files in surprising place, check if the target
  498. // is a symlink.
  499. if osutil.IsSymlink(targetPath) {
  500. return fmt.Errorf("cannot overwrite symbolic link: %s", upload.Name)
  501. }
  502. if err = com.Copy(tmpPath, targetPath); err != nil {
  503. return fmt.Errorf("copy: %v", err)
  504. }
  505. }
  506. if err = git.Add(localPath, git.AddOptions{All: true}); err != nil {
  507. return fmt.Errorf("git add --all: %v", err)
  508. }
  509. err = git.CreateCommit(
  510. localPath,
  511. &git.Signature{
  512. Name: doer.DisplayName(),
  513. Email: doer.Email,
  514. When: time.Now(),
  515. },
  516. opts.Message,
  517. )
  518. if err != nil {
  519. return fmt.Errorf("commit changes on %q: %v", localPath, err)
  520. }
  521. err = git.Push(localPath, "origin", opts.NewBranch,
  522. git.PushOptions{
  523. CommandOptions: git.CommandOptions{
  524. Envs: ComposeHookEnvs(ComposeHookEnvsOptions{
  525. AuthUser: doer,
  526. OwnerName: repo.MustOwner().Name,
  527. OwnerSalt: repo.MustOwner().Salt,
  528. RepoID: repo.ID,
  529. RepoName: repo.Name,
  530. RepoPath: repo.RepoPath(),
  531. }),
  532. },
  533. },
  534. )
  535. if err != nil {
  536. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  537. }
  538. return DeleteUploads(uploads...)
  539. }