-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a fallback for font rendering when not allowed to use eval
#6141
Conversation
Thanks for working on the issue. Could you address jshint issues and squash commits? Make sure you are friendly to the https://github.com/mozilla/pdf.js/wiki/Style-Guide. Few points:
|
@yurydelendik Thanks for the kind words! Its my pleasure to work on this 😁 I cleaned up much of the excess array creation, ensured I passed the linter, and applied the style guide rules where I saw I violated them. I'd love another review on this when you get a chance 👀 I'm unsure how to test this feature. I'm not the most familiar with this code base and I'm having trouble finding any tests around this. I also am not sure if there is a nice way to force the After this is resolved, I'm happy to squash my commits to prepare this for merging ✨ |
@skalnik It is easier for us to review your code if you squash the commits into one commit. Could you please do that? Thanks! |
6681cea
to
2f7dd63
Compare
@timvandermeij D'oh, I always just look at the PR diff on GitHub, so I didn't even think about that. I've gone ahead and squashed the commits 👍 |
} | ||
/* jshint -W054 */ | ||
this.compiledGlyphs[character] = new Function('c', 'size', js); | ||
} catch(e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use lazy initialized property (e.g. isEvalSupported
) at FontLoader that will use shadow() where you will testnew Function
capabilities and branch based on that, e.g. var result; try { new Function(''); result = true; } catch (e) { result = false }; return shadow(...
.
See example at https://github.com/skalnik/pdf.js/blob/fix-font-csp-issues/src/display/font_loader.js#L54
Reword commit comment "[PATCH] Add fallback for font loading when eval disabled |
If you introduce FontLoader.isEvalSupported property, you will be able to fake different compile methods by returning true or false. The internal font rendering is working when PDFJS.disableFontFace is set to |
Could you add isEvalSupported to the FontLoader?
And use it to branch the logic in the |
Ah, after looking at your example, I think I understand what you were saying now. Sorry, a lot of these JS patterns are new to me |
There are some lint errors. Tip: you can catch lint errors locally by running |
e596313
to
3442ecd
Compare
Could you optimize your code, by using objects instead of array & slice? Your current method involves a array copy operation at every glyph creation call. Here are some suggestions for optimization: Use an object, not an arrayI have created some alternatives and a benchmark, my proposed method ("usingObject") is 2-4x faster than your method (Safari 8. Chrome 43, Firefox 40,
For a benchmark of the alternatives, see http://jsfiddle.net/5co62ohk/1/. My top suggestion is 3-4x faster than my proposed alternative in Chrome 43, Safari 8 and IE 11 and 1.3-1.5x faster in Firefox 40. Special-case parameter-less argumentsThe generic for loop in
|
11aed83
to
8f044d6
Compare
I've implemented these changes 😁 Oh wait! I lied, I didn't do the second one. Doing that now. |
ac13648
to
bf4544f
Compare
@Rob--W Any more thoughts on this? |
@skalnik Two small notes, and then it looks fine to me. |
bf4544f
to
ae25b9c
Compare
All fixed up 🔧 |
this.compiledGlyphs[character] = new Function('c', 'size', js); | ||
var cmds = objs.get(this.loadedName + '_path_' + character); | ||
var js = ''; | ||
var current, i, len, j, alen; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
j
and alen
don't seem to be used, so remove them.
And could you move var js = '';
inside if (FontLoader.isEvalSupported) {
?
@skalnik I found another two minor issues. I guess that that's really it. |
81c395d
to
88ba476
Compare
Patched that up as well! |
In terms of code, LGTM. I'll defer the merge to @yurydelendik. |
if (PDFJS.isEvalSupported) { | ||
try { | ||
/* jshint evil: true */ | ||
new Function('console.log("test");'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make function empty.
Looks good with comments above addressed. |
88ba476
to
04cbddc
Compare
Updated! |
The bots time out when processing
|
04cbddc
to
8daceca
Compare
for (i = 0, len = cmds.length; i < len; i++) { | ||
current = cmds[i]; | ||
|
||
if(current.args !== undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: please add one space between if
and (
.
In some cases, such as in use with a CSP header, constructing a function with a string of javascript is not allowed. However, compiling the various commands that need to be done on the canvas element is faster than interpreting them. This patch changes the font renderer to instead emit commands that are compiled by the font loader. If, during compilation, we receive an EvalError, we instead interpret them.
8daceca
to
341c5e9
Compare
@timvandermeij That should be resolved. @Snuffleupagus tiny nits fixed up |
/botio-linux preview |
From: Bot.io (Linux)ReceivedCommand cmd_preview from @timvandermeij received. Current queue size: 0 Live output at: http://107.21.233.14:8877/a50cdef429e0d69/output.txt |
From: Bot.io (Linux)SuccessFull output at http://107.21.233.14:8877/a50cdef429e0d69/output.txt Total script time: 0.64 mins Published |
/botio test |
From: Bot.io (Linux)ReceivedCommand cmd_test from @timvandermeij received. Current queue size: 0 Live output at: http://107.21.233.14:8877/f72d3db853ecabb/output.txt |
From: Bot.io (Windows)ReceivedCommand cmd_test from @timvandermeij received. Current queue size: 0 Live output at: http://107.22.172.223:8877/539e862e2ff05cb/output.txt |
From: Bot.io (Windows)SuccessFull output at http://107.22.172.223:8877/539e862e2ff05cb/output.txt Total script time: 18.47 mins
|
From: Bot.io (Linux)SuccessFull output at http://107.21.233.14:8877/f72d3db853ecabb/output.txt Total script time: 19.02 mins
|
This patch looks really good and much cleaner than before to me. The tests also pass now. @yurydelendik would like to test this once more and after that this should be good to merge. |
@yurydelendik any updates here since you wanted to test one more time? |
Looks good. Thank you for the patch. |
Provide a fallback for font rendering when not allowed to use `eval`
This provides a fallback for font rendering if PDF.js is used in a place where it is not allowed to use
eval
, such as an environment with a CSP header withunsafe-eval
.It does so by changing the code emitted from the font_renderer to be more generic commands, and then when they're run in the font_loader, they're compiled into JS and used to construct a function like before. However if we catch an
EvalError
, we instead useFunction.prototype.apply
to run the commands.Fixes #4660