mirror of
https://github.com/miniflux/v2.git
synced 2025-07-22 17:18:37 +00:00
Allow width and height attributes for img tags
This commit is contained in:
parent
9c6ea92122
commit
d85908e3de
2 changed files with 33 additions and 2 deletions
|
@ -113,6 +113,10 @@ func sanitizeAttributes(baseURL, tagName string, attributes []html.Attribute) ([
|
||||||
value = sanitizeSrcsetAttr(baseURL, value)
|
value = sanitizeSrcsetAttr(baseURL, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tagName == "img" && (attribute.Key == "width" || attribute.Key == "height") && !isPositiveInteger(value) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if isExternalResourceAttribute(attribute.Key) {
|
if isExternalResourceAttribute(attribute.Key) {
|
||||||
if tagName == "iframe" {
|
if tagName == "iframe" {
|
||||||
if isValidIframeSource(baseURL, attribute.Val) {
|
if isValidIframeSource(baseURL, attribute.Val) {
|
||||||
|
@ -350,7 +354,7 @@ func isValidIframeSource(baseURL, src string) bool {
|
||||||
|
|
||||||
func getTagAllowList() map[string][]string {
|
func getTagAllowList() map[string][]string {
|
||||||
whitelist := make(map[string][]string)
|
whitelist := make(map[string][]string)
|
||||||
whitelist["img"] = []string{"alt", "title", "src", "srcset", "sizes"}
|
whitelist["img"] = []string{"alt", "title", "src", "srcset", "sizes", "width", "height"}
|
||||||
whitelist["picture"] = []string{}
|
whitelist["picture"] = []string{}
|
||||||
whitelist["audio"] = []string{"src"}
|
whitelist["audio"] = []string{"src"}
|
||||||
whitelist["video"] = []string{"poster", "height", "width", "src"}
|
whitelist["video"] = []string{"poster", "height", "width", "src"}
|
||||||
|
@ -511,3 +515,10 @@ func isValidDataAttribute(value string) bool {
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isPositiveInteger(value string) bool {
|
||||||
|
if number, err := strconv.Atoi(value); err == nil {
|
||||||
|
return number > 0
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
@ -15,6 +15,26 @@ func TestValidInput(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestImgWithWidthAndHeightAttribute(t *testing.T) {
|
||||||
|
input := `<img src="https://example.org/image.png" width="10" height="20">`
|
||||||
|
expected := `<img src="https://example.org/image.png" width="10" height="20" loading="lazy">`
|
||||||
|
output := Sanitize("http://example.org/", input)
|
||||||
|
|
||||||
|
if output != expected {
|
||||||
|
t.Errorf(`Wrong output: %s`, output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestImgWithIncorrectWidthAndHeightAttribute(t *testing.T) {
|
||||||
|
input := `<img src="https://example.org/image.png" width="10px" height="20px">`
|
||||||
|
expected := `<img src="https://example.org/image.png" loading="lazy">`
|
||||||
|
output := Sanitize("http://example.org/", input)
|
||||||
|
|
||||||
|
if output != expected {
|
||||||
|
t.Errorf(`Wrong output: %s`, output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestImgWithTextDataURL(t *testing.T) {
|
func TestImgWithTextDataURL(t *testing.T) {
|
||||||
input := `<img src="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" alt="Example">`
|
input := `<img src="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" alt="Example">`
|
||||||
expected := ``
|
expected := ``
|
||||||
|
@ -57,7 +77,7 @@ func TestSourceWithSrcsetAndMedia(t *testing.T) {
|
||||||
|
|
||||||
func TestMediumImgWithSrcset(t *testing.T) {
|
func TestMediumImgWithSrcset(t *testing.T) {
|
||||||
input := `<img alt="Image for post" class="t u v ef aj" src="https://miro.medium.com/max/5460/1*aJ9JibWDqO81qMfNtqgqrw.jpeg" srcset="https://miro.medium.com/max/552/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 276w, https://miro.medium.com/max/1000/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 500w" sizes="500px" width="2730" height="3407">`
|
input := `<img alt="Image for post" class="t u v ef aj" src="https://miro.medium.com/max/5460/1*aJ9JibWDqO81qMfNtqgqrw.jpeg" srcset="https://miro.medium.com/max/552/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 276w, https://miro.medium.com/max/1000/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 500w" sizes="500px" width="2730" height="3407">`
|
||||||
expected := `<img alt="Image for post" src="https://miro.medium.com/max/5460/1*aJ9JibWDqO81qMfNtqgqrw.jpeg" srcset="https://miro.medium.com/max/552/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 276w, https://miro.medium.com/max/1000/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 500w" sizes="500px" loading="lazy">`
|
expected := `<img alt="Image for post" src="https://miro.medium.com/max/5460/1*aJ9JibWDqO81qMfNtqgqrw.jpeg" srcset="https://miro.medium.com/max/552/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 276w, https://miro.medium.com/max/1000/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 500w" sizes="500px" width="2730" height="3407" loading="lazy">`
|
||||||
output := Sanitize("http://example.org/", input)
|
output := Sanitize("http://example.org/", input)
|
||||||
|
|
||||||
if output != expected {
|
if output != expected {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue