Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:06:00+00:00 2026-06-15T01:06:00+00:00

I’m facing some difficulties when parsing bbcode safely, specifically [img] and [url]. Language is

  • 0

I’m facing some difficulties when parsing bbcode safely, specifically [img] and [url]. Language is less important, but this is regarding JavaScript.)

  1. URLs:
    Not long ago users were able to write [url=#” onclick=”alert(‘test’);”]Link[/url] on my site, and when others clicked the link an alert would appear. However, by replacing all double and single quotes with nothing, i.e. removing them, the alert hax did not work any further. My question here is if this is enough security for urls? Or are there any other scenarios I need to be aware of?

  2. Images:
    What security features do I need for the img bbcode? Is it enough to remove quotes and check if the end of the url ends with a known image file type, such as .png or .jpg? Or do I need to do more?

Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:06:01+00:00Added an answer on June 15, 2026 at 1:06 am

    With the caveats from my comment, I suggest you just whitelist characters for a URL: a-z, 0-9, &, ., /, ?, :, =, etc. Then replace the .*? by your allowed characters :

    /\[img\]([a-z0-9:&?=\/\.%]+?)\[\/img\]/ig
    /\[url\=([a-z0-9:&?=\/\.%]+?)\](.*?)\[\/url\]/ig
    

    This will cover most cases I think, except international URLs. Quotes are not allowed in this regexp, so no need to escape them. They meant to be expressed as %22. Also, this doesn’t validate URLs, but only protects from XSS I believe.

    Both [url] and [img] take a URL, so this part of the regexp is the same. And you shouldn’t check for .png or .jpeg because many images do not have a URL with an explicit extension.

    Then the url group in the regexp match will only need to be escaped for HTML.

    Full code:

    var imgRe = /\[img\]([a-z0-9:&?=\/\.%;]+?)\[\/img\]/ig;
    var linkRe = /\[url\=([a-z0-9:&?=\/\.%;]+?)\](.*?)\[\/url\]/ig
    
    $('#convert').click(function() {
        var output = $('#bbcode').val();
    
        // Escape HTML special characters
        // It's wrong to escape them before converting the bbcode into HTML
        // but I couldn't think of issues
        output = output.replace(/&/g, '&');
        output = output.replace(/</g, '&lt;');
        output = output.replace(/"/g, '&quot;');
    
        // Convert bbcode
        output = output.replace(imgRe, function(str, url) {
            return '<img src="' + url  + '"/>';
        });
    
        output = output.replace(linkRe, function(str, url, txt) {
            return '<a href="' + url  + '">' + txt + '</a>';
        });
    
        // print output
        $('#pre').html(output);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.