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

Create semgrep.test.js #30396

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions semgrep.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const el = element.innerHTML;

function bad1(userInput) {
// ruleid: insecure-document-method
const { JSDOM } = require('jsdom');

function bad1(userInput) {
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>');
const safeElement = dom.window.document.createElement('div');
safeElement.textContent = userInput;
el.appendChild(safeElement);
}
}

function bad2(userInput) {
// ruleid: insecure-document-method
// Import jsdom at the top of your file
const { JSDOM } = require('jsdom');

function bad2(userInput) {
// Create a new JSDOM instance
const dom = new JSDOM('<!DOCTYPE html><body></body>');
const document = dom.window.document;

// Safely set the content by creating a new element and setting its text content
const newElement = document.createElement('div');
newElement.textContent = userInput;
document.body.appendChild(newElement);
}
}

function bad3(userInput) {
const name = '<div>' + userInput + '</div>';
// ruleid: insecure-document-method
// Import DOMPurify to sanitize user input
const DOMPurify = require('dompurify');

function bad3(userInput) {
// Sanitize the user input to prevent XSS
const sanitizedInput = DOMPurify.sanitize('<div>' + userInput + '</div>');

// Use document.write with sanitized input
document.write(sanitizedInput);
}
}

function ok1() {
const name = "<div>it's ok</div>";
// ok: insecure-document-method
el.innerHTML = name;
}

function ok2() {
// ok: insecure-document-method
document.write("<div>it's ok</div>");
}