Compare commits

...

2 Commits

Author SHA1 Message Date
wxiaoguang
e94f8d56f1
Correctly handle submodule view and avoid throwing 500 error (#34121)
Auto-redirect for in-site links, and show 404 for external links
(to avoid open redirect or phishing)
2025-04-06 09:38:08 +00:00
wxiaoguang
a62ed19da6
Use overflow-wrap: anywhere to replace word-break: break-all (#34126) 2025-04-06 17:13:02 +08:00
7 changed files with 62 additions and 7 deletions

View File

@ -20,6 +20,8 @@ import (
unit_model "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
giturl "code.gitea.io/gitea/modules/git/url"
"code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
@ -302,8 +304,33 @@ func handleRepoEmptyOrBroken(ctx *context.Context) {
ctx.Redirect(link)
}
func handleRepoViewSubmodule(ctx *context.Context, submodule *git.SubModule) {
submoduleRepoURL, err := giturl.ParseRepositoryURL(ctx, submodule.URL)
if err != nil {
HandleGitError(ctx, "prepareToRenderDirOrFile: ParseRepositoryURL", err)
return
}
submoduleURL := giturl.MakeRepositoryWebLink(submoduleRepoURL)
if httplib.IsCurrentGiteaSiteURL(ctx, submoduleURL) {
ctx.RedirectToCurrentSite(submoduleURL)
} else {
// don't auto-redirect to external URL, to avoid open redirect or phishing
ctx.Data["NotFoundPrompt"] = submoduleURL
ctx.NotFound(nil)
}
}
func prepareToRenderDirOrFile(entry *git.TreeEntry) func(ctx *context.Context) {
return func(ctx *context.Context) {
if entry.IsSubModule() {
submodule, err := ctx.Repo.Commit.GetSubModule(entry.Name())
if err != nil {
HandleGitError(ctx, "prepareToRenderDirOrFile: GetSubModule", err)
return
}
handleRepoViewSubmodule(ctx, submodule)
return
}
if entry.IsDir() {
prepareToRenderDirectory(ctx)
} else {

View File

@ -0,0 +1,32 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"net/http"
"testing"
"code.gitea.io/gitea/models/unittest"
git_module "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/contexttest"
"github.com/stretchr/testify/assert"
)
func TestViewHomeSubmoduleRedirect(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "/user2/repo1/src/branch/master/test-submodule")
submodule := &git_module.SubModule{Path: "test-submodule", URL: setting.AppURL + "user2/repo-other.git"}
handleRepoViewSubmodule(ctx, submodule)
assert.Equal(t, http.StatusSeeOther, ctx.Resp.WrittenStatus())
assert.Equal(t, "/user2/repo-other", ctx.Resp.Header().Get("Location"))
ctx, _ = contexttest.MockContext(t, "/user2/repo1/src/branch/master/test-submodule")
submodule = &git_module.SubModule{Path: "test-submodule", URL: "https://other/user2/repo-other.git"}
handleRepoViewSubmodule(ctx, submodule)
// do not auto-redirect for external URLs, to avoid open redirect or phishing
assert.Equal(t, http.StatusNotFound, ctx.Resp.WrittenStatus())
}

View File

@ -38,7 +38,7 @@
{{if .ErrorMsg}}
<div class="tw-mt-8">
<p>{{ctx.Locale.Tr "error.occurred"}}:</p>
<pre class="tw-whitespace-pre-wrap tw-break-all">{{.ErrorMsg}}</pre>
<pre class="tw-whitespace-pre-wrap tw-wrap-anywhere">{{.ErrorMsg}}</pre>
</div>
{{end}}
<div class="tw-mt-8 tw-text-center">

View File

@ -878,7 +878,6 @@ overflow-menu .ui.label {
.code-inner {
font: 12px var(--fonts-monospace);
white-space: pre-wrap;
word-break: break-all;
overflow-wrap: anywhere;
line-height: inherit; /* needed for inline code preview in markup */
}

View File

@ -447,8 +447,7 @@
margin: 0;
font-size: 100%;
white-space: pre-wrap;
word-break: break-all;
overflow-wrap: break-word;
overflow-wrap: anywhere;
background: transparent;
border: 0;
}

View File

@ -1724,8 +1724,7 @@ tbody.commit-list {
line-height: 18px;
margin: 1em;
white-space: pre-wrap;
word-break: break-all;
overflow-wrap: break-word;
overflow-wrap: anywhere;
}
.content-history-detail-dialog .header .avatar {

View File

@ -955,7 +955,6 @@ export default defineComponent({
.job-step-logs .job-log-line .log-msg {
flex: 1;
word-break: break-all;
white-space: break-spaces;
margin-left: 10px;
overflow-wrap: anywhere;