8000 [pull] main from miniflux:main by pull[bot] · Pull Request #316 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
5 changes: 4 additions & 1 deletion internal/http/response/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package response // import "miniflux.app/v2/internal/http/response"
import (
"compress/flate"
"compress/gzip"
"fmt"
"io"
"log/slog"
"maps"
Expand Down Expand Up @@ -86,7 +87,9 @@ func (b *Builder) WithoutCompression() *Builder {
func (b *Builder) WithCaching(etag string, duration time.Duration, callback func(*Builder)) {
etag = normalizeETag(etag)
b.headers.Set("ETag", etag)
b.headers.Set("Cache-Control", "public, immutable")
// max-age is required for the "immutable" directive to take effect: without
// it, browsers still revalidate content-hashed assets on every reload.
b.headers.Set("Cache-Control", fmt.Sprintf("public, max-age=%d, immutable", int64(duration.Seconds())))
b.headers.Set("Expires", time.Now().Add(duration).UTC().Format(http.TimeFormat))

if ifNoneMatch(b.r.Header.Get("If-None-Match"), etag) {
Expand Down
4 changes: 2 additions & 2 deletions internal/http/response/builder_test.go
10BC8
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func TestBuildResponseWithCachingEnabled(t *testing.T) {
t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
}

expectedHeader := "public, immutable"
expectedHeader := "public, max-age=60, immutable"
actualHeader := resp.Header.Get("Cache-Control")
if actualHeader != expectedHeader {
t.Fatalf(`Unexpected cache control header, got %q instead of %q`, actualHeader, expectedHeader)
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestBuildResponseWithCachingAndIfNoneMatch(t *testing.T) {
t.Fatalf(`Unexpected body, got %q instead of %q`, actual, tt.expectedBody)
}

if resp.Header.Get("Cache-Control") != "public, immutable" {
if resp.Header.Get("Cache-Control") != "public, max-age=60, immutable" {
t.Fatalf(`Unexpected Cache-Control header: %q`, resp.Header.Get("Cache-Control"))
}

Expand Down
4 changes: 4 additions & 0 deletions internal/storage/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"golang.org/x/crypto/bcrypt"
)

var dummyBcryptHash = []byte("$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy")

// CountUsers returns the total number of users.
func (s *Storage) CountUsers() (int, error) {
var result int
Expand Down Expand Up @@ -673,6 +675,8 @@ func (s *Storage) CheckPassword(username, password string) error {

err := s.db.QueryRow("SELECT password FROM users WHERE username=$1", username).Scan(&hash)
if errors.Is(err, sql.ErrNoRows) {
// Perform a dummy bcrypt comparison against the hashed `password` string to avoid leaking whether the user exists via response timing.
_ = bcrypt.CompareHashAndPassword(dummyBcryptHash, []byte(password))
return fmt.Errorf(`store: unable to find this user: %s`, username)
} else if err != nil {
return fmt.Errorf(`store: unable to fetch user: %v`, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/template/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Engine struct {
func NewEngine(basePath string) *Engine {
return &Engine{
templates: make(map[string]*template.Template),
funcMap: &funcMap{basePath},
funcMap: &funcMap{basePath: basePath},
}
}

Expand Down
13 changes: 8 additions & 5 deletions internal/template/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ import (
)

type funcMap struct {
basePath string
basePath string
iconPaths map[string]string
}

// Map returns a map of template functions that are compiled during template parsing.
func (f *funcMap) Map() template.FuncMap {
// Pre-compute every icon URL once, as iconPath is called a lot during pages rendering.
f.iconPaths = make(map[string]string, len(static.BinaryBundles))
for filename, bundle := range static.BinaryBundles {
f.iconPaths[filename] = f.basePath + "/icon/" + bundle.Checksum + "/" + filename
}
return template.FuncMap{
"contains": strings.Contains,
"csp": csp,
Expand Down Expand Up @@ -155,10 +161,7 @@ func (f *funcMap) Map() template.FuncMap {
}

func (f *funcMap) iconPath(filename string) string {
if bundle, ok := static.BinaryBundles[filename]; ok {
return fmt.Sprintf("%s/icon/%s/%s", f.basePath, bundle.Checksum, filename)
}
return fmt.Sprintf("%s/icon/_/%s", f.basePath, filename)
return f.iconPaths[filename]
}

func (f *funcMap) iconFunc() func(string) template.HTML {
Expand Down
Loading
0