Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing sanitize test requires replacing only < #2

Open
JasonWarrenUK opened this issue Apr 18, 2024 · 0 comments
Open

Passing sanitize test requires replacing only < #2

JasonWarrenUK opened this issue Apr 18, 2024 · 0 comments

Comments

@JasonWarrenUK
Copy link

Current Behaviour

The sanitize test will fail if the characters >&'" are cleaned, and requires only replacing <.

Fail Example

Code

function sanitize(dirty) {
    const clean = dirty.replace(/[<>&'"]/g, function(match) {
        switch (match) {
            case '<':
                return '&lt;';
            case '>':
                return '&gt;';
            case '&':
                return '&amp;';
            case "'":
                return '&#39;';
            case '"':
                return '&quot;';
            }
        }
    );

    return clean;
}

Error Message

npm run test:sanitize

> test:sanitize
> node test/sanitization.test.js

POST /
GET /
✖ POST with script tag is correctly sanitized (117.702346ms)
  AssertionError [ERR_ASSERTION]: Expected <script> to have '<' replaced with '&lt;', but received:

      <!doctype html>
      <html>
        <head>
          <title>All posts</title>
          <meta name="viewport" content="width=device-width, initial-scale=1">
        </head>
        <body>

      <h2>New post</h2>
      <form method="POST">
        <p>
          <label for="nickname">Nickname</label>
          <input id="nickname" name="nickname">
        </p>
        <p>
          <label for="message">Message</label>
          <textarea id="message" name="message"></textarea>
        </p>
        <button>Send</button>
      </form>
      <h2>All posts</h2>
      <ul>

      <li>
        <p>&lt;script&gt;alert(&#39;uh oh&#39;)&lt;/script&gt;</p>
        <p>—oli | 18/04/2024, 21:50:16</p>
      </li>

      </ul>

        </body>
      </html>

      at TestContext.<anonymous> (/Users/jasonwarren/Code/FAC/FAC-Challenge-Sanitize/test/sanitization.test.js:12:10)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async Test.run (node:internal/test_runner/test:548:9)
      at async startSubtest (node:internal/test_runner/harness:190:3) {
    generatedMessage: false,
    code: 'ERR_ASSERTION',
    actual: '\n    <!doctype html>\n    <html>\n      <head>\n        <title>All posts</title>\n        <meta name="viewport" content="width=device-width, initial-scale=1">\n      </head>\n      <body>\n        \n    <h2>New post</h2>\n...',
    expected: /&lt;script>alert\('uh oh'\)&lt;\/script>/i,
    operator: 'match'
  }

ℹ tests 1
ℹ pass 0
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 221.632316

Success Example

function sanitize(dirty) {
    //removed other characters
    const clean = dirty.replace(/[<]/g,
        switch (match) {
            case '<':
                return '&lt;';
            // case '>':
            //     return '&gt;';
            // case '&':
            //     return '&amp;';
            // case "'":
            //     return '&#39;';
            // case '"':
            //     return '&quot;';
            }
        }
    );

    return clean;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant