From 5a07fd8932b9c8573d3ef56cb84fc0a6479f6372 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Thu, 26 May 2022 05:44:04 +0200 Subject: [PATCH] Add new rewrite rule to decode base64 content --- reader/rewrite/rewrite_functions.go | 30 +++++++++++++++++++++++++++++ reader/rewrite/rewriter.go | 6 ++++++ reader/rewrite/rewriter_test.go | 30 +++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/reader/rewrite/rewrite_functions.go b/reader/rewrite/rewrite_functions.go index b5b52409..20e2c5d9 100644 --- a/reader/rewrite/rewrite_functions.go +++ b/reader/rewrite/rewrite_functions.go @@ -5,6 +5,7 @@ package rewrite // import "miniflux.app/reader/rewrite" import ( + "encoding/base64" "fmt" "html" "net/url" @@ -288,3 +289,32 @@ func addCastopodEpisode(entryURL, entryContent string) string { return player + `
` + entryContent } + +func applyFuncOnTextContent(entryContent string, selector string, repl func(string) string) string { + var treatChildren func(i int, s *goquery.Selection) + treatChildren = func(i int, s *goquery.Selection) { + if s.Nodes[0].Type == 1 { + s.ReplaceWithHtml(repl(s.Nodes[0].Data)) + } else { + s.Contents().Each(treatChildren) + } + } + + doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent)) + if err != nil { + return entryContent + } + + doc.Find(selector).Each(treatChildren) + + output, _ := doc.Find("body").First().Html() + return output +} + +func decodeBase64Content(entryContent string) string { + if ret, err := base64.StdEncoding.DecodeString(strings.TrimSpace(entryContent)); err != nil { + return entryContent + } else { + return html.EscapeString(string(ret)) + } +} diff --git a/reader/rewrite/rewriter.go b/reader/rewrite/rewriter.go index feb0139e..b00ea1a1 100644 --- a/reader/rewrite/rewriter.go +++ b/reader/rewrite/rewriter.go @@ -102,6 +102,12 @@ func applyRule(entryURL, entryContent string, rule rule) string { } case "add_castopod_episode": entryContent = addCastopodEpisode(entryURL, entryContent) + case "base64_decode": + if len(rule.args) >= 1 { + entryContent = applyFuncOnTextContent(entryContent, rule.args[0], decodeBase64Content) + } else { + entryContent = applyFuncOnTextContent(entryContent, "body", decodeBase64Content) + } } return entryContent diff --git a/reader/rewrite/rewriter_test.go b/reader/rewrite/rewriter_test.go index f796da7d..fab7da26 100644 --- a/reader/rewrite/rewriter_test.go +++ b/reader/rewrite/rewriter_test.go @@ -295,3 +295,33 @@ func TestRewriteAddCastopodEpisode(t *testing.T) { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } + +func TestRewriteBase64Decode(t *testing.T) { + content := `VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=` + expected := `This is some base64 encoded content` + output := Rewriter("https://example.org/article", content, `base64_decode`) + + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} + +func TestRewriteBase64DecodeInHTML(t *testing.T) { + content := `
Lorem Ipsum not valid base64VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=
` + expected := `
Lorem Ipsum not valid base64This is some base64 encoded content
` + output := Rewriter("https://example.org/article", content, `base64_decode`) + + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} + +func TestRewriteBase64DecodeArgs(t *testing.T) { + content := `
Lorem IpsumVGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=
` + expected := `
Lorem IpsumThis is some base64 encoded content
` + output := Rewriter("https://example.org/article", content, `base64_decode(".base64")`) + + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +}