3

Would encoding quotation marks and removing eventual javascript: prefixes be enough?

P.S. Safe enough to defeat XSS attacks that is.

7
  • Your question is way toooooo unspecific to make clear what the term "safe" in the title stands for. You might want to filter protocol URLs thought, but then you should ask for that. Context is okay, but keep your question to the part you want to learn about. Commented Sep 3, 2013 at 18:14
  • @hakre, safe enough to defeat XSS attacks that is. Commented Sep 3, 2013 at 18:19
  • well, then I'd like to make clear that removing the verbatim javascript: is not enough to prevent XSS in a href attribute. Instead I highly recommend you do some whitelisting here (that is far easier to implement and you don't need to be a super-pro to make this safe) Commented Sep 3, 2013 at 18:21
  • @hakre, would you mind elaborating on that in an answer? Commented Sep 3, 2013 at 18:24
  • If you understand a bit into which direction I pointed to, why not formulate the answer your own, then ping me and I take a look? Commented Sep 3, 2013 at 18:28

2 Answers 2

1

you can use the php function to validate urls

$url = "http://google.com";
if (filter_var($url, FILTER_VALIDATE_URL)) {
  echo "URL is valid";
}
else {
  echo "URL is invalid";
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not interested in validation. I'm rather interested in making a string safe (sanitization) regardless of weather it is a valid URL or not.
0

Encoding with htmlspecialchars() with the ENT_QUOTES flag will technically make the URL safe/sanitary for use from an HTML perspective, but it does not guarantee that it'll create a valid address.

$url = 'http://invalid"url';
$url = htmlspecialchars($url, ENT_QUOTES); // Yields "http://invalid"url"

1 Comment

Using htmlspecialchars() makes the URI safe for using it in an HTML attribute. It guarantees valid HTML, but it does not help anything in regards of security (=the OP wants to protect the users from being presented with malicious JavaScript links).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.