[SECURITY] XSS via unescaped innerHTML assignment#9
Open
GAURAV-1313 wants to merge 2 commits into
Open
Conversation
✅ Deploy Preview for nofriction canceled.
|
Owner
Author
AI Code ReviewSummaryThe introduced renderUserInput function directly injects potentially unsafe HTML into the DOM without any sanitation or validation, posing a critical security risk. There are no guards against null elements or unsafe input, which could lead to runtime errors or XSS vulnerabilities. Key FindingsBugs
Security
Recommended FixesGeneral
Security Fixes
Suggested Tests
import { JSDOM } from "jsdom";
import { renderUserInput } from "./render";
describe("renderUserInput", () => {
let container;
beforeEach(() => {
// Setup a minimal DOM environment
const dom = new JSDOM(`<!DOCTYPE html><body><div id=\"output\"></div></body>`);
global.document = dom.window.document;
container = document.getElementById("output");
});
afterEach(() => {
jest.restoreAllMocks();
});
it("should render given HTML string into the #output element (happy path)", () => {
const html = "<p>Hello</p>";
renderUserInput(html);
expect(container.innerHTML).toBe(html);
});
it.each(["", "<br>", "<div></div>"]) "should render various valid HTML strings correctly", (html) => {
renderUserInput(html);
expect(container.innerHTML).toBe(html);
});
it("should handle null and undefined gracefully", () => {
expect(() => renderUserInput(null)).not.toThrow();
expect(container.innerHTML).toBe("null");
expect(() => renderUserInput(undefined)).not.toThrow();
expect(container.innerHTML).toBe("undefined");
});
it("should handle numeric input coerced to string", () => {
renderUserInput(1234);
expect(container.innerHTML).toBe("1234");
});
it("should throw if #output element is missing", () => {
// Remove output element
container.remove();
expect(() => renderUserInput("<p>Test</p>")).toThrow(TypeError);
});
it("should not mutate the input argument", () => {
const html = "<span>immutable</span>";
const htmlCopy = html;
renderUserInput(html);
expect(html).toBe(htmlCopy);
});
it("should verify return type is undefined", () => {
const html = "<b>test</b>";
const ret = renderUserInput(html);
expect(ret).toBeUndefined();
});
});Auto-Fix AvailableAdd the 3 issues found. Reviewed by RepoSpace AI. |
- src/render.js: Added a check to ensure the output element exists before attempting to set its innerHTML, preventing potential runtime errors.
Owner
Author
Auto-Fix ResultsApplied (1)
Generated by RepoSpace AI. Review AI commits before merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User input rendered directly as HTML without sanitization.