duandou8120 2019-05-22 17:00
浏览 335

MariaDB全文搜索在BOOLEAN模式中允许哪些字符?

I want to pass user input through PHP to an SQL query on a MariaDB 10.1.37 InnoDB table to do a search. I want to protect against building invalid SQL queries. Currently I am removing the known operator characters like this:

preg_replace('/[\+\-\<\>\(\)\~\*\"]/', ' ', $user_input)

Then I append a * character to each word. So if the user input is foo bar the query will be:

SELECT columnname, MATCH(columnname) AGAINST('foo* bar*' IN BOOLEAN MODE) AS score
FROM tablename;

Unfortunately there are other characters that a user can input that will break the query. For example, if the user inputs % the query will be AGAINST('%*' IN BOOLEAN MODE) which gives the error: syntax error, unexpected $end, expecting FTS_TERM or FTS_NUMB or '*' .

I could try to filter out characters that cause problems (a blacklist) or I could try to whitelist characters that I know are ok.

I can't find anything in the documentation to define what characters are allowed or disallowed.

Is there a known set of characters that is either allowed or disallowed?

  • 写回答

1条回答 默认 最新

  • douba1214 2019-05-27 18:57
    关注

    Take the opposite approach? Decide what characters to allow.

    preg_replace("/[^-'a-zA-Z0-9]/", ' ', $user_input)
    

    Keep:

    • Dash for hyphenated words
    • Apostrophe for contractions

    Remove:

    • Plus and asterisk -- because of FT syntax
    • Slash and double-quote -- PHP syntax

    Then use double-quotes, not apostrophe, in

    AGAINST("foo* bar*" IN BOOLEAN MODE) 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 vscode问题请教
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM