Skip to content

Commit

Permalink
Merge pull request #621 from syjer/more-robust-data-uri-base64
Browse files Browse the repository at this point in the history
more robust data uri with base64 encoded data. Handle whitespaces.
  • Loading branch information
danfickle authored Dec 18, 2020
2 parents 7373154 + cef71c3 commit fcf4a1d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -136,7 +135,7 @@ public FSStream getUrl(String url) {
String data = url.substring(idxSeparator+1);
byte[] res;
if (url.indexOf("base64,") == idxSeparator - 6 /* 6 = "base64,".length */) {
res = Base64.getMimeDecoder().decode(data);
res = ImageUtil.fromBase64Encoded(data);
} else {
res = data.getBytes(StandardCharsets.UTF_8);
}
Expand Down Expand Up @@ -438,6 +437,10 @@ public static class DefaultUriResolver implements FSUriResolver {
public String resolveURI(String baseUri, String uri) {
if (uri == null || uri.isEmpty())
return null;

if (uri.startsWith("data:")) {
return uri; //bypass URI "formatting" check for data uri, as we may have whitespace in the base64 encoded data
}

try {
URI possiblyRelative = new URI(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.regex.Pattern;

/**
* Static utility methods for working with images. Meant to suggest "best practices" for the most straightforward
Expand Down Expand Up @@ -202,7 +203,13 @@ public static BufferedImage createTransparentImage(int width, int height) {
g2d.dispose();
return bi;
}


private static final Pattern WHITE_SPACE = Pattern.compile("\\s+");

public static byte[] fromBase64Encoded(String b64encoded) {
return Base64.getMimeDecoder().decode(WHITE_SPACE.matcher(b64encoded).replaceAll(""));
}

/**
* Get the binary content of an embedded base 64 image.
*
Expand All @@ -213,7 +220,7 @@ public static byte[] getEmbeddedBase64Image(String imageDataUri) {
int b64Index = imageDataUri.indexOf("base64,");
if (b64Index != -1) {
String b64encoded = imageDataUri.substring(b64Index + "base64,".length());
return Base64.getMimeDecoder().decode(b64encoded);
return fromBase64Encoded(b64encoded);
} else {
XRLog.log(Level.SEVERE, LogMessageId.LogMessageId0Param.LOAD_EMBEDDED_DATA_URI_MUST_BE_ENCODED_IN_BASE64);
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<html>
<head>
<style>
@page {margin:0;size: 70px 70px}
html, body,img {margin:0;padding:0;}
</style>
</head>

<body>
<img style="height:64px; width:64px;border:1px solid red;"
src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAACXBIWXMAAA7EAA
AOxAGVKw4bAAAE2klEQVRoge2Za0xbZRjH/x0wb
mMggzIQmFTAYYjD6WSr2eUD29R42eIy2eYlojHT
aEycuuiSRWN08QNZBJIZx2VGg1GJjhh1cZMi47K5sDBXx7g
OCi1QbqVSRunaxw/zAuSc95z3LeZI0t+n5rz/5/k///a05/Q9O
iLCYmaJ1gP4Sy CA1gQCaE0ggNYs+gDBIkWuUbScxFgvHFZ4PQrioBD
EGZCQieQc6DNE7JjouK /EDiuqDsBYgJV3IDoJQSEKeu8MRq5hqA29zbC3IzgUGZuxLh8h4c
JDz4G46DxLZXtouIuvajZeDzV/ Tcd3U0M5zUyJ9/kbngAddfTps+R2+
e/6b4xznxH5/OmkOoC9g8r2Lsz0/+D1UG0JVb1O9g66clqsh7oAE4N0fDe
NWcQ8FLj8A320jeo+FqtW9zNqKkbea7glZWG+dvPIfhBxaUi5W6xaRYDOeuiA
23LFDJTxXMdQm3C1UgDyoe4Ytr4hbAAA
432sVZsZU+MIWirWWylAYwWy8hAeLStoq8HJt1mXM+cQ
irYDQGO5tIAI9z+PlBzFWSVhBhjrRXcT1
j/D0lyqRss3aK/FQKtsE/LhyL
1orxUbkQ07gAVpudAxNSNdA
PDVq+g8Ky24ef6ER2PwKipfFJ
uSAfNeaMyC2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</html>
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,14 @@ public void testIssue615InfiniteLoopBreakWord() throws IOException {
assertTrue(vt.runTest("issue-615-infinite-loop-break-word"));
}

/**
* Test that we allow whitespaces inside the base64 encoded data uri.
*/
@Test
public void testMoreRobustDataUriBase64() throws IOException {
assertTrue(vt.runTest("more-robust-data-uri-base64"));
}

// TODO:
// + Elements that appear just on generated overflow pages.
// + content property (page counters, etc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Locale;
import java.util.logging.Level;

import com.openhtmltopdf.layout.SharedContext;
import com.openhtmltopdf.resource.ImageResource;
import com.openhtmltopdf.swing.NaiveUserAgent;
import com.openhtmltopdf.util.ImageUtil;
import com.openhtmltopdf.util.LogMessageId;
import com.openhtmltopdf.util.XRLog;

Expand Down Expand Up @@ -77,10 +75,7 @@ public ImageResource getImageResource(String uriStr) {

if (is != null) {
try {
URI uri = new URI(uriStr);
if (uri.getPath() != null
&& uri.getPath().toLowerCase(Locale.US)
.endsWith(".pdf")) {
if (uriStr.toLowerCase(Locale.US).endsWith(".pdf")) {
// TODO: Implement PDF AS IMAGE
// PdfReader reader = _outputDevice.getReader(uri);
// PDFAsImage image = new PDFAsImage(uri);
Expand Down

0 comments on commit fcf4a1d

Please sign in to comment.