Eval is evil?
-
Hi,
I developed a plugin. One of its options is to let the user insert a JS script via textarea in the plugin. I push the script to PHP array, than use
wp_localize_script
to turn into a JS Object. The script runs before Contact Form 7 email message is sent:function wpcf7_redirect_mailsent_handler() { document.addEventListener( 'wpcf7mailsent', function( event ) { form = wpcf7_redirect_forms [ event.detail.contactFormId ]; // Script to run after sent. if ( form.after_sent_script ) { eval( form.after_sent_script ); } } }
Now, I have two questions
- Is this the best method? Is this safe? eval() has a bad reputation. If not,
I’ll be happy to hear your suggestions. - I sanitize the textarea of the script with
sanitize_textarea_field
to keep the spaces\rows in the textarea, but I also see it in the JS object as\r\n
.
So for example, I can run a script like this:eval("alert('hello');\r\nalert('world')");
. It runs OK for now, but is there a better way?
Thank you,
Yuval. - Is this the best method? Is this safe? eval() has a bad reputation. If not,
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Eval is evil?’ is closed to new replies.