Skip to content

Commit a1dfa2f

Browse files
authored
Merge branch 'main' into fix-fork-owner
2 parents 1f0375b + 5bc030e commit a1dfa2f

File tree

11 files changed

+124
-41
lines changed

11 files changed

+124
-41
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ require (
4848
github.com/ethantkoenig/rupture v1.0.1
4949
github.com/felixge/fgprof v0.9.5
5050
github.com/fsnotify/fsnotify v1.7.0
51-
github.com/gliderlabs/ssh v0.3.7
51+
github.com/gliderlabs/ssh v0.3.8
5252
github.com/go-ap/activitypub v0.0.0-20240910141749-b4b8c8aa484c
5353
github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73
5454
github.com/go-chi/chi/v5 v5.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv
293293
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
294294
github.com/git-lfs/pktline v0.0.0-20230103162542-ca444d533ef1 h1:mtDjlmloH7ytdblogrMz1/8Hqua1y8B4ID+bh3rvod0=
295295
github.com/git-lfs/pktline v0.0.0-20230103162542-ca444d533ef1/go.mod h1:fenKRzpXDjNpsIBhuhUzvjCKlDjKam0boRAenTE0Q6A=
296-
github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE=
297-
github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8=
296+
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
297+
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
298298
github.com/glycerine/go-unsnap-stream v0.0.0-20181221182339-f9677308dec2/go.mod h1:/20jfyN9Y5QPEAprSgKAUr+glWDY39ZiUEAYOEv5dsE=
299299
github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
300300
github.com/go-ap/activitypub v0.0.0-20240910141749-b4b8c8aa484c h1:82lzmsy5Nr6JA6HcLRVxGfbdSoWfW45C6jnY3zFS7Ks=

modules/markup/markdown/markdown_math_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ func TestMathRender(t *testing.T) {
2020
}{
2121
{
2222
"$a$",
23-
`<p><code class="language-math is-loading">a</code></p>` + nl,
23+
`<p><code class="language-math">a</code></p>` + nl,
2424
},
2525
{
2626
"$ a $",
27-
`<p><code class="language-math is-loading">a</code></p>` + nl,
27+
`<p><code class="language-math">a</code></p>` + nl,
2828
},
2929
{
3030
"$a$ $b$",
31-
`<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl,
31+
`<p><code class="language-math">a</code> <code class="language-math">b</code></p>` + nl,
3232
},
3333
{
3434
`\(a\) \(b\)`,
35-
`<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl,
35+
`<p><code class="language-math">a</code> <code class="language-math">b</code></p>` + nl,
3636
},
3737
{
3838
`$a$.`,
39-
`<p><code class="language-math is-loading">a</code>.</p>` + nl,
39+
`<p><code class="language-math">a</code>.</p>` + nl,
4040
},
4141
{
4242
`.$a$`,
@@ -64,27 +64,27 @@ func TestMathRender(t *testing.T) {
6464
},
6565
{
6666
"$a$ ($b$) [$c$] {$d$}",
67-
`<p><code class="language-math is-loading">a</code> (<code class="language-math is-loading">b</code>) [$c$] {$d$}</p>` + nl,
67+
`<p><code class="language-math">a</code> (<code class="language-math">b</code>) [$c$] {$d$}</p>` + nl,
6868
},
6969
{
7070
"$$a$$",
71-
`<code class="chroma language-math display">a</code>` + nl,
71+
`<code class="language-math display">a</code>` + nl,
7272
},
7373
{
7474
"$$a$$ test",
75-
`<p><code class="language-math display is-loading">a</code> test</p>` + nl,
75+
`<p><code class="language-math">a</code> test</p>` + nl,
7676
},
7777
{
7878
"test $$a$$",
79-
`<p>test <code class="language-math display is-loading">a</code></p>` + nl,
79+
`<p>test <code class="language-math">a</code></p>` + nl,
8080
},
8181
{
8282
`foo $x=\$$ bar`,
83-
`<p>foo <code class="language-math is-loading">x=\$</code> bar</p>` + nl,
83+
`<p>foo <code class="language-math">x=\$</code> bar</p>` + nl,
8484
},
8585
{
8686
`$\text{$b$}$`,
87-
`<p><code class="language-math is-loading">\text{$b$}</code></p>` + nl,
87+
`<p><code class="language-math">\text{$b$}</code></p>` + nl,
8888
},
8989
}
9090

@@ -110,7 +110,7 @@ func TestMathRenderBlockIndent(t *testing.T) {
110110
\alpha
111111
\]
112112
`,
113-
`<pre class="code-block is-loading"><code class="chroma language-math display">
113+
`<pre class="code-block is-loading"><code class="language-math display">
114114
\alpha
115115
</code></pre>
116116
`,
@@ -122,7 +122,7 @@ func TestMathRenderBlockIndent(t *testing.T) {
122122
\alpha
123123
\]
124124
`,
125-
`<pre class="code-block is-loading"><code class="chroma language-math display">
125+
`<pre class="code-block is-loading"><code class="language-math display">
126126
\alpha
127127
</code></pre>
128128
`,
@@ -137,7 +137,7 @@ a
137137
d
138138
\]
139139
`,
140-
`<pre class="code-block is-loading"><code class="chroma language-math display">
140+
`<pre class="code-block is-loading"><code class="language-math display">
141141
a
142142
b
143143
c
@@ -154,7 +154,7 @@ c
154154
c
155155
\]
156156
`,
157-
`<pre class="code-block is-loading"><code class="chroma language-math display">
157+
`<pre class="code-block is-loading"><code class="language-math display">
158158
a
159159
b
160160
c
@@ -165,15 +165,15 @@ c
165165
"indent-0-oneline",
166166
`$$ x $$
167167
foo`,
168-
`<code class="chroma language-math display"> x </code>
168+
`<code class="language-math display"> x </code>
169169
<p>foo</p>
170170
`,
171171
},
172172
{
173173
"indent-3-oneline",
174174
` $$ x $$<SPACE>
175175
foo`,
176-
`<code class="chroma language-math display"> x </code>
176+
`<code class="language-math display"> x </code>
177177
<p>foo</p>
178178
`,
179179
},
@@ -188,10 +188,10 @@ foo`,
188188
> \]
189189
`,
190190
`<blockquote>
191-
<pre class="code-block is-loading"><code class="chroma language-math display">
191+
<pre class="code-block is-loading"><code class="language-math display">
192192
a
193193
</code></pre>
194-
<pre class="code-block is-loading"><code class="chroma language-math display">
194+
<pre class="code-block is-loading"><code class="language-math display">
195195
b
196196
</code></pre>
197197
</blockquote>
@@ -207,7 +207,7 @@ b
207207
2. b`,
208208
`<ol>
209209
<li>a
210-
<pre class="code-block is-loading"><code class="chroma language-math display">
210+
<pre class="code-block is-loading"><code class="language-math display">
211211
x
212212
</code></pre>
213213
</li>

modules/markup/markdown/math/block_renderer.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import (
1212
"github.com/yuin/goldmark/util"
1313
)
1414

15+
// Block render output:
16+
// <pre class="code-block is-loading"><code class="language-math display">...</code></pre>
17+
//
18+
// Keep in mind that there is another "code block" render in "func (r *GlodmarkRender) highlightingRenderer"
19+
// "highlightingRenderer" outputs the math block with extra "chroma" class:
20+
// <pre class="code-block is-loading"><code class="chroma language-math display">...</code></pre>
21+
//
22+
// Special classes:
23+
// * "is-loading": show a loading indicator
24+
// * "display": used by JS to decide to render as a block, otherwise render as inline
25+
1526
// BlockRenderer represents a renderer for math Blocks
1627
type BlockRenderer struct {
1728
renderInternal *internal.RenderInternal
@@ -38,7 +49,7 @@ func (r *BlockRenderer) writeLines(w util.BufWriter, source []byte, n gast.Node)
3849
func (r *BlockRenderer) renderBlock(w util.BufWriter, source []byte, node gast.Node, entering bool) (gast.WalkStatus, error) {
3950
n := node.(*Block)
4051
if entering {
41-
code := giteaUtil.Iif(n.Inline, "", `<pre class="code-block is-loading">`) + `<code class="chroma language-math display">`
52+
code := giteaUtil.Iif(n.Inline, "", `<pre class="code-block is-loading">`) + `<code class="language-math display">`
4253
_ = r.renderInternal.FormatWithSafeAttrs(w, code)
4354
r.writeLines(w, source, n)
4455
} else {

modules/markup/markdown/math/inline_renderer.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313
"github.com/yuin/goldmark/util"
1414
)
1515

16+
// Inline render output:
17+
// <code class="language-math">...</code>
18+
1619
// InlineRenderer is an inline renderer
1720
type InlineRenderer struct {
1821
renderInternal *internal.RenderInternal
@@ -25,11 +28,7 @@ func NewInlineRenderer(renderInternal *internal.RenderInternal) renderer.NodeRen
2528

2629
func (r *InlineRenderer) renderInline(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
2730
if entering {
28-
extraClass := ""
29-
if _, ok := n.(*InlineBlock); ok {
30-
extraClass = "display "
31-
}
32-
_ = r.renderInternal.FormatWithSafeAttrs(w, `<code class="language-math %sis-loading">`, extraClass)
31+
_ = r.renderInternal.FormatWithSafeAttrs(w, `<code class="language-math">`)
3332
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
3433
segment := c.(*ast.Text).Segment
3534
value := util.EscapeHTML(segment.Value(source))

modules/ssh/ssh.go

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import (
1313
"errors"
1414
"fmt"
1515
"io"
16+
"maps"
1617
"net"
1718
"os"
1819
"os/exec"
1920
"path/filepath"
21+
"reflect"
2022
"strconv"
2123
"strings"
2224
"sync"
@@ -33,9 +35,22 @@ import (
3335
gossh "golang.org/x/crypto/ssh"
3436
)
3537

36-
type contextKey string
37-
38-
const giteaKeyID = contextKey("gitea-key-id")
38+
// The ssh auth overall works like this:
39+
// NewServerConn:
40+
// serverHandshake+serverAuthenticate:
41+
// PublicKeyCallback:
42+
// PublicKeyHandler (our code):
43+
// reset(ctx.Permissions) and set ctx.Permissions.giteaKeyID = keyID
44+
// pubKey.Verify
45+
// return ctx.Permissions // only reaches here, the pub key is really authenticated
46+
// set conn.Permissions from serverAuthenticate
47+
// sessionHandler(conn)
48+
//
49+
// Then sessionHandler should only use the "verified keyID" from the original ssh conn, but not the ctx one.
50+
// Otherwise, if a user provides 2 keys A (a correct one) and B (public key matches but no private key),
51+
// then only A succeeds to authenticate, sessionHandler will see B's keyID
52+
53+
const giteaPermissionExtensionKeyID = "gitea-perm-ext-key-id"
3954

4055
func getExitStatusFromError(err error) int {
4156
if err == nil {
@@ -61,8 +76,32 @@ func getExitStatusFromError(err error) int {
6176
return waitStatus.ExitStatus()
6277
}
6378

79+
// sessionPartial is the private struct from "gliderlabs/ssh/session.go"
80+
// We need to read the original "conn" field from "ssh.Session interface" which contains the "*session pointer"
81+
// https://github.com/gliderlabs/ssh/blob/d137aad99cd6f2d9495bfd98c755bec4e5dffb8c/session.go#L109-L113
82+
// If upstream fixes the problem and/or changes the struct, we need to follow.
83+
// If the struct mismatches, the builtin ssh server will fail during integration tests.
84+
type sessionPartial struct {
85+
sync.Mutex
86+
gossh.Channel
87+
conn *gossh.ServerConn
88+
}
89+
90+
func ptr[T any](intf any) *T {
91+
// https://pkg.go.dev/unsafe#Pointer
92+
// (1) Conversion of a *T1 to Pointer to *T2.
93+
// Provided that T2 is no larger than T1 and that the two share an equivalent memory layout,
94+
// this conversion allows reinterpreting data of one type as data of another type.
95+
v := reflect.ValueOf(intf)
96+
p := v.UnsafePointer()
97+
return (*T)(p)
98+
}
99+
64100
func sessionHandler(session ssh.Session) {
65-
keyID := fmt.Sprintf("%d", session.Context().Value(giteaKeyID).(int64))
101+
// here can't use session.Permissions() because it only uses the value from ctx, which might not be the authenticated one.
102+
// so we must use the original ssh conn, which always contains the correct (verified) keyID.
103+
sshConn := ptr[sessionPartial](session)
104+
keyID := sshConn.conn.Permissions.Extensions[giteaPermissionExtensionKeyID]
66105

67106
command := session.RawCommand()
68107

@@ -164,6 +203,23 @@ func sessionHandler(session ssh.Session) {
164203
}
165204

166205
func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
206+
// The publicKeyHandler (PublicKeyCallback) only helps to provide the candidate keys to authenticate,
207+
// It does NOT really verify here, so we could only record the related information here.
208+
// After authentication (Verify), the "Permissions" will be assigned to the ssh conn,
209+
// then we can use it in the "session handler"
210+
211+
// first, reset the ctx permissions (just like https://github.com/gliderlabs/ssh/pull/243 does)
212+
// it shouldn't be reused across different ssh conn (sessions), each pub key should have its own "Permissions"
213+
oldCtxPerm := ctx.Permissions().Permissions
214+
ctx.Permissions().Permissions = &gossh.Permissions{}
215+
ctx.Permissions().Permissions.CriticalOptions = maps.Clone(oldCtxPerm.CriticalOptions)
216+
217+
setPermExt := func(keyID int64) {
218+
ctx.Permissions().Permissions.Extensions = map[string]string{
219+
giteaPermissionExtensionKeyID: fmt.Sprint(keyID),
220+
}
221+
}
222+
167223
if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
168224
log.Debug("Handle Public Key: Fingerprint: %s from %s", gossh.FingerprintSHA256(key), ctx.RemoteAddr())
169225
}
@@ -238,8 +294,7 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
238294
if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
239295
log.Debug("Successfully authenticated: %s Certificate Fingerprint: %s Principal: %s", ctx.RemoteAddr(), gossh.FingerprintSHA256(key), principal)
240296
}
241-
ctx.SetValue(giteaKeyID, pkey.ID)
242-
297+
setPermExt(pkey.ID)
243298
return true
244299
}
245300

@@ -266,8 +321,7 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
266321
if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
267322
log.Debug("Successfully authenticated: %s Public Key Fingerprint: %s", ctx.RemoteAddr(), gossh.FingerprintSHA256(key))
268323
}
269-
ctx.SetValue(giteaKeyID, pkey.ID)
270-
324+
setPermExt(pkey.ID)
271325
return true
272326
}
273327

services/feed/notifier.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,12 @@ func (a *actionNotifier) SyncPushCommits(ctx context.Context, pusher *user_model
417417
}
418418

419419
func (a *actionNotifier) SyncCreateRef(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
420+
// ignore pull sync message for pull requests refs
421+
// TODO: it's better to have a UI to let users chose
422+
if refFullName.IsPull() {
423+
return
424+
}
425+
420426
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
421427
ActUserID: repo.OwnerID,
422428
ActUser: repo.MustOwner(ctx),
@@ -431,6 +437,12 @@ func (a *actionNotifier) SyncCreateRef(ctx context.Context, doer *user_model.Use
431437
}
432438

433439
func (a *actionNotifier) SyncDeleteRef(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
440+
// ignore pull sync message for pull requests refs
441+
// TODO: it's better to have a UI to let users chose
442+
if refFullName.IsPull() {
443+
return
444+
}
445+
434446
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
435447
ActUserID: repo.OwnerID,
436448
ActUser: repo.MustOwner(ctx),

templates/repo/view_list.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{/* use grid layout, still use the old ID because there are many other CSS styles depending on this ID */}}
22
<div id="repo-files-table" {{if .HasFilesWithoutLatestCommit}}hx-indicator="#repo-files-table .repo-file-cell.message" hx-trigger="load" hx-swap="morph" hx-post="{{.LastCommitLoaderURL}}"{{end}}>
3-
<div class="repo-file-line">
3+
<div class="repo-file-line repo-file-last-commit">
44
<div class="latest-commit">{{template "repo/latest_commit" .}}</div>
55
<div>{{if and .LatestCommit .LatestCommit.Committer}}{{DateUtils.TimeSince .LatestCommit.Committer.When}}{{end}}</div>
66
</div>

web_src/css/repo/clone.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/* only used by "repo/empty.tmpl" */
22
.clone-buttons-combo {
3+
display: flex;
4+
align-items: center;
35
flex: 1;
46
}
57

68
.clone-buttons-combo input {
79
border-left: none !important;
810
border-radius: 0 !important;
11+
height: 30px;
912
}
1013

1114
/* used by the clone-panel popup */

web_src/css/repo/home-file-list.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
padding: 6px 10px;
4545
}
4646

47+
#repo-files-table .repo-file-last-commit {
48+
background: var(--color-box-header);
49+
}
50+
4751
#repo-files-table .repo-file-cell.name {
4852
max-width: 300px;
4953
white-space: nowrap;
@@ -59,6 +63,7 @@
5963
}
6064

6165
#repo-files-table .repo-file-cell.age {
66+
text-align: right;
6267
white-space: nowrap;
6368
color: var(--color-text-light-1);
6469
}

0 commit comments

Comments
 (0)