← Back to jQuery Course | Chapter 15: Performance & Best Practices | Lesson 5 of 5

Security और XSS Prevention

Cross-site scripting, या XSS, तब हो सकता है जब untrusted text को HTML के रूप में insert किया जाए। जब तक HTML जानबूझकर ज़रूरी न हो, user input को data की तरह treat करें।
Syntax
javascript
$(selector).text(userInput); // safe
$(selector).html(userInput); // unsafe with untrusted input

XSS क्या है?

Cross-site scripting (XSS) तब होता है जब untrusted text — कोई user द्वारा type किया गया, या किसी external API से return किया गया — page में execut।

उदाहरण: What is XSS?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="output"></div>
    <script>
      const userInput = "<img src=x onerror=alert(1)>";
      $("#output").text(userInput);
    </script>
  </body>
</html>

text() बनाम html()

text() method आपके दिए गए किसी भी value को plain text की तरह treat करता है और उसे safely escape करता है, जबकि html() string को actual HTML markup की तरह parse करता है।

उदाहरण: text() vs html()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="safe"></p><p id="risky"></p>
    <script>
      const input = "<b>bold</b>";
      $("#safe").text(input);
      $("#risky").html(input);
    </script>
  </body>
</html>

Untrusted Content Escape करें

जब भी content users से या किसी ऐसे external system से आए जिसे आप पूरी तरह control नहीं करते, उसे HTML के रूप में insert करने के बजाय plain text की तरह रखें।

उदाहरण: Escape Untrusted Content

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="comment"></div>
    <script>
      const userComment = "Nice post!";
      $("#comment").text(userComment);
    </script>
  </body>
</html>

Unsafe String Building से बचें

untrusted values शामिल करने वाली strings को concatenate करके HTML बनाना एक common तरीका है जिससे XSS risks किसी application में घुस आते हैं, क्योंकि यह भूलना आसान है।

उदाहरण: Avoid Unsafe String Building

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="output"></div>
    <script>
      const username = "Alice";
      $("<p>").text("Hello, " + username).appendTo("#output");
    </script>
  </body>
</html>

एक छोटा Security Project

एक simple, reliable rule follow करें: किसी user या किसी external source से आई किसी भी value को default रूप से untrusted मानें। इसे दिखाने के लिए text() इस्तेमाल करें।

उदाहरण: Small Security Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <ul id="feed"></ul>
    <script>
      const comments = ["Great!", "<img src=x onerror=alert(1)>"];
      comments.forEach(function(c) {
      $("<li>").text(c).appendTo("#feed");
      });
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक user द्वारा type किए गए text के लिए .html(userInput) इस्तेमाल करना, जो <img onerror=...> जैसा injected markup चला सकता है; .text(userInput) इस्तेमाल करें।
  2. सिर्फ <script> tags हटाकर sanitize करने की कोशिश करना, जबकि attackers onerror जैसे दूसरे tags और attributes इस्तेमाल कर सकते हैं।
  3. untrusted input के साथ "<p>" + name + "</p>" जैसी strings को concatenate करके HTML बनाना, जबकि value को बाद में .text() से set किया जाना चाहिए।
चैप्टर सारांश
  • Selectors cache करना और chaining इस्तेमाल करना repeated work से बचाते हैं।
  • Efficient event handling और reflow तथा repaint कम करना pages को responsive रखते हैं।
  • Security practices, XSS prevention सहित, jQuery code को safe रखती हैं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.