Browse Source

feat(sanitizer): add validation for empty width and height attributes in img tags

Frédéric Guillot 10 months ago
parent
commit
cecc18420d

+ 3 - 0
internal/reader/sanitizer/sanitizer.go

@@ -541,6 +541,9 @@ func isValidDataAttribute(value string) bool {
 }
 
 func isPositiveInteger(value string) bool {
+	if value == "" {
+		return false
+	}
 	if number, err := strconv.Atoi(value); err == nil {
 		return number > 0
 	}

+ 10 - 0
internal/reader/sanitizer/sanitizer_test.go

@@ -99,6 +99,16 @@ func TestImgWithIncorrectWidthAndHeightAttribute(t *testing.T) {
 	}
 }
 
+func TestImgWithEmptywidthAndHeightAttribute(t *testing.T) {
+	input := `<img src="https://example.org/image.png" width="" height="">`
+	expected := `<img src="https://example.org/image.png" loading="lazy">`
+	output := SanitizeHTMLWithDefaultOptions("http://example.org/", input)
+
+	if output != expected {
+		t.Errorf(`Wrong output: %s`, output)
+	}
+}
+
 func TestImgWithTextDataURL(t *testing.T) {
 	input := `<img src="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" alt="Example">`
 	expected := ``