I don’t use the plugin mentioned in this post anymore.
On this blog, I use the Crayon syntax highlighter for WordPress to render all the code snippets, since this is a programming blog after all. Crayon is one of the more popular highlighting plugins, as clearly demonstrated by the sad condition of its support forum. It comes with a bunch of color schemes including Ethan Schoonover’s extremely popular “solarized” color scheme and a replica of what Github uses (although the plugin’s version contains a tad bit more purple). A light pastel blue would have fit with this blog’s color scheme quite nicely, but definitely not purple. So, I dove deeper.
Crayon stores its highlighting themes as plain CSS in WordPress’s
upload directory. That’s the same place that photos and other media go for your posts. I can think of a couple good reasons why this decision makes more sense than loading the CSS directly from the plugin’s folder:
- WordPress has (by necessity) file permissions to write to the uploads directory, which means that if the plugin ever needs to customize themes, it can do that internally.
- There are hooks for CDN’s and such that mirror WordPress’s upload directory, but may not do the same for plugins and other things.
- On network sites, customized color schemes can be site-specific although the plugin is installed and activated network-wide.
However, there’s also one really bad effect (that likely applies only to me) of this, which I spent a good deal of the afternoon debugging. I run nginx on the backend, and its configuration divides WordPress into two regions: one contains
wp-admin, and the other, everything else. This way, I can restrict WordPress cookies to only the former, which is run over SSL using a self-signed certificate, and avoid having the issue where Google indexes everything twice. (Visitors shouldn’t be reading blogs over
It would be nice if everything could be so cleanly divided this way, but
wp-admin also uses file uploads like image thumbnails, which is why there are all the mixed-content warnings you may have seen if you’ve ever administered anything through a web panel over
https. Browsers complain when you try to load pages with mixed content, but they will let you do so anyway. Things are different, however, when the request is made after the page has loaded, through AJAX.
There’s an inherent security problem with letting a page script load whatever resources it wants to from anywhere on the Internet. This issue is addressed by the same-origin policy framework, wherein the server with the resource (as opposed to the one containing the web page) gets to decide if access is allowed.
HTTP/1.1 200 OK Access-Control-Allow-Origin: http://example.com
The header takes either a list of origins, null, or the wildcard *. It’s ultimately up to the browser to implement this security header, but most modern ones do.
Now, here’s the twist: Crayon uses the built-in WordPress function,
wp_upload_dir(), to find and load custom themes. If your server is set up like mine, this will return a
http scheme address, whereas a
https address is required. Without a check to see if the page is loaded through SSL, the plain-text AJAX call will fail because browsers treat the SSL and the plain-text version of a site as different origins.
There are a couple ways to go about this. You can:
- Disable SSL temporarily while you’re editing the custom color themes (probably the easiest).
- Instruct the browser to ignore same-origin policy.
- Patch the plugin code to use
I ended up going with the third, after unsuccessfully attempting to send a
access-control-allow-origin header with every response on the server-side. This was a rather frustrating issue to resolve, but it was interesting to see the kinds of problems that arise in exchange for progress in security.