php - Parsing a Markdown style link safely -


I wrote a few code to match and parse the link of this style:

   

I have this code which is to remove the link text, Then the URL itself, then stick them in a HERF link. I am worried that maybe I am reminding someone in a way to inject the XSS, because I am going to a decent amount of characters. is it safe?

  $ pattern_square = '\ [(* *?) \]'; $ Pattern_round = "\ ((*. *?) \)"; $ Pattern = "/".$pattern_square.$pattern_round."/"; Preg_match ($ patterns, $ input, $ matches); $ Word = $ match [1]; $ Url = $ match [2]; $ Word = ereg_replace ("[^ -_ @ 0- 9a-ja-z.]", "", $ word); $ Url = ereg_replace ("[^ - A-Za-z0-9 + & amp; # /% | = ~ _ | |:]", "", $ Url); $ End = "& lt; a href = '$ url' & gt; $ word & lt; / a & gt;";  

It seems that the work is fine, and it does exclude some stupid URLs, including semicolon and backlass, but I care about those URLs is not.

If you already passed the input (which you is doing it, right?) Then this link is already impossible that XSS might contain any letters.

If you have not already passed the input through the htmlspecialchars , it does not matter what you filter after parsing the link, because you first Has been spoiled because someone can include arbitrary HTML or XSS outside links in any meanness.

This function will safely parse the markdown link in the text when applying htmlspecialchars :

  function doMarkdownLinks ($ s) {return Preg_replace_callback ('/ \ [(* *?)]] \ ((*. *?) \) /', Function ($ matches) {return '& lt; A href = "'. $ Matches [2].' '& Gt;' $. Matches [1] '& lt; / a & gt;';}, Htmlspecialchars ($ s));}  

If you need to do something more complex than that, So I advise you to use a current parser, because it is very easy to make mistakes with such a thing.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -