为什么不应该出现这种错误呢?

时间:2017-04-27 20:00:51

标签: javascript html if-statement

我一直在编写一个脚本来检查反映的XSS漏洞。我检查你是否有" http://"或" https://"在您的网址和' *'在查询的地方。但是,当我把https://google.com/#q=*", it results in错误时! MISSING' http://',或' https://'!`。这是我的代码:

<!DOCTYPE html>

<html>

  <head>

    <title>Slingshot.XSS</title>

  </head>

  <body style="font-family:monospace;" align="center">

    <h2>Slingshot.XSS</h2>
    <h3>Slingshot.XSS is a script that launches pre-loaded XSS payloads at a target to test its vulnerabilities.</h3>
    <h4>Please report all issues to <a href="https://github.com/keeganjk/slingshot.xss/issues"></a> or contact me at keeganjkuhn@gmail.com.</h4>
    <a href="github.com/keeganjk/slingshot.xss" style="font-family:monospace" align="center">Source Code / Learn More</a>
    <br />

    <h4>Enter a URL with <b>*</b> in the place of query.</h4>
    <h5>Example: https://www.google.com/#q=*</h5>
    <input type="text" id="myText" placeholder="Enter a URL"> <button onclick="myFunction()">Submit</button>

    <p id="demo">No Submitted URL</p>

    <script>

      function myFunction() {

        var x = document.getElementById("myText").value;

        // Error check
        if ( !x.includes("*") && ( !x.includes("http://") || !x.includes("https://") ) ) {

            document.getElementById("demo").innerHTML = "ERROR! MISSING \'*\' IN PLACE OF QUERY, \'http://\', AND \'https://\'!";
            x = false;
            return 0;

        }

        if ( !x.includes("*") ) {

            document.getElementById("demo").innerHTML = "ERROR! MISSING \'*\' IN PLACE OF QUERY!";
            x = false;
            return 0;

        }

        if ( !x.includes("http://") || !x.includes("https://") ) {

            document.getElementById("demo").innerHTML = "ERROR! MISSING \'http://\', OR \'https://\'!";
            x = false;
            return 0;

        }

        document.getElementById("demo").innerHTML = x;

      }

    </script>

  </body>

</html>

我做错了什么?

3 个答案:

答案 0 :(得分:3)

您检查http是否不在,或者https不在。两者之一将始终为真。 一个接一个地执行检查......例如

答案 1 :(得分:0)

您需要正确编写if条件。

更改条件

if ( !x.includes("http://") || !x.includes("https://") ) {

if ( !(x.includes("http://") || x.includes("https://")) ) {

这样,只有在网址不包含http://https://

时才会引发错误

完整代码:

&#13;
&#13;
<!DOCTYPE html>

    <html>

      <head>

        <title>Slingshot.XSS</title>

      </head>

      <body style="font-family:monospace;" align="center">

        <h2>Slingshot.XSS</h2>
        <h3>Slingshot.XSS is a script that launches pre-loaded XSS payloads at a target to test its vulnerabilities.</h3>
        <h4>Please report all issues to <a href="https://github.com/keeganjk/slingshot.xss/issues"></a> or contact me at keeganjkuhn@gmail.com.</h4>
        <a href="github.com/keeganjk/slingshot.xss" style="font-family:monospace" align="center">Source Code / Learn More</a>
        <br />

        <h4>Enter a URL with <b>*</b> in the place of query.</h4>
        <h5>Example: https://www.google.com/#q=*</h5>
        <input type="text" id="myText" placeholder="Enter a URL"> <button onclick="myFunction()">Submit</button>

        <p id="demo">No Submitted URL</p>

        <script>

          function myFunction() {

            var x = document.getElementById("myText").value;

            // Error check
            if ( !x.includes("*") && ( !x.includes("http://") || !x.includes("https://") ) ) {

                document.getElementById("demo").innerHTML = "ERROR! MISSING \'*\' IN PLACE OF QUERY, \'http://\', AND \'https://\'!";
                x = false;
                return 0;

            }

            if ( !x.includes("*") ) {

                document.getElementById("demo").innerHTML = "ERROR! MISSING \'*\' IN PLACE OF QUERY!";
                x = false;
                return 0;

            }

            if ( !(x.includes("http://") || x.includes("https://")) ) {

                document.getElementById("demo").innerHTML = "ERROR! MISSING \'http://\', OR \'https://\'!";
                x = false;
                return 0;

            }

            document.getElementById("demo").innerHTML = x;

          }

        </script>

      </body>

    </html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我重构了您的函数,以展示在将验证逻辑与错误呈现分开时如何降低代码的复杂性。

function myFunction() {
    var errors = [];
    var x = document.getElementById("myText").value;

    if (!x.includes("http://") && !x.includes("https://")) {
      errors.push('missing HTTP or HTTPS');
    }

    if (!x.includes("*")) {
      errors.push('missing * in place of query')
    }

    // render the errors
    if (errors.length) {
      x = 'Error: ' + errors.join(', ') + '!';        
    }

    document.getElementById("demo").innerHTML = x;
}

&#13;
&#13;
<!DOCTYPE html>

<html>

  <head>

    <title>Slingshot.XSS</title>

  </head>

  <body style="font-family:monospace;" align="center">

    <h2>Slingshot.XSS</h2>
    <h3>Slingshot.XSS is a script that launches pre-loaded XSS payloads at a target to test its vulnerabilities.</h3>
    <h4>Please report all issues to <a href="https://github.com/keeganjk/slingshot.xss/issues"></a> or contact me at keeganjkuhn@gmail.com.</h4>
    <a href="github.com/keeganjk/slingshot.xss" style="font-family:monospace" align="center">Source Code / Learn More</a>
    <br />

    <h4>Enter a URL with <b>*</b> in the place of query.</h4>
    <h5>Example: https://www.google.com/#q=*</h5>
    <input type="text" id="myText" placeholder="Enter a URL"> <button onclick="myFunction()">Submit</button>

    <p id="demo">No Submitted URL</p>

    <script>

      function myFunction() {
        var errors = [];
        var x = document.getElementById("myText").value;

        if (!x.includes("http://") && !x.includes("https://")) {
          errors.push('missing HTTP or HTTPS');
        }
        
        if (!x.includes("*")) {
          errors.push('missing * in place of query')
        }

        if (errors.length) {
          x = 'Error: ' + errors.join(', ') + '!';        
        }


        document.getElementById("demo").innerHTML = x;

      }
    </script>
&#13;
&#13;
&#13;