Thanks to visit codestin.com
Credit goes to github.com

Skip to content

repo: prevent data loss by checking directory existence before creati…#8091

Merged
unknwon merged 8 commits intogogs:mainfrom
maishivamhoo123:final-fix-8085
Jan 19, 2026
Merged

repo: prevent data loss by checking directory existence before creati…#8091
unknwon merged 8 commits intogogs:mainfrom
maishivamhoo123:final-fix-8085

Conversation

@maishivamhoo123
Copy link
Contributor

@maishivamhoo123 maishivamhoo123 commented Jan 18, 2026

Describe the pull request

This PR fixes a data loss vulnerability where Gogs would delete an existing directory on the filesystem if a user tried to create a repository with the same name.
#8085

By moving the path existence check to the beginning of CreateRepository, the process exits before the initialization phase. This prevents the execution of RemoveAllWithNotice during a rollback, ensuring existing data remains untouched.

Link to the issue: closes #8085
gogs

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code or have provided the test plan.
  • I have added an entry to CHANGELOG.

Test plan

  1. Manual Test: - Created a folder secret-project.git with a file secret.txt inside the repository root via terminal.
    • Attempted to create a repo named secret-project in the UI.
    • Result: Received "directory already exists" error; secret.txt survived.
  2. Automated Test: - Added and ran Test_CreateRepository_PreventDeletion in internal/database/repo_test.go.
    • Result: PASS

Copy link
Member

@unknwon unknwon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@maishivamhoo123
Copy link
Contributor Author

maishivamhoo123 commented Jan 18, 2026

"Hi @unknwon Sir ,
I have applied the suggested changes (using pkg/errors and cleaning up the test formatting).

I discovered that the tests were failing in CI because the mock user was hitting the repository creation limit before reaching the path existence check. I've updated the test case to use an Admin user to bypass those initial constraints. The tests should pass now. Could you please take another look?

@unknwon
Copy link
Member

unknwon commented Jan 18, 2026

@maishivamhoo123 the test is failing because you're mocking a user entry without setting its MaxRepoCreation, if you set it to -1, should mean unlimited.

@maishivamhoo123
Copy link
Contributor Author

@unknwon can please review it? all the test cases are running properly and made all the requested changes.

Copy link
Member

@unknwon unknwon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@unknwon unknwon merged commit beeeb64 into gogs:main Jan 19, 2026
12 checks passed
@codecov
Copy link

codecov bot commented Jan 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (3650b32) to head (d6e7033).
⚠️ Report is 164 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #8091       +/-   ##
==========================================
- Coverage   15.28%       0   -15.29%     
==========================================
  Files         106       0      -106     
  Lines       13864       0    -13864     
==========================================
- Hits         2119       0     -2119     
+ Misses      11465       0    -11465     
+ Partials      280       0      -280     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding repository which exists in repo ROOT but not in DB removes its files

2 participants