Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
miniflux
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
miniflux
Commits
a155ab6d
Commit
a155ab6d
authored
5 years ago
by
Jebbs
Committed by
Frédéric Guillot
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Filter valid XML characters for UTF-8 XML documents before decoding
This change should reduce "illegal character code" XML errors.
parent
a4ebb33c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
reader/xml/decoder.go
+38
-1
38 additions, 1 deletion
reader/xml/decoder.go
reader/xml/decoder_test.go
+58
-3
58 additions, 3 deletions
reader/xml/decoder_test.go
with
96 additions
and
4 deletions
reader/xml/decoder.go
+
38
−
1
View file @
a155ab6d
...
...
@@ -10,13 +10,25 @@ import (
"fmt"
"io"
"io/ioutil"
"strings"
"miniflux.app/reader/encoding"
)
// NewDecoder returns a XML decoder that filters illegal characters.
func
NewDecoder
(
data
io
.
Reader
)
*
xml
.
Decoder
{
decoder
:=
xml
.
NewDecoder
(
data
)
var
decoder
*
xml
.
Decoder
buffer
,
_
:=
ioutil
.
ReadAll
(
data
)
enc
:=
procInst
(
"encoding"
,
string
(
buffer
))
if
enc
!=
""
&&
enc
!=
"utf-8"
&&
enc
!=
"UTF-8"
&&
!
strings
.
EqualFold
(
enc
,
"utf-8"
)
{
// filter invalid chars later within decoder.CharsetReader
decoder
=
xml
.
NewDecoder
(
bytes
.
NewReader
(
buffer
))
}
else
{
// filter invalid chars now, since decoder.CharsetReader not called for utf-8 content
filteredBytes
:=
bytes
.
Map
(
filterValidXMLChar
,
buffer
)
decoder
=
xml
.
NewDecoder
(
bytes
.
NewReader
(
filteredBytes
))
}
decoder
.
Entity
=
xml
.
HTMLEntity
decoder
.
Strict
=
false
decoder
.
CharsetReader
=
func
(
charset
string
,
input
io
.
Reader
)
(
io
.
Reader
,
error
)
{
...
...
@@ -48,3 +60,28 @@ func filterValidXMLChar(r rune) rune {
}
return
-
1
}
// This function is copied from encoding/xml package,
// procInst parses the `param="..."` or `param='...'`
// value out of the provided string, returning "" if not found.
func
procInst
(
param
,
s
string
)
string
{
// TODO: this parsing is somewhat lame and not exact.
// It works for all actual cases, though.
param
=
param
+
"="
idx
:=
strings
.
Index
(
s
,
param
)
if
idx
==
-
1
{
return
""
}
v
:=
s
[
idx
+
len
(
param
)
:
]
if
v
==
""
{
return
""
}
if
v
[
0
]
!=
'\'
'
&&
v
[
0
]
!=
'"'
{
return
""
}
idx
=
strings
.
IndexRune
(
v
[
1
:
],
rune
(
v
[
0
]))
if
idx
==
-
1
{
return
""
}
return
v
[
1
:
idx
+
1
]
}
This diff is collapsed.
Click to expand it.
reader/xml/decoder_test.go
+
58
−
3
View file @
a155ab6d
...
...
@@ -11,19 +11,74 @@ import (
"testing"
)
func
TestIllegalCharacters
(
t
*
testing
.
T
)
{
func
Test
UTF8With
IllegalCharacters
(
t
*
testing
.
T
)
{
type
myxml
struct
{
XMLName
xml
.
Name
`xml:"rss"`
Version
string
`xml:"version,attr"`
Title
string
`xml:"title"`
}
data
:=
fmt
.
Sprintf
(
`<?xml version="1.0" encoding="windows-1251"?><rss version="2.0"><title>%s</title></rss>`
,
"
\x10
"
)
expected
:=
"Title & 中文标题"
data
:=
fmt
.
Sprintf
(
`<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`
,
"
\x10
"
)
reader
:=
strings
.
NewReader
(
data
)
var
x
myxml
decoder
:=
NewDecoder
(
reader
)
err
:=
decoder
.
Decode
(
&
x
)
if
err
!=
nil
{
t
.
Error
(
err
)
return
}
if
x
.
Title
!=
expected
{
t
.
Errorf
(
"Incorrect entry title, expected: %s, got: %s"
,
expected
,
x
.
Title
)
}
}
func
TestWindows251WithIllegalCharacters
(
t
*
testing
.
T
)
{
type
myxml
struct
{
XMLName
xml
.
Name
`xml:"rss"`
Version
string
`xml:"version,attr"`
Title
string
`xml:"title"`
}
expected
:=
"Title & 中文标题"
data
:=
fmt
.
Sprintf
(
`<?xml version="1.0" encoding="windows-1251"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`
,
"
\x10
"
)
reader
:=
strings
.
NewReader
(
data
)
var
x
myxml
decoder
:=
NewDecoder
(
strings
.
NewReader
(
data
)
)
decoder
:=
NewDecoder
(
reader
)
err
:=
decoder
.
Decode
(
&
x
)
if
err
!=
nil
{
t
.
Error
(
err
)
return
}
if
x
.
Title
!=
expected
{
t
.
Errorf
(
"Incorrect entry title, expected: %s, got: %s"
,
expected
,
x
.
Title
)
}
}
func
TestIllegalEncodingField
(
t
*
testing
.
T
)
{
type
myxml
struct
{
XMLName
xml
.
Name
`xml:"rss"`
Version
string
`xml:"version,attr"`
Title
string
`xml:"title"`
}
expected
:=
"Title & 中文标题"
data
:=
fmt
.
Sprintf
(
`<?xml version="1.0" encoding="invalid"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`
,
"
\x10
"
)
reader
:=
strings
.
NewReader
(
data
)
var
x
myxml
decoder
:=
NewDecoder
(
reader
)
err
:=
decoder
.
Decode
(
&
x
)
if
err
!=
nil
{
t
.
Error
(
err
)
return
}
if
x
.
Title
!=
expected
{
t
.
Errorf
(
"Incorrect entry title, expected: %s, got: %s"
,
expected
,
x
.
Title
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment