What this quiz covers
This quiz focuses on Input Validation And Output Encoding, giving you a quick way to practice the rules, question types, and explanations that matter most for Cyber Security.
A profile value is displayed once as text inside a <div> and once inside a JavaScript string literal in a generated <script> block. The development team currently applies standard HTML entity encoding to the value in both locations.
Which assessment of this design is most accurate?
Cyber Security Quiz
Practice Input Validation And Output Encoding in Cyber Security with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.
This quiz focuses on Input Validation And Output Encoding, giving you a quick way to practice the rules, question types, and explanations that matter most for Cyber Security.
Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.
A profile value is displayed once as text inside a <div> and once inside a JavaScript string literal in a generated <script> block. The development team currently applies standard HTML entity encoding to the value in both locations.
Which assessment of this design is most accurate?
<div> element and inside a JavaScript string literal. HTML entity encoding (converting < to <, " to ", etc.) is designed to neutralize characters that carry meaning in HTML markup. However, inside a JavaScript string, the browser's JavaScript parser runs before HTML entity decoding in that context, meaning an attacker could inject characters like \, ', or " to break out of the string literal and execute arbitrary code — none of which HTML encoding would stop. Answer C is correct: each location demands context-appropriate encoding. The <div> needs HTML encoding; the script block needs JavaScript string encoding (or, better yet, the team should avoid injecting user data into script blocks altogether.
Answer A is wrong because being "inside an HTML document" does not mean every location follows HTML parsing rules — script blocks follow JavaScript parsing rules, creating a separate injection surface. Answer B is wrong because allowlist validation and output encoding serve complementary, not interchangeable, roles; validation reduces what enters the system, but encoding protects each specific output context. Answer D is wrong because you cannot apply JavaScript encoding once and reuse it for HTML output — the two encoding schemes produce different results for different parsers.
The key study tip: always map where data lands and ask "what parser processes this?" One encoding scheme per context, not one for the whole document.An application accepts a username, uses it in a database lookup, and later displays it in an HTML page. The proposed allowlist permits letters, digits, apostrophes, and hyphens because those characters occur in legitimate names.
Which control combination best addresses the two distinct uses of the username?
<, >, ", &, etc. into their HTML entities) prevents the browser from interpreting user data as markup when it's rendered on the page. Answer A applies exactly the right tool to each threat at the right stage, which is why it's correct.
Answer B encodes for HTML before the database lookup, meaning you're storing HTML-encoded data in the database and hoping it displays correctly later. This conflates two separate concerns, corrupts your stored data, and still doesn't properly protect the SQL layer — parameterization is the right SQL defense, not pre-encoding.
Answer C manually escapes apostrophes as a SQL defense, which is fragile and incomplete (it misses many attack vectors), and arbitrarily stripping hyphens for display doesn't address XSS at all — it just breaks legitimate names like "Smith-Jones."
Answer D relies on the browser to encode output, but browsers render HTML — they don't sanitize it on your behalf. Server-side output encoding is your responsibility, not the client's.
The key study pattern: match the defense to the context. SQL belongs with parameterization; HTML output belongs with encoding. Never let one fix try to cover both threats.An identity system allows internationalized account names. It blocks a reserved name and enforces uniqueness by comparing the submitted text before Unicode normalization. A downstream component normalizes names before authorization decisions.
Which change best prevents inconsistent validation and comparison outcomes?
A web framework automatically HTML-encodes template variables. A developer also HTML-encodes user comments before saving them. When a user enters A & B, the page displays A & B rather than A & B.
Which change best fixes the display problem without weakening the injection defense?
& to an input denylist and reject comments containing ampersands.& into & before storing it, then the template engine encodes it again, turning & into &amp; — which renders visibly as A & B instead of A & B. The fix is to store raw, canonical text (exactly what the user typed) and let the template's auto-encoding handle the conversion once at render time. That's precisely what C recommends: keep storage clean and trust the single, consistent encoding layer at the HTML sink.
A is dangerous because disabling template encoding eliminates your primary injection defense entirely. The manually encoded stored data would render correctly, but any variable that skips manual encoding becomes an XSS vulnerability.
B decodes stored comments before inserting them into the template, which sounds logical but creates a race condition of trust: you're now injecting decoded, potentially malicious content into the template, relying solely on the template engine catching everything. It also complicates the data pipeline unnecessarily.
D blocking & on a denylist is a classic security anti-pattern. Denylists are incomplete by design — they break legitimate input and don't address the root cause (double-encoding), leaving you vulnerable to variants you didn't anticipate.
The study tip: whenever you see encoding happening at both storage and rendering, suspect double-encoding. Always encode once, late, and at the sink.After login, an application redirects users to a returnUrl parameter. It permits a value when the raw string starts with https://portal.example.com. A tester supplies https://portal.example.com.attacker.test/path, which passes the check.
Which validation strategy most reliably enforces the intended redirect policy?
https://portal.example.com.attacker.test/path as having the hostname portal.example.com.attacker.test, which clearly doesn't match the approved host portal.example.com. You're comparing structured data, not raw substrings, so no amount of clever string crafting can forge a match.
Looking at the distractors: A is a red herring — HTML-encoding prevents XSS injection but does nothing to validate where the redirect actually sends the user. The domain remains attacker-controlled after encoding. B is a fragile blocklist approach; attackers can bypass it through URL encoding, Unicode tricks, or other obfuscation, and blocklists are notoriously hard to make complete. D is essentially the same flawed prefix-check described in the passage itself, just phrased differently — requiring the hostname to appear "before the first slash" still allows portal.example.com.attacker.test to satisfy the check.
As a study tip, remember: allowlists beat blocklists, and structural validation beats string matching. Any time a question offers a parsing/canonicalization approach versus a "look for this substring" approach for security validation, the structured approach is almost always correct.A document service accepts a user-supplied filename, rejects any input containing the literal substring ../, and then combines the value with /srv/docs/. The underlying platform decodes percent-encoded characters and resolves path segments before opening the file.
Which change most directly corrects the conceptual flaw in this validation design?
..\\ and %2e%2e/.../, but the underlying platform decodes and resolves paths after that check runs. This means an attacker can submit %2e%2e/secrets.txt — the filter sees no ../, passes the input, and then the platform decodes it into ../secrets.txt, enabling path traversal anyway.
B is correct because canonicalization collapses all representations of a path — percent-encoding, double encoding, mixed slashes, redundant segments — into a single normalized form before the security decision is made. Once you have the resolved absolute path, you simply check whether it starts with /srv/docs/. This is a root-cause fix: you're comparing the path the OS actually uses against your intended boundary, regardless of how cleverly the input was encoded.
A is wrong because HTML-encoding protects against cross-site scripting, not path traversal. It's solving a completely different vulnerability class. C expands the denylist but doesn't fix the architecture — it's whack-a-mole. Attackers can still bypass it with double encoding (%252e%252e/) or other variants you haven't anticipated. D is nonsensical from a security standpoint: validating after the file opens means the unauthorized access has already occurred.
The study tip here: denylists fail against encoding tricks; allowlists and canonicalization win. Whenever a question offers "reject more bad patterns" versus "normalize then verify boundary," the latter is almost always the sounder design.A discussion site intentionally permits limited formatting in comments, including <strong> and safe hyperlinks. Encoding every angle bracket prevents script execution but also causes permitted markup to appear as literal text.
Which design best preserves the intended formatting while controlling active content?
script and render all remaining comment text as trusted HTML.<strong>), strips everything else, enforces safe attributes, and restricts URL schemes (blocking javascript: hrefs, for example). This preserves legitimate formatting while neutralizing active content at the structural level.
Option A describes a fundamentally backwards approach — decoding HTML entities after rendering reintroduces dangerous content that sanitization or encoding had already neutralized, essentially undoing your defenses at the worst possible moment.
Option C is a classic blocklist trap. Rejecting only the word "script" is trivially bypassed with techniques like <scr ipt>, <SCRIPT>, or event handlers like onerror=. Blocklists can never anticipate every bypass; allowlists are far more reliable.
Option D addresses symptoms rather than causes. Content Security Policy is a valuable defense-in-depth layer, but it's a browser-side mitigation — it doesn't sanitize the stored HTML. Validating length does nothing to prevent malicious payloads that fit within the limit.
Study tip: On security exams, whenever you see an "allow some HTML" scenario, allowlist-based sanitization is almost always the correct answer. Blocklists and CSP-only solutions are common distractors designed to test whether you understand defense at the source.A server safely HTML-encodes a user's status message into a hidden <div>. Client-side code later reads the div's text and assigns it to another element using innerHTML. A reviewer claims the original server-side encoding makes the second operation safe.
Which statement best evaluates the reviewer's claim?
innerHTML creates a new sink. (correct answer)<div>. That encoding neutralizes any malicious characters at that moment. However, when JavaScript later reads the div's .textContent (or .innerText), the browser automatically decodes the HTML entities back into their original characters — restoring any <script> tags or event handlers the attacker embedded. Assigning that decoded string to another element via innerHTML then interprets it as live HTML, completing the XSS attack. D is correct because the second innerHTML assignment is an entirely new, unsanitized sink, making the reviewer's claim false.
A reflects a common and dangerous misconception — that encoding is a "one and done" protection that travels with the data. It doesn't. B introduces an irrelevant condition; the div's visibility has no bearing on whether JavaScript can read and reinsert its content. C confuses input length limits (a separate control) with injection vulnerability — message length is entirely unrelated to whether malicious HTML gets executed.
Your study tip: always trace data through its full lifecycle in the browser. Ask "where does this data land next?" — because each new sink requires its own protection.A new administrative dashboard displays customer notes imported years ago from several legacy systems. The current customer portal validates newly submitted notes, so the team plans to render all database values directly because they are now considered internal data.
What is the most appropriate security recommendation?
A registration page uses browser-side code to require an age from 18 through 120 and to limit a display name to 40 characters. The API accepts requests from the page and from mobile clients. A developer argues that repeating these checks in the API would be redundant.
Which approach best assigns responsibility for input validation?
User-Agent header for "mobile" provides zero real protection. Any attacker can spoof any user agent header in seconds. Trusting web requests because they came from a browser is the same mistake as trusting client-side validation.
C moves validation to the database layer, which is too late. Malicious or malformed data may trigger errors, corrupt state, or exploit vulnerabilities before it ever reaches a constraint check. Defense-in-depth means catching bad input as early as possible on the server, not delegating it entirely downstream.
D conflates two different controls. HTML encoding is an output-encoding technique used to prevent XSS when rendering data — it is not a substitute for input validation of business rules like age ranges or field lengths.
Study tip: Remember "never trust the client" as a core security axiom. On exam questions, any answer that delegates security responsibility to the browser, a header, or the database should be an immediate red flag.