- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Your Comment
Admin
Sounds like they made the button (and probably other UI elements) re-usable with a templating system (PHP). The only real WTF is the escaping, and that's explainable by the mere existence of junior programmers. It's a super quick fix... Just escape it properly.
Admin
Any server-side generated JavaScript is pure evil.
Never do that, guys. Never. The only acceptable thing is setting a variable equal to a serialized JSON, to be used by the actual client-side JS view logic. The rest is a huge NOPE.
Got there recently when I had to deal with a server-side generated JS that used to build an entire menu, used to look and feel the same in all the subdomains. It injected not only logic, but also HTML and CSS. It was horrible. Even more so because it gave jQuery for granted, while the new application is based on Angular and jQuery is nowhere to be seen...
Admin
Until you've seen copious amounts of client side javascript code generating more client side javascript code you've not truly seen hell.
scr += "function assignScripts(){\n;"; scr += "var jqscr = document.getElementById("jqueryScript");\njqscr.setAttribute("src","/sysicons/jquery-current.js");\n"; //scr += "alert('load ' + jqscr.getAttribute('src'));\n"; scr += "var sgscr = document.getElementById("sysscript");sgscr.setAttribute("src",MYAWG + "/display/app/sys-remit/js");\n"; scr += "}\n";
Admin
Pretty sure Konstantinos would like to escape this one.
Admin
(unregistered)
Admin
this reminded me of an old book called "hard drive": a programmer who worked on a voice recognition program slipped in several hidden operations that would generate a VIRUS!
Admin
What happens when one of those characters is already escaped?
Admin
In a string variable?
Well, the escape character would be escaped, that's all.
Admin
Could've been solved with a trivially simple
Boom. Valid Javascript data value, no "hope I got this right" manual escaping.
Admin
Why are there unicorns on the site?
Admin
Am I reading that right, that JavaScript sends arbitrary php code to be executed server side? That doesn't seem like a massive gaping security hole, at all!
Admin
And then they have to laboriously recapture the escaped characters.
Admin
Someone needed to read the PHP manual more carefully. It's easy to do all the string escaping in one call to str_replace(). Nesting like that is required in SQL.
Admin
When the only tool you have is a melon baller...
Admin
It's the other way round. The file is being created on the server using PHP. So they use PHP to create JavaScript. Not that big of a security hole. Just ugly.
Admin
And that is why json_encode() exists.
Admin
Rather the opposite. The results of the server-side code are injected into the client-side javascript. This happens on the server before the page is sent to the client.
Admin
I worked on a code base like this. It was the worst. Wordpress plugins getting called by javascript, building scripts with php templates that set up globals for other scripts, ajax into php files, ajax into other scripts. It was a nightmare
Admin
If this only were generated JavaScript it would be hardly more than Purgatory.
But the article's headline is misleading - this is generated PHP. Client-side generated PHP.
Ok, I suppose this PHP generating JavaScript was in turn generated by some PHP script.
(Which might have been created by some VBA script, who knows ...)
Admin
No, it's "classic" generated JS by a PHP script. The PHP part isn't generated. Double generated code is the mental child of an arch-demon. This isn't so bad...
Admin
No, it's an optical illusion. The PHP is processed on the server, creating strings that are interpolated into the page. It happens that the interpolation occurs within a script, but that's a red herring.
This isn't specific to PHP. The same effect can occur with ASP, JSP, VTL--any server-processed language that lets you insert strings into HTML pages.
Admin
Even outside of PHP, most server-side templates allow server-side injection into any part of the page, including javascript. See Django (in fact, in order to make your URLs portable you need to invoke {% url reverse_url_name %}, so you could have both or $.get("{% url some_view %}") even.