Jump to content

Ampersand (&) breaks search and causes error


Recommended Posts

Posted

Hi! I noticed a suspicious search string in my website's logs.

Someone searched for "&", which causes an error.

I noticed that the same string causes an error on the Skyscrapers demo site: http://processwire.com/skyscrapers/search/

Is this something to something to be worried about?

--

Error: Exception: Unknown Selector operator: '%=&' -- was your selector value properly escaped?

Posted

GET on a form should turn & into %26 and prevent this problem. What method are you using?

I'm using GET. Should be the same code as the skyscraper profile, I think.

head.inc:

<form id='search-form' action='<?php echo $config->urls->root?>search/' method='get'>
   <input type='text' name='q' id='search_query' placeholder="Search..." value='<?php echo htmlentities($input->whitelist('q'), ENT_QUOTES, 'UTF-8'); ?>' />
   <button type='submit' id='search_submit'>Search</button>
</form>

search.php:

if($q = $sanitizer->selectorValue($input->get->q)) {

  }
Posted

Hm, it's the responsibility of the browser to do this. What to you get on the url? is the & encoded or not? Can you also try in another browser?

Posted

True, it is being encoded. Must be a problem with the selectors. If you do it while logged i you will get a useful message that should confirm this.

To solve this you can pass the query to $sanitizer->selectorValue($value)  and make the search only if it returns a value:

// (not tested)

$query = $sanitizer->selectorValue($query);

if (!$query) // not a valid search
 
Posted

Thanks! But isn't that what I'm already doing in search.php?

if($q = $sanitizer->selectorValue($input->get->q)) {
 // do search
} else {
 // don't search
}
Posted

Sorry, I'm not looking at the code, and didn't noticed that you posted it above. And yes, it's the same thing... and it's strange because that should prevent any error.

Anyway, and answering your question, I don't think it's something you should worry about.

Posted

Ok thanks! The same problem appears in Ryan's Skyscraper template - so maybe it's a core issue, then? I'll leave it for now :)

Posted

That's a strange one. But There's anyway a couple strange things when using PW search with selectors sometimes.

On my new blog http://soma.urlich.ch this doesn't happen and I have the same code. When I search for & it get's transformed to "&" and it finds results that contain no visible "&".

On all other PW sites there's an error.

On processwire.com if you enter "test&" you get some results and the first search entry "

is strange as it is a page that isn't viewable!

Anyway, searching with like "some & string" isn't a problem, and maybe just a min char count test on the search form could easily prevent error for things like "&".

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...