Skip to content

Fix template path possible symlink#36731

Closed
lunny wants to merge 1 commit intogo-gitea:mainfrom
lunny:lunny/fix_template_gen_link
Closed

Fix template path possible symlink#36731
lunny wants to merge 1 commit intogo-gitea:mainfrom
lunny:lunny/fix_template_gen_link

Conversation

@lunny
Copy link
Copy Markdown
Member

@lunny lunny commented Feb 24, 2026

os.Lstat will only check the symlink file. But it cannot avoid some directory of the file path is a symlink. This PR fix the possible symlink directory.

@lunny lunny added type/bug backport/v1.25 This PR should be backported to Gitea 1.25 labels Feb 24, 2026
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 24, 2026
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Feb 24, 2026
@silverwind silverwind changed the title Fix template path possible symblink Fix template path possible symlink Feb 24, 2026
@silverwind silverwind requested a review from Copilot February 24, 2026 06:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens repository template expansion to prevent writes through symlinked directories in the generated path, addressing a symlink-based path traversal risk during .gitea/template processing.

Changes:

  • Add a per-path-component symlink check (ensureNoSymlinkInPath) before writing substituted template files.
  • Extend TestProcessGiteaTemplateFile with a new case covering a symlinked destination directory pointing outside the temp repo.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
services/repository/generate.go Adds ensureNoSymlinkInPath and uses it to block template writes into symlinked paths.
services/repository/generate_test.go Adds a regression test ensuring symlinked directories cannot cause out-of-tree writes.
Comments suppressed due to low confidence (1)

services/repository/generate.go:183

  • ensureNoSymlinkInPath errors are currently swallowed (return nil) which can hide real filesystem problems (e.g. permission/IO errors) and silently drop a templated file after tmpFullPath has already been removed. Consider only skipping on the specific “symlink detected / escapes base” conditions (and optionally logging), while returning unexpected errors to fail the template processing.
	if err := ensureNoSymlinkInPath(tmpDir, newLocalPath); err != nil {
		return nil
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wxiaoguang
Copy link
Copy Markdown
Contributor

wxiaoguang commented Feb 24, 2026

Fix path resolving #36734

generatedContent := generateExpansion(ctx, string(content), templateRepo, generateRepo)
substSubPath := filePathSanitize(generateExpansion(ctx, tmpDirSubPath, templateRepo, generateRepo))
newLocalPath := filepath.Join(tmpDir, substSubPath)
if err := ensureNoSymlinkInPath(tmpDir, newLocalPath); err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The check is incomplete

@wxiaoguang wxiaoguang closed this Feb 24, 2026
@lunny lunny deleted the lunny/fix_template_gen_link branch February 24, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/v1.25 This PR should be backported to Gitea 1.25 lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/go Pull requests that update Go code type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants