elbywan/yett

Unblocking does not work if the passed value is in a variable

b33rd33r opened this issue · 6 comments

This is something that I came across in a more complex environment and almost got me thinking that I must be crazy so I tried to reproduce this issue with almost no libraries at all.

As you can see in your demo site, when someone uses window.yett.unblock like this:

window.yett.unblock('/mypattern/');

everything works without an issue.

Same goes if you enter an array of previously blacklisted items:

window.yett.unblock(['/mypattern1/', '/mypattern2/');

However, when I try to pass the arguments through a variable, it does not work.

I have setup a very basic html page that demonstrates what I'm trying to say. Please check it here:

http://yett.mashup.gr/

Note that inputArr and anotherArr have the exact same initial assignment.

Also, if you execute the unblock function directly from Chrome's console like so: unblockScript([ /testme\.js/ ]); it works without issues. The problem is when the input to the unblock function does not come as a literal but as a variable.

Hi @b33rd33r,

Also, if you execute the unblock function directly from Chrome's console like so: unblockScript([ /testme\.js/ ]); it works without issues. The problem is when the input to the unblock function does not come as a literal but as a variable.

Actually when I try that on the Chrome console it does not work:

capture d ecran 2019-02-18 a 10 30 17

I think your issues comes from the fact that you are trying to pass an array of Regexp to the unblock function. But the unblock function expects an array of strings.

capture d ecran 2019-02-18 a 10 30 28

I have updated the page to use the exact same template with your demo site and the array is now declared as an array of strings, not regexes. Still not working.

Edit: I have tried pretty much every combination of strings, regexes and arrays in the more complex development environment where the problem first appeared. I understand how this may sound crazy and I really hope that I'm doing something very wrong and there is a simple solution that I'm missing but I've spent close to 2 hours banging my head against the wall for this one.

@b33rd33r Can you try replacing:

var unblockArray = [ '/inline\.js$/' ];

with:

var unblockArray = [ 'inline.js' ];

That works, thanks a lot!

Question: since I'm using a variable that contains an array of regexes that comes from the backend to block the scripts and I want to selectively enable them based on the user's choice, is there a way to use the same regexes that I'm using for blacklisting in order to do the unblocking?

If the regex that comes from the backend is simple enough, I can maybe create a "somewhat equivalent" text (e.g. in inline\.js I can maybe keep the inline part) but if we're talking about a regex like: (?<=[^<]+?)<[^>]+? it can't be done. What would maybe help if using the regexes is not possible, to have like a method that I could call on window.yett to get a list of the scripts that were blocked so that I can match the regex against them and figure out which one is which.

That works, thanks a lot!

Glad to hear that! 😉

Question: since I'm using a variable that contains an array of regexes that comes from the backend to block the scripts and I want to selectively enable them based on the user's choice, is there a way to use the same regexes that I'm using for blacklisting in order to do the unblocking?

Not right now, but I just added a commit that enables passing regexes to the unblock function. It should help with your use case.

With the change, if the .toString() representation of both RegExp is equal then it will be removed from the blacklist (or for people using the whitelist - it will be added to the list).

I'm currently waiting for the tests to pass, then I'll release a new version (if everything is fine in a few minutes).

@b33rd33r Just published 0.1.8, that should solve the issue.

Feel free to reopen if you have any trouble with the feature!