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

Skip to content
22 changes: 18 additions & 4 deletions internal/cli/ask_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,27 @@ func askCredentials() (string, string) {
fmt.Print("Enter Username: ")

reader := bufio.NewReader(os.Stdin)
username, _ := reader.ReadString('\n')
username, err := reader.ReadString('\n')
if err != nil {
printErrorAndExit(fmt.Errorf("unable to read username: %w", err))
}

fmt.Print("Enter Password: ")

state, _ := term.GetState(fd)
defer term.Restore(fd, state)
bytePassword, _ := term.ReadPassword(fd)
state, err := term.GetState(fd)
if err != nil {
printErrorAndExit(fmt.Errorf("unable to get terminal state: %w", err))
}
defer func() {
if restoreErr := term.Restore(fd, state); restoreErr != nil {
printErrorAndExit(fmt.Errorf("unable to restore terminal state: %w", restoreErr))
}
}()

bytePassword, err := term.ReadPassword(fd)
if err != nil {
printErrorAndExit(fmt.Errorf("unable to read password: %w", err))
}

fmt.Print("\n")
return strings.TrimSpace(username), strings.TrimSpace(string(bytePassword))
Expand Down
20 changes: 10 additions & 10 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ func Parse() {
return
}

if flagInfo {
info()
return
}

if flagVersion {
fmt.Println(version.Version)
return
}

if flagDebugMode {
config.Opts.SetLogLevel("debug")
}
Expand Down Expand Up @@ -148,16 +158,6 @@ func Parse() {
return
}

if flagInfo {
info()
return
}

if flagVersion {
fmt.Println(version.Version)
return
}

if config.Opts.IsDefaultDatabaseURL() {
slog.Info("The default value for DATABASE_URL is used")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/googlereader/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
mff "miniflux.app/v2/internal/reader/handler"
mfs "miniflux.app/v2/internal/reader/subscription"
"miniflux.app/v2/internal/storage"
"miniflux.app/v2/internal/urllib"
"miniflux.app/v2/internal/validator"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -405,7 +406,7 @@ func (h *handler) quickAddHandler(w http.ResponseWriter, r *http.Request) {
}

feedURL := r.Form.Get(paramQuickAdd)
if !validator.IsValidURL(feedURL) {
if !urllib.IsAbsoluteURL(feedURL) {
json.BadRequest(w, r, fmt.Errorf("googlereader: invalid URL: %s", feedURL))
return
}
Expand Down
3 changes: 3 additions & 0 deletions internal/locale/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (l *LocalizedErrorWrapper) Error() error {

func (l *LocalizedErrorWrapper) Translate(language string) string {
if l.translationKey == "" {
if l.originalErr == nil {
return ""
}
return l.originalErr.Error()
}
return NewPrinter(language).Printf(l.translationKey, l.translationArgs...)
Expand Down
10 changes: 10 additions & 0 deletions internal/locale/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ func TestLocalizedErrorWrapper_WithNilError(t *testing.T) {
}
}

func TestLocalizedErrorWrapper_TranslateWithEmptyKeyAndNilError(t *testing.T) {
wrapper := NewLocalizedErrorWrapper(nil, "")

result := wrapper.Translate("en_US")
expected := ""
if result != expected {
t.Errorf("Expected empty string for nil wrapped error, got %q", result)
}
}

func TestLocalizedError_EmptyKey(t *testing.T) {
localizedErr := NewLocalizedError("")

Expand Down
2 changes: 1 addition & 1 deletion internal/locale/plural.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package locale // import "miniflux.app/v2/internal/locale"
// And http://www.unicode.org/cldr/charts/29/supplemental/language_plural_rules.html
func getPluralForm(lang string, n int) int {
switch lang {
case "ar_AR":
case "ar_SA":
switch {
case n == 0:
return 0
Expand Down
4 changes: 2 additions & 2 deletions internal/locale/plural_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func TestPluralRules(t *testing.T) {
2: 1, // n > 1
5: 1, // n > 1
},
// Arabic (ar_AR) - 6 forms
"ar_AR": {
// Arabic (ar_SA) - 6 forms
"ar_SA": {
0: 0, // n == 0
1: 1, // n == 1
2: 2, // n == 2
Expand Down
12 changes: 6 additions & 6 deletions internal/locale/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func TestPluralWithIndexOutOfBounds(t *testing.T) {

func TestPluralWithVariousLanguageRules(t *testing.T) {
defaultCatalog = catalog{
"ar_AR": translationDict{
"ar_SA": translationDict{
plurals: map[string][]string{
"items": {"no items", "one item", "two items", "few items", "many items", "other items"},
},
Expand All @@ -329,11 +329,11 @@ func TestPluralWithVariousLanguageRules(t *testing.T) {
expected string
}{
// Arabic tests
{"ar_AR", "items", 0, "no items"},
{"ar_AR", "items", 1, "one item"},
{"ar_AR", "items", 2, "two items"},
{"ar_AR", "items", 5, "few items"}, // n%100 >= 3 && n%100 <= 10
{"ar_AR", "items", 15, "many items"}, // n%100 >= 11
{"ar_SA", "items", 0, "no items"},
{"ar_SA", "items", 1, "one item"},
{"ar_SA", "items", 2, "two items"},
{"ar_SA", "items", 5, "few items"}, // n%100 >= 3 && n%100 <= 10
{"ar_SA", "items", 15, "many items"}, // n%100 >= 11

// Polish tests
{"pl_PL", "files", 1, "one file"},
Expand Down
16 changes: 16 additions & 0 deletions internal/mediaproxy/media_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,14 @@ func TestShouldProxifyURLWithMimeType(t *testing.T) {
mediaProxyResourceTypes: []string{"image"},
expected: true,
},
{
name: "URL with uppercase MIME type should be proxified",
mediaURL: "http://example.com/image.jpg",
mediaMimeType: "Image/JPEG",
mediaProxyOption: "all",
mediaProxyResourceTypes: []string{"image"},
expected: true,
},
{
name: "URL with audio MIME type and audio resource type should be proxified",
mediaURL: "http://example.com/song.ogg",
Expand All @@ -662,6 +670,14 @@ func TestShouldProxifyURLWithMimeType(t *testing.T) {
mediaProxyResourceTypes: []string{"audio"},
expected: true,
},
{
name: "URL with mixed-case audio MIME type should be proxified",
mediaURL: "http://example.com/song.ogg",
mediaMimeType: "AuDiO/Ogg",
mediaProxyOption: "all",
mediaProxyResourceTypes: []string{"audio"},
expected: true,
},
{
name: "URL with video MIME type and video resource type should be proxified",
mediaURL: "http://example.com/movie.webm",
Expand Down
2 changes: 2 additions & 0 deletions internal/mediaproxy/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func ShouldProxifyURLWithMimeType(mediaURL, mediaMimeType, mediaProxyOption stri
return false
}

mediaMimeType = strings.ToLower(mediaMimeType)

for _, mediaType := range mediaProxyResourceTypes {
if strings.HasPrefix(mediaMimeType, mediaType+"/") {
return true
Expand Down
24 changes: 24 additions & 0 deletions internal/reader/icon/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ func (f *iconFinder) downloadIcon(iconURL string) (*model.Icon, error) {
}

func resizeIcon(icon *model.Icon) *model.Icon {
const (
maxFaviconDimension = 4096
maxFaviconPixels = 4096 * 4096
)

if icon.MimeType == "image/svg+xml" {
minifier := minify.New()
minifier.AddFunc("image/svg+xml", svg.Minify)
Expand All @@ -209,6 +214,25 @@ func resizeIcon(icon *model.Icon) *model.Icon {
slog.Warn("Unable to decode icon metadata", slog.Any("error", err))
return icon
}

if config.Width <= 0 || config.Height <= 0 {
slog.Warn("Icon resize skipped: invalid image dimensions",
slog.Int("width", config.Width),
slog.Int("height", config.Height),
)
return icon
}

pixelCount := int64(config.Width) * int64(config.Height)
if config.Width > maxFaviconDimension || config.Height > maxFaviconDimension || pixelCount > maxFaviconPixels {
slog.Warn("Icon rejected: image dimensions are too large",
slog.Int("width", config.Width),
slog.Int("height", config.Height),
slog.Int64("pixel_count", pixelCount),
)
return nil
}

if config.Height <= 32 && config.Width <= 32 {
slog.Debug("Icon doesn't need to be resized", slog.Int("height", config.Height), slog.Int("width", config.Width))
return icon
Expand Down
77 changes: 77 additions & 0 deletions internal/reader/icon/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package icon // import "miniflux.app/v2/internal/reader/icon"
import (
"bytes"
"encoding/base64"
"encoding/binary"
"hash/crc32"
"image"
"strings"
"testing"
Expand Down Expand Up @@ -408,6 +410,28 @@ func TestResizeInvalidImage(t *testing.T) {
}
}

func TestResizeIconTooLargeDimensions(t *testing.T) {
icon := model.Icon{
Content: mustMinimalPNG(t, 4097, 7),
MimeType: "image/png",
}

if resizeIcon(&icon) != nil {
t.Fatalf("Should reject images with too large dimensions")
}
}

func TestResizeIconTooLargePixelCount(t *testing.T) {
icon := model.Icon{
Content: mustMinimalPNG(t, 4096, 4097),
MimeType: "image/png",
}

if resizeIcon(&icon) != nil {
t.Fatalf("Should reject images with too many pixels")
}
}

func TestMinifySvg(t *testing.T) {
data := []byte(`<svg path d=" M1 4h-.001 V1h2v.001 M1 2.6 h1v.001"/></svg>`)
want := []byte(`<svg path="" d="M1 4H.999V1h2v.001M1 2.6h1v.001"/></svg>`)
Expand Down Expand Up @@ -441,3 +465,56 @@ func TestMinifySvgWithError(t *testing.T) {
t.Fatalf("Expected MimeType to remain image/svg+xml, got %s", result.MimeType)
}
}

func mustMinimalPNG(t *testing.T, width, height uint32) []byte {
t.Helper()

var b bytes.Buffer
b.Write([]byte{137, 80, 78, 71, 13, 10, 26, 10})
writePNGChunk(t, &b, "IHDR", func(data []byte) {
binary.BigEndian.PutUint32(data[0:4], width)
binary.BigEndian.PutUint32(data[4:8], height)
data[8] = 8
data[9] = 2
})
writePNGChunk(t, &b, "IEND", nil)

return b.Bytes()
}

func writePNGChunk(t *testing.T, b *bytes.Buffer, chunkType string, fill func([]byte)) {
t.Helper()

dataLen := 0
if chunkType == "IHDR" {
dataLen = 13
}

if err := binary.Write(b, binary.BigEndian, uint32(dataLen)); err != nil {
t.Fatal(err)
}

if _, err := b.WriteString(chunkType); err != nil {
t.Fatal(err)
}

data := make([]byte, dataLen)
if fill != nil {
fill(data)
}
if _, err := b.Write(data); err != nil {
t.Fatal(err)
}

crc := crc32.NewIEEE()
if _, err := crc.Write([]byte(chunkType)); err != nil {
t.Fatal(err)
}
if _, err := crc.Write(data); err != nil {
t.Fatal(err)
}

if err := binary.Write(b, binary.BigEndian, crc.Sum32()); err != nil {
t.Fatal(err)
}
}
16 changes: 14 additions & 2 deletions internal/template/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,21 @@ func dict(values ...any) (map[string]any, error) {
}

func truncate(str string, max int) string {
if runes := []rune(str); len(runes) > max {
return string(runes[:max]) + "…"
if max <= 0 {
panic("truncate: max must be greater than zero")
}

// Template callers pass feed titles from remote content. Scanning and
// allocating the entire untrusted input just to truncate it could create a
// denial-of-service risk, so stop as soon as we reach the requested limit.
runeCount := 0
for i := range str {
if runeCount == max {
return str[:i] + "…"
}
runeCount++
}

return str
}

Expand Down
Loading
0