mirror of
https://github.com/go-gitea/gitea.git
synced 2026-12-04 15:54:57 +00:00
feat: record and expose SHA-256 for attachments (#39954)
Release assets and other attachments had no content digest, so tools that download release binaries could not verify them. This records one and shows it on the releases page in a GitHub-like asset list. - Record the SHA-256 of each attachment while it streams to storage, migrated release assets included - Return it as `digest: "sha256:<hex>"` in the attachment API, the field GitHub uses for release assets - Lay out release assets in columns for digest with copy button, download count, size and date, stacked under the name on narrow screens - Rename the "Downloads" section to "Assets" and show the asset count - Attachments uploaded before this have no digest, backfilling them is left for a follow-up | Before | After | | --- | --- | | <img width="600" alt="before-desktop" src="https://github.com/user-attachments/assets/d6f290b4-5fba-4ee2-86c0-82c0bd77f702" /> | <img width="600" alt="after-desktop" src="https://github.com/user-attachments/assets/11bafe88-331d-4e96-9455-87774db539d8" /> | | <img width="360" alt="before-mobile" src="https://github.com/user-attachments/assets/22a83b07-ca43-4cc6-8d2a-c1fc92a32dfe" /> | <img width="370" alt="after-mobile" src="https://github.com/user-attachments/assets/d013b7d1-0683-4983-9864-5dbc7844f27d" /> | --------- Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
1 parent
b2e54a7b18
commit
9ed44be714
15 files changed
+113
-65
No files matched your search
@@ -447,6 +447,7 @@ func prepareMigrationTasks() []*migration {
|
||||
newMigration(368, "Add unique index for team name", v29.AddUniqueTeamNameIndex),
|
||||
newMigration(369, "Add runner_name to action_task", v29.AddRunnerNameToActionTask),
|
||||
newMigration(370, "Change raw_concurrency of action_run and action_run_job to TEXT on MSSQL", v29.ChangeActionsRawConcurrencyToText),
|
||||
newMigration(371, "Add hash_sha256 column to attachment", v29.AddHashSHA256ToAttachment),
|
||||
}
|
||||
return preparedMigrations
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package v29
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.dev/modelmigration/base"
|
||||
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func AddHashSHA256ToAttachment(_ctx context.Context, x base.EngineMigration) error {
|
||||
type Attachment struct {
|
||||
HashSHA256 string `xorm:"hash_sha256 VARCHAR(64) NOT NULL DEFAULT ''"`
|
||||
}
|
||||
_, err := x.SyncWithOptions(xorm.SyncOptions{
|
||||
IgnoreConstrains: true,
|
||||
IgnoreDropIndices: true,
|
||||
}, new(Attachment))
|
||||
return err
|
||||
}
|
||||
@@ -34,6 +34,7 @@ type Attachment struct {
|
||||
Name string
|
||||
DownloadCount int64 `xorm:"DEFAULT 0"`
|
||||
Size int64 `xorm:"DEFAULT 0"`
|
||||
HashSHA256 string `xorm:"hash_sha256 VARCHAR(64) NOT NULL DEFAULT ''"` // empty for attachments uploaded before it was recorded
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
CustomDownloadURL string `xorm:"-"`
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ type Attachment struct {
|
||||
Name string `json:"name"`
|
||||
// Size is the file size in bytes
|
||||
Size int64 `json:"size"`
|
||||
// Digest is the SHA-256 of the content as "sha256:{hex}", omitted when not recorded at upload
|
||||
Digest string `json:"digest,omitempty"`
|
||||
// DownloadCount is the number of times the attachment has been downloaded
|
||||
DownloadCount int64 `json:"download_count"`
|
||||
// swagger:strfmt date-time
|
||||
|
||||
@@ -2727,7 +2727,7 @@
|
||||
"repo.release.tag_name_invalid": "The tag name is not valid.",
|
||||
"repo.release.tag_name_protected": "The tag name is protected.",
|
||||
"repo.release.tag_already_exist": "This tag name already exists.",
|
||||
"repo.release.downloads": "Downloads",
|
||||
"repo.release.assets": "Assets",
|
||||
"repo.release.download_count": "Downloads: %s",
|
||||
"repo.release.add_tag_msg": "Use the title and content of release as tag message.",
|
||||
"repo.release.add_tag": "Create Tag Only",
|
||||
|
||||
@@ -6,6 +6,8 @@ package attachment
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -28,11 +30,13 @@ func NewAttachment(ctx context.Context, attach *repo_model.Attachment, file io.R
|
||||
|
||||
err := db.WithTx(ctx, func(ctx context.Context) error {
|
||||
attach.UUID = uuid.New().String()
|
||||
size, err := storage.Attachments.Save(attach.RelativePath(), file, size)
|
||||
hasher := sha256.New()
|
||||
size, err := storage.Attachments.Save(attach.RelativePath(), io.TeeReader(file, hasher), size)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Attachments.Save: %w", err)
|
||||
}
|
||||
attach.Size = size
|
||||
attach.HashSHA256 = hex.EncodeToString(hasher.Sum(nil))
|
||||
return db.Insert(ctx, attach)
|
||||
})
|
||||
|
||||
|
||||
@@ -4,13 +4,13 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
repo_model "gitea.dev/models/repo"
|
||||
"gitea.dev/models/unittest"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/base"
|
||||
|
||||
_ "gitea.dev/models/actions"
|
||||
|
||||
@@ -26,20 +26,17 @@ func TestUploadAttachment(t *testing.T) {
|
||||
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||
|
||||
fPath := "./attachment_test.go"
|
||||
f, err := os.Open(fPath)
|
||||
assert.NoError(t, err)
|
||||
defer f.Close()
|
||||
|
||||
content := "attachment content"
|
||||
attach, err := NewAttachment(t.Context(), &repo_model.Attachment{
|
||||
RepoID: 1,
|
||||
UploaderID: user.ID,
|
||||
Name: filepath.Base(fPath),
|
||||
}, f, -1)
|
||||
Name: "attachment.txt",
|
||||
}, strings.NewReader(content), -1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
attachment, err := repo_model.GetAttachmentByUUID(t.Context(), attach.UUID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, user.ID, attachment.UploaderID)
|
||||
assert.Equal(t, int64(0), attachment.DownloadCount)
|
||||
assert.Equal(t, base.EncodeSha256(content), attachment.HashSHA256)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
repo_model "gitea.dev/models/repo"
|
||||
api "gitea.dev/modules/structs"
|
||||
"gitea.dev/modules/util"
|
||||
)
|
||||
|
||||
func WebAssetDownloadURL(ctx context.Context, repo *repo_model.Repository, attach *repo_model.Attachment) string {
|
||||
@@ -36,6 +37,7 @@ func toAttachment(ctx context.Context, repo *repo_model.Repository, a *repo_mode
|
||||
Created: a.CreatedUnix.AsTime(),
|
||||
DownloadCount: a.DownloadCount,
|
||||
Size: a.Size,
|
||||
Digest: util.Iif(a.HashSHA256 != "", "sha256:"+a.HashSHA256, ""),
|
||||
UUID: a.UUID,
|
||||
DownloadURL: getDownloadURL(ctx, repo, a), // for web/api requests, return different download URLs
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ package migrations
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -356,11 +358,13 @@ func (g *GiteaLocalUploader) CreateReleases(ctx context.Context, releases ...*ba
|
||||
continue
|
||||
}
|
||||
if rc != nil {
|
||||
attach.Size, err = storage.Attachments.Save(attach.RelativePath(), rc, util.Iif(*asset.Size > 0, int64(*asset.Size), -1)) // GitLab links and Forgejo external links report no size
|
||||
hasher := sha256.New()
|
||||
attach.Size, err = storage.Attachments.Save(attach.RelativePath(), io.TeeReader(rc, hasher), util.Iif(*asset.Size > 0, int64(*asset.Size), -1)) // GitLab links and Forgejo external links report no size
|
||||
rc.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("release %s asset %s: %w", release.TagName, asset.Name, err)
|
||||
}
|
||||
attach.HashSHA256 = hex.EncodeToString(hasher.Sum(nil))
|
||||
}
|
||||
|
||||
rel.Attachments = append(rel.Attachments, &attach)
|
||||
|
||||
@@ -73,39 +73,43 @@
|
||||
{{$release.RenderedNote}}
|
||||
</div>
|
||||
<div class="divider"></div>
|
||||
<details class="download" {{if eq $idx 0}}open{{end}}>
|
||||
{{$showArchives := and (not $.DisableDownloadSourceArchives) (not $release.IsDraft) ($.Permission.CanRead ctx.Consts.RepoUnitTypeCode)}}
|
||||
<details {{if eq $idx 0}}open{{end}}>
|
||||
<summary>
|
||||
{{ctx.Locale.Tr "repo.release.downloads"}}
|
||||
<span class="flex-text-inline">{{ctx.Locale.Tr "repo.release.assets"}}<span class="ui small label">{{if $showArchives}}{{Eval (len $release.Attachments) "+" 2}}{{else}}{{len $release.Attachments}}{{end}}</span></span>
|
||||
</summary>
|
||||
<ul class="ui divided list attachment-list">
|
||||
{{range $att := $release.Attachments}}
|
||||
<li class="item">
|
||||
<a target="_blank" class="tw-flex-1 gt-ellipsis" rel="nofollow" download href="{{$att.DownloadURL ctx}}">
|
||||
<strong class="flex-text-inline">{{svg "octicon-package" 16 "download-icon"}}<span class="gt-ellipsis">{{$att.Name}}</span></strong>
|
||||
</a>
|
||||
<div class="attachment-right-info flex-text-inline">
|
||||
<span class="tw-pl-5">{{$att.Size | FormatByteSize}}</span>
|
||||
<span class="flex-text-inline" data-tooltip-content="{{ctx.Locale.Tr "repo.release.download_count" (ctx.Locale.PrettyNumber $att.DownloadCount)}}">
|
||||
{{svg "octicon-info"}}
|
||||
<div class="ui fitted segment tw-mt-2.5">
|
||||
<div class="flex-divided-list items-px-default">
|
||||
{{range $att := $release.Attachments}}
|
||||
<div class="item release-asset">
|
||||
<a target="_blank" rel="nofollow" download href="{{$att.DownloadURL ctx}}">
|
||||
<strong class="flex-text-inline">{{svg "octicon-package" 16 "tw-text-text-light-1"}}<span class="gt-ellipsis">{{$att.Name}}</span></strong>
|
||||
</a>
|
||||
<span class="flex-text-inline release-asset-digest">
|
||||
{{if $att.HashSHA256}}
|
||||
<span class="gt-ellipsis">sha256:{{$att.HashSHA256}}</span>
|
||||
<button type="button" class="btn interact-fg" data-clipboard-text="sha256:{{$att.HashSHA256}}" data-tooltip-content="{{ctx.Locale.Tr "copy_hash"}}">{{svg "octicon-copy" 14}}</button>
|
||||
{{end}}
|
||||
</span>
|
||||
<div class="tw-flex-1"></div>
|
||||
{{DateUtils.TimeSince $att.CreatedUnix}}
|
||||
<span class="flex-text-inline" data-tooltip-content="{{ctx.Locale.Tr "repo.release.download_count" (ctx.Locale.PrettyNumber $att.DownloadCount)}}">{{svg "octicon-download"}}{{ctx.Locale.PrettyNumber $att.DownloadCount}}</span>
|
||||
<span>{{$att.Size | FormatByteSize}}</span>
|
||||
<span>{{DateUtils.TimeSince $att.CreatedUnix}}</span>
|
||||
</div>
|
||||
</li>
|
||||
{{end}}
|
||||
{{if and (not $.DisableDownloadSourceArchives) (not $release.IsDraft) ($.Permission.CanRead ctx.Consts.RepoUnitTypeCode)}}
|
||||
<li class="item">
|
||||
<a class="archive-link" download href="{{$.RepoLink}}/archive/{{$release.TagName | PathEscapeSegments}}.zip" rel="nofollow">
|
||||
<strong class="flex-text-inline">{{svg "octicon-file-zip" 16 "download-icon"}}{{ctx.Locale.Tr "repo.release.source_code"}} (ZIP)</strong>
|
||||
</a>
|
||||
</li>
|
||||
<li class="item">
|
||||
<a class="archive-link" download href="{{$.RepoLink}}/archive/{{$release.TagName | PathEscapeSegments}}.tar.gz" rel="nofollow">
|
||||
<strong class="flex-text-inline">{{svg "octicon-file-zip" 16 "download-icon"}}{{ctx.Locale.Tr "repo.release.source_code"}} (TAR.GZ)</strong>
|
||||
</a>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{end}}
|
||||
{{if $showArchives}}
|
||||
<div class="item">
|
||||
<a class="archive-link" download href="{{$.RepoLink}}/archive/{{$release.TagName | PathEscapeSegments}}.zip" rel="nofollow">
|
||||
<strong class="flex-text-inline">{{svg "octicon-file-zip" 16 "tw-text-text-light-1"}}{{ctx.Locale.Tr "repo.release.source_code"}} (ZIP)</strong>
|
||||
</a>
|
||||
</div>
|
||||
<div class="item">
|
||||
<a class="archive-link" download href="{{$.RepoLink}}/archive/{{$release.TagName | PathEscapeSegments}}.tar.gz" rel="nofollow">
|
||||
<strong class="flex-text-inline">{{svg "octicon-file-zip" 16 "tw-text-text-light-1"}}{{ctx.Locale.Tr "repo.release.source_code"}} (TAR.GZ)</strong>
|
||||
</a>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
{{if not (or $release.IsTag $release.IsDraft)}}
|
||||
{{template "shared/reactions" dict
|
||||
|
||||
+5
@@ -2676,6 +2676,11 @@
|
||||
"type": "string",
|
||||
"x-go-name": "Created"
|
||||
},
|
||||
"digest": {
|
||||
"description": "Digest is the SHA-256 of the content as \"sha256:{hex}\", omitted when not recorded at upload",
|
||||
"type": "string",
|
||||
"x-go-name": "Digest"
|
||||
},
|
||||
"download_count": {
|
||||
"description": "DownloadCount is the number of times the attachment has been downloaded",
|
||||
"format": "int64",
|
||||
|
||||
+5
@@ -25154,6 +25154,11 @@
|
||||
"format": "date-time",
|
||||
"x-go-name": "Created"
|
||||
},
|
||||
"digest": {
|
||||
"description": "Digest is the SHA-256 of the content as \"sha256:{hex}\", omitted when not recorded at upload",
|
||||
"type": "string",
|
||||
"x-go-name": "Digest"
|
||||
},
|
||||
"download_count": {
|
||||
"description": "DownloadCount is the number of times the attachment has been downloaded",
|
||||
"type": "integer",
|
||||
|
||||
@@ -48,6 +48,7 @@ func TestAPIIssueAttachment(t *testing.T) {
|
||||
apiAttachment := DecodeJSON(t, resp, &api.Attachment{})
|
||||
|
||||
unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID, IssueID: issue.ID})
|
||||
assert.Empty(t, apiAttachment.Digest)
|
||||
|
||||
commentAttachment := unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: 3, RepoID: repo.ID})
|
||||
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/assets/%d", repoOwner.Name, repo.Name, unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: commentAttachment.IssueID}).Index, commentAttachment.ID)).
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
unit_model "gitea.dev/models/unit"
|
||||
"gitea.dev/models/unittest"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/base"
|
||||
"gitea.dev/modules/git"
|
||||
"gitea.dev/modules/setting"
|
||||
api "gitea.dev/modules/structs"
|
||||
@@ -424,6 +425,7 @@ func TestAPIUploadAssetRelease(t *testing.T) {
|
||||
attachment := DecodeJSON(t, resp, &api.Attachment{})
|
||||
assert.Equal(t, filename, attachment.Name)
|
||||
assert.EqualValues(t, len(bufImageBytes), attachment.Size)
|
||||
assert.Equal(t, "sha256:"+base.EncodeSha256(string(bufImageBytes)), attachment.Digest)
|
||||
})
|
||||
t.Run("UploadWithName", func(t *testing.T) {
|
||||
resp := performUpload(t, assetURL+"?name=test-asset", bufImageBytes, http.StatusCreated)
|
||||
|
||||
@@ -59,36 +59,33 @@
|
||||
margin-bottom: 2px; /* the legacy trick to align the avatar vertically, no better solution at the moment */
|
||||
}
|
||||
|
||||
#release-list .release-entry .attachment-list {
|
||||
border: 1px solid var(--color-secondary);
|
||||
border-radius: var(--border-radius);
|
||||
#release-list .release-asset {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(40%, 1fr) minmax(0, 250px) minmax(60px, auto) minmax(75px, auto) minmax(100px, auto); /* fixed minimums keep columns aligned across rows */
|
||||
place-items: center end;
|
||||
gap: 16px;
|
||||
white-space: nowrap;
|
||||
color: var(--color-text-light-2);
|
||||
}
|
||||
|
||||
#release-list .release-entry .attachment-list > .item {
|
||||
display: flex;
|
||||
padding: 8px;
|
||||
flex-wrap: wrap;
|
||||
#release-list .release-asset > a,
|
||||
#release-list .release-asset-digest {
|
||||
justify-self: stretch;
|
||||
}
|
||||
|
||||
#release-list .release-entry .attachment-list > .item a {
|
||||
min-width: 300px;
|
||||
#release-list .release-asset-digest {
|
||||
font-family: var(--fonts-monospace);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
#release-list .release-entry .attachment-list .attachment-right-info {
|
||||
flex-shrink: 0;
|
||||
min-width: 300px;
|
||||
}
|
||||
|
||||
#release-list .release-entry .detail .download[open] summary {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
#release-list .download-icon {
|
||||
color: var(--color-text-light-1);
|
||||
}
|
||||
|
||||
#release-list .release-entry .detail .download .list li:last-child {
|
||||
border-bottom: none;
|
||||
@media (max-width: 1023.98px) {
|
||||
#release-list .release-asset {
|
||||
grid-template-columns: minmax(0, 1fr) repeat(3, auto);
|
||||
gap: 4px 12px;
|
||||
}
|
||||
#release-list .release-asset > a {
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
}
|
||||
|
||||
#tags-table .tag-list-row-title {
|
||||
|
||||
Reference in new issue
Block a user