By matt v. on
I'm working on a module that allows users to enter search "commands" and be redirected to search results on another site (a la yubnub.org).
Everything was working great until I started running check_url on the destination urls, which encodes all the ampersands (causing arguments in the URL to get garbled).
Here's the relevant code (or here's the full function, in case it might help):
// inject arguments
$url = str_replace('%s', $arguments, $url);
if (!ereg(":", $url)) {
drupal_goto(check_plain($url));
}
else {
// check_url is breaking some commands, since ampersands get encoded.
drupal_set_header("Location: ". check_url($url));
}
Any recommendations on how to safely work around that? In addition, I'd welcome any security (or general) feedback on the module.
Comments
Read the docs
The check_url function performs the following action:
Prepare a URL for use in an HTML attribute. Strips harmful protocols.
The first part is causing you problems as you don't wish to display the URL, but use it as a header location. What you want is most of http://api.drupal.org/api/function/filter_xss_bad_protocol without calling check_plain() at the end. Unfortunately, it doesn't look like that function has been decomposed enough for you to call the portion you need. I'd recommend re-implementing it for your own use without calling check_plain. This will keep your URL's browser friendly.
I got it working again...
Thanks for you help. I got it working again by using explode to break the URL apart, check the individual pieces, then used implode to put it back together. It may not be particularly efficient, but it seems to be working well, so far: