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 8865011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:23:54+00:00 2026-06-14T16:23:54+00:00

I happen to read about XSS and how to avoid it. From what I

  • 0

I happen to read about XSS and how to avoid it. From what I have read, I came to know that we need input filtering, proper handling of application code and output encoding to make the web application somewhat XSS safe. After going through several articles, several doubts still persist.

  • When I tried jQuery.text(“untrusted_data”) or element.text=untrusted_data, the
    browser seems to be encoding the content perfectly, but I have read somewhere else that client-side encoding should not be trusted and you have to “always” encode at the server-side. Why is client-side encoding considered not safe ?

  • Whenever I tried to set value using jQuery.val(untrusted_data) or element.value=untrusted_data it seems to be safe enough. So is it really XSS safe or did I miss any case here ?

  • Also, I happen to read somewhere that jQuery.setAttribute(attrName,untrusted_data) and setAttribute(attrName,untrusted_data) are generally considered safe only if the Attribute Names doesn’t include URL Context based attributes(src,href etc) or Event Handlers(onClick,onMouseOver etc). If that is the case, how am I supposed to set an href attribute by using setAttribute(“href”,untrusted_data)? Is encodeForHtmlAttribute(untrusted_data) from server-side the right way of approach ?
  • How should I handle dynamic html creation. Consider the below example

<div id="divName1" onClick="copyData()">untrusted_data</div>
<div id="divName2"></div>

function copyData()
{
  var divValue = jQuery("#divName1").html();
  jQuery("#divName2").html(divValue);/XSS Here
}

What I want to achieve here is to get the text from divName1 and paste it to divName2’s content. The code I wrote above is XSS vulnerable. I can change this to

jQuery(“#divName2”).text(divValue)

So, this will ensure encoding, but as I understood, we say that client-side encoding is unsafe and only server-side encoding should be used. How should I write this to be XSS safe without using client-side encoding? I am a bit confused here :(.
Please help me out to clear these doubts.

  • 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-14T16:23:56+00:00Added an answer on June 14, 2026 at 4:23 pm

    This is a lot of questions at once.

    Why is client-side encoding considered not safe ?

    Client-side encoding is fine as long as it is done properly and consistently. Client-side encoding often has a lower bar since it does not need to worry about character-encoding level attacks like UTF-7 attacks.

    Whenever I tried to set value using jQuery.val(untrusted_data) or element.value=untrusted_data it seems to be safe enough. So is it really XSS safe or did I miss any case here ?

    Assuming untrusted_data is a string and the element whose value is being set is a regular text node in a flow or block element then you’re fine. You might run into trouble if the node whose value is being assigned is a text node in a <script> element or a URL, event handler, or style attribute node or anything related to <object>.

    Also, I happen to read somewhere that jQuery.setAttribute(attrName,untrusted_data) and setAttribute(attrName,untrusted_data) are generally considered safe only if the Attribute Names doesn’t include URL Context based attributes(src,href etc) or Event Handlers(onClick,onMouseOver etc). If that is the case,

    That is partially correct. Other attributes have sharp edges like style and many things related to <object> and <embed>, and <meta>.

    If you don’t know much about the attribute then don’t expose it to untrusted data.
    Things that are generally safe are attributes that contain textual content like title, or that have enumerated values like dir=ltr and dir=rtl.

    Once you’re dealing with attributes that take more complex values you’re at risk of attackers exploiting obscure browser extensions like -moz-binding in style attributes.

    it seems to be safe enough

    Landmines seem to be safe until you step on them.

    You can’t conclude much from something “seeming safe”. You really do need to look at it and understand what can potentially go wrong, and arrange things so that you are at risk only when there is a perfect storm of things going wrong (P(disaster) = P(failure0) * P(failure1) * …) and that you are not at risk when only one thing goes wrong (P(disaster) = P(failure0) + P(failure1)*P(!failure0) + …).

    how am I supposed to set an href attribute by using setAttribute(“href”,untrusted_data)?

    Don’t do it without whitelisting the protocol.

    if (!/^https?:\/\//i.test(untrusted_data) && !/^mailto:/i.test(untrusted_data)) {
      throw new Error('unsafe');
    }
    

    Is encodeForHtmlAttribute(untrusted_data) from server-side the right way of approach ?

    No. HTML encoding the value passed to setAttribute is redundant and will not help preserve any security properties. <iframe srcdoc> might be a rare exception since its content is HTML if my recollection of recent spec changes is correct.

    What I want to achieve here is to get the text from divName1 and paste it to divName2’s content. The code I wrote above is XSS vulnerable.

    Don’t muck around with HTML. Browsers’ .innerHTML getters are buggy and sometimes that leads to exploits as with backticks acting as value delimiters in IE. Just clone the nodes from one to the other. Something like the below should do it:

    var div1 = $('#divName1');
    for (var child = div1[0].firstChild(); child; child = child.nextSibling) {
      $('#divName2').append([child.cloneNode(true)]);
    }
    

    I can change this to

    jQuery("#divName2").text(divValue)
    

    That’s fine if all you want is the textual content.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read about filtering table plugins. What I'm searching for is like this
I happen to have an ASP.NET 2.0 project that I want to apply a
In explanations I've read about public key cryptography, it is said that some large
I am a beginner in sql server.I have read about buffer cache in sql
I'm developing a php/MySQLi based site. I have a question about what might happen
I am only somewhat familiar with multi-threading in that I've read about it but
I have just read about http://marc.info/?l=php-internals&m=131031747409271&w=2 . I use mysql_query in all my php
I know that it is poor programming and architecture when you have a class
This question came about because code that worked previously in .NET 4.0 failed with
I have read many about REST api in php articles. but I still get

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.