Skip to content
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

Avoid using function constructor for alt format handling #12

Closed
wants to merge 1 commit into from

Conversation

tschaub
Copy link
Contributor

@tschaub tschaub commented Nov 19, 2013

This allows rbush to be used where the Content Security Policy (or other) disallows the use of eval and related functions (e.g. browser extensions). Previously, the format supplied could contain function calls (e.g. ['.getMaxX()', ...]). With this change, only property names are accepted - though they can be dot prefixed or bracket wrapped (e.g. '.maxX' or '[0]').

This allows rbush to be used where the Content Security Policy (or other) disallows the use of eval and related functions (e.g. browser extensions).  Previously, the format supplied could contain function calls (e.g. `['.getMaxX()', ...]`).  With this change, only property names are accepted - though they can be dot prefixed or bracket wrapped (e.g. '.maxX' or '[0]').
@tschaub
Copy link
Contributor Author

tschaub commented Nov 19, 2013

The tests and docs didn't show use of .getMaxX() style accessors. Not sure if you need to support those. If not, it would be nice to be able to avoid the Function constructor.

@mourner
Copy link
Owner

mourner commented Nov 19, 2013

That's interesting, I've never heard about security policies blocking eval-like statements. Can you share more details on this? What was the original related issue that you encountered?

This could be a nice change, but doesn't support more complex data scenarios (e.g. ['.bbox[0]', '.bbox[1]', ...), and potentially introduces a performance hit due to closures inside extremely performance-sensitive methods (did you compare benchmark results?).

@mourner
Copy link
Owner

mourner commented Nov 20, 2013

The security stuff was brought up in Leaflet too, so that's a valid point it seems. Leaflet/Leaflet#2209

An alternative solution (although requiring more verbose code) would be to make toBBox/compare methods public and don't run _initFormat if specified false for it explicitly, to allow simply defining all the methods manually. rbush.toBBox = function (...

@tschaub
Copy link
Contributor Author

tschaub commented Nov 20, 2013

Yes, the Content Security Policy (CSP) for browser apps/extensions is the common case where eval and friends are disallowed.

Allowing people to override the toBBox and compare* methods makes sense.

FWIW, I couldn't find any significant difference in the benchmarks with and without this change. But I also didn't have the patience to run them enough times to get much confidence in the results :).

@tschaub
Copy link
Contributor Author

tschaub commented Nov 20, 2013

@mourner here's an alternate implementation: https://github.com/tschaub/rbush/compare/less-evil-II

This is quite a bit more verbose.

If you do allow custom toBBox, it looks like the default implementation could avoid creating an extra array (reducing pressure on the garbage collector). I left in the extra array creation because that's what the current _toBBox does - but I didn't immediately see a place where you modify it.

@mourner
Copy link
Owner

mourner commented Nov 21, 2013

Nice, thanks! I meant that we could keep the current format handling (instead of removing it completely), but make it possible to bypass it in vulnerable cases such as developing an extension.

Yeah, no need to slice the bbox — it was done this way for simplicity in format-handling code (I found no significant performance difference).

@tschaub
Copy link
Contributor Author

tschaub commented Nov 21, 2013

I meant that we could keep the current format handling (instead of removing it completely)

Makes sense. I can update that branch.

@tschaub
Copy link
Contributor Author

tschaub commented Nov 21, 2013

Ok, closing in favor of #14. Let me know if you had something else in mind.

@tschaub tschaub closed this Nov 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants