Skip to content

Commit

Permalink
In an EPUB, espace the path before using it as a uri.
Browse files Browse the repository at this point in the history
  • Loading branch information
llemeurfr committed Jan 1, 2024
1 parent a0d7c3b commit aa0cbcf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pack/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ func encryptEPUBResource(compressor *flate.Writer, compress bool, encrypter cryp
data.KeyInfo.RetrievalMethod.URI = "license.lcpl#/encryption/content_key"
data.KeyInfo.RetrievalMethod.Type = "http://readium.org/2014/01/lcp#EncryptedContentKey"

data.CipherData.CipherReference.URI = xmlenc.URI(file.Path)
// espace the path before using it as a uri
data.CipherData.CipherReference.URI = xmlenc.URI(xmlenc.ResourcePathEscape(file.Path))

// declare to the reading software that the content is compressed before encryption
method := NoCompression
Expand Down
18 changes: 17 additions & 1 deletion xmlenc/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package xmlenc
import (
"encoding/xml"
"io"
"net/url"
"strings"

"golang.org/x/net/html/charset"
)
Expand All @@ -17,9 +19,23 @@ type Manifest struct {
XMLName struct{} `xml:"urn:oasis:names:tc:opendocument:xmlns:container encryption"`
}

// ResourcePathEscape encodes a path for a URL but keep slashes unescaped
func ResourcePathEscape(path string) string {
segments := strings.Split(path, "/")

// Encode each segment individually
for i, segment := range segments {
segments[i] = url.PathEscape(segment)
}

// Join the encoded segments with slashes
return strings.Join(segments, "/")
}

// DataForFile returns the EncryptedData item corresponding to a given path
func (m Manifest) DataForFile(path string) (Data, bool) {
uri := URI(path)
uri := URI(ResourcePathEscape(path))

for _, datum := range m.Data {
if datum.CipherData.CipherReference.URI == uri {
return datum, true
Expand Down

0 comments on commit aa0cbcf

Please sign in to comment.