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
12 changes: 9 additions & 3 deletions internal/converter/epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import (
"crypto/md5"
"fmt"
"html"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -123,9 +124,7 @@

// Prepend source URL if provided
if options.SourceURL != "" {
sourceHeader := fmt.Sprintf(`<p style="font-size: 0.85em; color: #666; margin-bottom: 1.5em;">Source: <a href="%s">%s</a></p>`,
options.SourceURL, options.SourceURL)
processedHTML = sourceHeader + processedHTML
processedHTML = sourceHeaderHTML(options.SourceURL) + processedHTML
}

// Add the main content section
Expand All @@ -144,6 +143,13 @@
return nil
}

// sourceHeaderHTML renders the attribution line linking back to the source.
func sourceHeaderHTML(sourceURL string) string {
escaped := html.EscapeString(sourceURL)
return fmt.Sprintf(`<p style="font-size: 0.85em; color: #666; margin-bottom: 1.5em;">Source: <a href="%s">%s</a></p>`,
escaped, escaped)
Comment thread
rmitchellscott marked this conversation as resolved.
Dismissed
}

// ConvertHTMLFileToEPUB reads an HTML file and converts it to EPUB.
func ConvertHTMLFileToEPUB(htmlPath string, options EPUBOptions) (string, error) {
logging.Logf("[EPUB] ConvertHTMLFileToEPUB: processing %s", htmlPath)
Expand Down
51 changes: 51 additions & 0 deletions internal/converter/epub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package converter

import (
"strings"
"testing"
)

func TestSourceHeaderHTMLEscapesURL(t *testing.T) {
tests := []struct {
name string
sourceURL string
unwanted string
}{
{
name: "quote closing the href attribute",
sourceURL: `https://example.com/x"><script>alert(1)</script>`,
unwanted: "<script>",
},
{
name: "quote opening an attribute on the anchor",
sourceURL: `https://example.com/x" onmouseover="alert(1)`,
unwanted: `onmouseover="`,
},
{
name: "angle brackets in the link text",
sourceURL: `https://example.com/<img src=x onerror=alert(1)>`,
unwanted: "<img",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sourceHeaderHTML(tt.sourceURL)
if strings.Contains(got, tt.unwanted) {
t.Errorf("sourceHeaderHTML(%q) leaked %q:\n%s", tt.sourceURL, tt.unwanted, got)
}
if strings.Count(got, `<a href="`) != 1 {
t.Errorf("expected exactly one anchor, got:\n%s", got)
}
})
}
}

func TestSourceHeaderHTMLKeepsOrdinaryURL(t *testing.T) {
url := "https://example.com/article?id=1&page=2"
got := sourceHeaderHTML(url)

if !strings.Contains(got, "https://example.com/article?id=1&amp;page=2") {
t.Errorf("expected the URL preserved with escaping, got:\n%s", got)
}
}