Skip to content

Added ALWAYS_RENDER_RAW_FILES option to the repository section #685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions conf/app.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ PULL_REQUEST_QUEUE_LENGTH = 1000
PREFERRED_LICENSES = Apache License 2.0,MIT License
; Disable ability to interact with repositories by HTTP protocol
DISABLE_HTTP_GIT = false
; Force correct content-type on raw files.
; This can be a security issue if improperly used because it allows html from a repo to run on the same domain as gitea.
ALWAYS_RENDER_RAW_FILES = false

[repository.editor]
; List of file extensions that should have line wraps in the CodeMirror editor
Expand Down
4 changes: 4 additions & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ var (
PullRequestQueueLength int
PreferredLicenses []string
DisableHTTPGit bool
AlwaysRenderRawFiles bool

// Repository editor settings
Editor struct {
Expand All @@ -179,6 +180,7 @@ var (
PullRequestQueueLength: 1000,
PreferredLicenses: []string{"Apache License 2.0,MIT License"},
DisableHTTPGit: false,
AlwaysRenderRawFiles: false,

// Repository editor settings
Editor: struct {
Expand Down Expand Up @@ -824,6 +826,8 @@ please consider changing to GITEA_CUSTOM`)
if !filepath.IsAbs(Repository.Upload.TempPath) {
Repository.Upload.TempPath = path.Join(workDir, Repository.Upload.TempPath)
}

Repository.AlwaysRenderRawFiles = sec.Key("ALWAYS_RENDER_RAW_FILES").MustBool()
Copy link
Member

Choose a reason for hiding this comment

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

You need also update app.ini configuration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@appleboy Done.


sec = Cfg.Section("picture")
AvatarUploadPath = sec.Key("AVATAR_UPLOAD_PATH").MustString(path.Join(AppDataPath, "avatars"))
Expand Down
11 changes: 10 additions & 1 deletion routers/repo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (
"fmt"
"io"
"strings"
"path/filepath"
"mime"

"code.gitea.io/git"

"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/context"
)

Expand All @@ -28,7 +31,13 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error {
// Google Chrome dislike commas in filenames, so let's change it to a space
name = strings.Replace(name, ",", " ", -1)

if base.IsTextFile(buf) || ctx.QueryBool("render") {
if setting.Repository.AlwaysRenderRawFiles {
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name))
mimetype := mime.TypeByExtension(filepath.Ext(name))
if mimetype != "" {
ctx.Resp.Header().Set("Content-Type", mimetype)
}
} else if base.IsTextFile(buf) || ctx.QueryBool("render") {
ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
} else if base.IsImageFile(buf) || base.IsPDFFile(buf) {
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name))
Expand Down