4 ms·
Look, I am gonna be blunt. A great man once said: "Those who sacrifice essential Security to purchase a little temporary Convenience deserve neither" :) I hav
by EGreg 9y ago
Look, I am gonna be blunt.
A great man once said: "Those who sacrifice essential Security to purchase a little temporary Convenience deserve neither" :)
I have seen Javascript frameworks and fads come and go. I remember 10 years ago "progressive enhancement" was the greatest thing, and onclick inside your HTML was considered hideous. Then came Angular with its ng-click and suddenly placing behavior stuff inside declarative HTML was cool again. Then came React and placing HTML -- er, excuse me, JSX inside your Javascript files was ... shocking but then cool. Then came Vue...
Look, it isn't that difficult:
HTML is for structure
CSS is for presentation
JS is for dynamic behavior
Want animations? Use classes and CSS.
Want complex behavior? Use classes, find your elements with getElementsByClassName and do your thing.
Want extra info? Stuff it into a JSON-encoded data-{something} attribute.
There is no reason to let your declarative HTML contain code-like things. Have it contain markers using HTML-compatible things like CSS classes and data- attributes (we used to call this "microformats").
Then on the server...
When you render your HTML tags you escape your attributes and text content.
When you output Javascript ... you DON'T. Instead you json_encode stuff that a .js file should consume. Oh, and use SRIs when loading the js from CDNs.
Before you execute SQL, you escape the values.
======================
In short - you escape for the medium you are interacting with.
======================
Seriously, it's straightforward and you can't mess it up... unless you start using "syntax sugar" that makes you insert things into a language that it wasn't designed for, after reading a framework touting "LOOK THE GREATEST SHINY NEW NONSTANDARD TECHNIQUE".
It's really not adding all that much. I remember when two-way binding was hot and touted by Angular 1. It's not anymore. I remember when CoffeeScript was the next greatest thing. It's not so much anymore.
Meanwhile, if you stick to the ES5/6 standards and let each language of the Web do what it's supposed to, you have a great future ahead of you. And much less injections to worry about.
Update - to the downvoters: go ahead, type words! I am interested to have an actual discussion after over 15 years of seeing SO MANY SECURITY HOLES from this one thing: frameworks interpolating stuff into a language that was designed for something else.
- jaequery 9y agoI used to think that way too. But Vue is really great for pages that have forms. It literally makes your life 10x easier.
- EGreg 9y agoAs I said, "sacrificing essential Security to purchase a little Convenience." And I bet you it's not 10x easier at all! Not even 2x. Write a small piece of code illustrating this convenience, and I will show you a similar piece of code that is just as easy to write/maintain and retains the proper separation of concerns. Seriously, I'm happy to be proven wrong on the 10x easier front.
- pitaj 9y agoYes but can that code be portable as a compliment you can embed multiple times in a web page? Separation of concerns is not the same as separation of technologies. Putting JS, CSS, and HTML into separate files isn't necessarily separation of concerns, you're just separating tightly coupled code in different languages into multiple files.
- EGreg 9y agoYes of course it can be portable!
- sametmax 9y agoYou mean your undocumented, untested custom way of doing the most basic things ? A table you can reorder ? Separating data from presentation ? Sharing widgets ? Applying style conditionally ? Then combine all that ? Of course you can do it easily. In a team of 1.
- spion 9y agoYou're getting downvoted because you're equating Angular 1, React and Vue. React has one huge difference: its "template language" is (almost) never compiled at run time. Its not HTML at all. This makes it difficult to coerce it to interpret any random string as an expression. That annoying "build step" that everyone hates, that is the convenience for which people are sacrificing security. When you have no build step, your template compiler must accept expression strings at run-time, and it will inevitably process user input at some point unless you're very careful all the time. "Escaping" things is a stop-gap that doesn't work. You could just use fully static HTML/JS endpoints (react based) combined with fully JSON based RPC endpoints and it will be safe. Then you can combine that into server-side rendering, and it will stay safe (unless renderToString has a problem). No escaping required if you are not sending code strings to a compiler at runtime.
- EGreg 9y agoI get your point about precompilers. LESS, SASS, JSX and so on. But that's only one thing, my main point was "don't put executable code into your HTML". I would say, btw, that a Template Engine is what you need, such as Handlebars. They compile stuff too. What is wrong with escaping? That is what you do when you dynamically interpolate stuff. Of course interpolating stuff where it doesn't belong can lead to this: https://medium.com/dailyjs/exploiting-script-injection-flaws-in-reactjs-883fb1fe36c1 https://medium.com/dailyjs/exploiting-script-injection-flaws...
- spion 9y agoYou need escaping only when you're sending strings to a compiler (which is what you do when you do server-side rendering), and its a never ending source of bugs. You basically need to re-implement the entire logic of a full blown parser for the target language to guarantee successful escaping. E.g. see `mysql_real_for_real_this_time_escape_string` versus just supporting parameterized queries. Parameters don't need to be escaped; they will never be interpreted as SQL code. This list is also fun: https://gist.github.com/JohannesHoppe/5612274 https://gist.github.com/JohannesHoppe/5612274 - will your framework escape implementation cover all of them? Are you sure? If you serve a static html (no templates) + css + JS (pre-compiled JSX templates) client with all dynamic content being served as e.g. JSON, you're getting rid of a large number of attack vectors from the start. You are down to a handful of attributes, of which only dangerouslySetInnerHTML is a React-originating attack vector because similarly to all other template languages it sends a string to a compiler. It should probably be a symbol, too, especially now that we have spread syntax for props. https://github.com/facebook/react/issues/10506 https://github.com/facebook/react/issues/10506 - until then, you can simply forbid spread syntax for props and/or dangerouslySetInnerHTML. Any other HTML attributes that have a string-to-compiler behaviour remain a problem, regardless of whether you are using jquery or react or whatever. (e.g. href). Its sad but for some of them the only recourse we have is escaping. Still, thats a huge improvement over escaping everything.
- matthewmacleod 9y agoI appreciate your view here, and to some extent you have valid points – but you're also way off the mark in several ways that don't really do anybody any favours. Ultimately I think that precision is really required here. I'll use the React example, because I'm currently elbow-deep in it. JSX is explicitly not 'HTML in JS' or 'JS in HTML' or anything else like that. It doesn't (or at least, shouldn't) run in the browser. It's only a sugary syntax for making it easier to write JS code that outputs HTML, and it exists because writing imperative code that outputs deeply nested trees is kind of annoying to write. Even better, everything is properly sanitised at the correct level; there's essentially no way to use unescaped data without explicitly trying to do so. That's a pretty great state of affairs. I'm not going to be a cheerleader for all this stuff, because you're right that the correct tool should be used for the correct job. But frankly if you don't see the benefits of stability, modularity, and even security of using something like React versus patching together your own framework using `getElementsByClassName` then I think you're missing something.