Skip to main content
Menu
Advisories

getgrav/grav-plugin-admin2 cross-site scripting (XSS)

GHSA-96XM-C5HR-59RXPublished Updated GitHub

Description

i18n.tHtml(key, params) looks up a translation string, substitutes params into it (e.g. replacing {username} with the actual username), and then runs the whole resulting string through marked.parseInline(). Interpolation happens before parsing, so if a params value itself contains markdown or raw HTML syntax, that syntax is live by the time marked sees it, it isn't inert data sitting inside a placeholder, it's part of the markdown source.

Several call sites pass a real user's username or display name as one of these interpolation values: the two-factor "force disable" confirmation prompt, and the "this page is locked by {owner}" notice shown to another editor trying to open the same page. Grav's server-side username validation (DataUser::isValidUsername()) blocks filesystem-dangerous characters (\ / ? * : ; { }) but does not block <, >, ", or '. A username can be <img src=x onerror=alert(1)> and pass validation.

Combined: create (or have) an account with such a username, get any admin to view a UI surface that renders your username through tHtml(), and the payload executes in their session. onerror fires without any click.

Affected product and version

Product: Grav Admin2 frontend, getgrav/grav-admin-next Confirmed present in: current develop checkout, commit c2133c3f9def8a1c86e49fe3a4777d3b9cea8549 Related server-side validation gap: getgrav/grav, system/src/Grav/Common/User/DataUser/User.php

Affected code

src/lib/stores/i18n.svelte.ts, tHtml():

function tHtml(key: string | undefined, params?: TranslateParams): string {
    const text = t(key, params);   // <-- interpolation happens here, params substituted into the template
    try {
        return marked.parseInline(text, { async: false }) as string;  // <-- then the WHOLE string is parsed as markdown
    } catch (err) {
        console.warn(`[i18n] tHtml render failed for "${key}":`, err);
        return text;
    }
}

Two concrete call sites passing a real user's name through, unescaped, as an interpolation value:

src/lib/components/TwoFactorField.svelte:

{@html i18n.tHtml('ADMIN_NEXT.TWO_FACTOR_FIELD.FORCE_DISABLE_PROMPT', { username })}

src/lib/components/sync/EditorLockNotice.svelte:

{@html i18n.tHtml('ADMIN_NEXT.SYNC.EDITOR_LOCK_NOTICE.LOCKED', {
    owner: ownerName,
    editor: ownerLabel,
    href: `${base}/users/${auth.username}`,
})}

ownerName is the display name/username of whoever currently has the page locked for editing, shown to any other user, including an admin, who tries to open the same page.

Server-side validation gap that makes the payload constructible in the first place, system/src/Grav/Common/User/DataUser/User.php, isValidUsername():

public static function isValidUsername(string $username): bool
{
    if (!$username) {
        return false;
    }

    // Username must not contain filesystem-dangerous characters: \ / ? * : ; { } or newlines
    if (!preg_match('/^[^\\\\/?*:;{}\\\\\\\\\\n]+$/u', $username)) {
        return false;
    }

    // Username must not contain path traversal sequences (..)
    if (str_contains($username, '..')) {
        return false;
    }
    ...

<, >, ", ' are all absent from the blocked-character set.

Proof of concept, verified, real output

I confirmed both halves independently: that the server-side username validation genuinely allows the payload, and that the client-side interpolation-then-parse sequence genuinely turns it into live markup.

Server-side: I traced isValidUsername()'s regex directly (shown above), <img src=x onerror=...> contains none of the characters it excludes (\ / ? * : ; { }, newline, or ..), so it passes.

Client-side, using the exact pinned marked version (17.0.5) and reproducing tHtml()'s real sequence, interpolate first, then parseInline:

import { marked } from 'marked';

const template = 'Are you sure you want to disable 2FA for **{username}**?';

const payloads = [
    '<img src=x onerror=alert(document.cookie)>',
    '<svg onload=alert(1)>',
];

for (const maliciousUsername of payloads) {
    const interpolated = template.replace('{username}', maliciousUsername);
    console.log('Username:', maliciousUsername);
    console.log('marked.parseInline() output:', marked.parseInline(interpolated, { async: false }));
    console.log();
}

Actual output:

Username: <img src=x onerror=alert(document.cookie)>
marked.parseInline() output: Are you sure you want to disable 2FA for <strong><img src=x onerror=alert(document.cookie)></strong>?

Username: <svg onload=alert(1)>
marked.parseInline() output: Are you sure you want to disable 2FA for <strong><svg onload=alert(1)></strong>?

Both produce a live, auto-executing element embedded directly in what was meant to be a plain-text confirmation sentence. Note this doesn't even need the javascript:-URI trick my other report against this repo used, <img onerror>/<svg onload> fire without a click and without needing a colon anywhere, which matters because it means this exact payload shape isn't blocked by validation that only thinks to block URI schemes, it needs to block HTML metacharacters specifically.

I was not able to run this against a live Grav instance with a real account and a real admin session to confirm the full end-to-end chain (create account, admin views the prompt, script fires), for the same environment reasons as my other reports, no bootstrapped instance available. What I've verified directly: the exact server-side validation function a real username would be checked against accepts the payload, and the exact client-side transform (interpolate then parse) a real tHtml() call would apply to it produces live, executing markup. Both halves of the chain are real, unmodified code, not reconstructions.

Impact

Any account holder (however low-privileged, this only requires being able to have an account, self-registration if the site has it enabled makes this fully unauthenticated-attacker-reachable) can set a username or trigger a "locked by" state that, when viewed by an admin through either of these two UI surfaces, executes arbitrary JavaScript in that admin's authenticated session. EditorLockNotice in particular requires nothing more than the attacker opening a page for editing (any editor-level access) while an admin later tries to open the same page, a completely ordinary, low-suspicion interaction pattern.

Suggested fix

Two independent layers, either alone would help, both together is the right answer:

  1. tHtml() should HTML-escape interpolation values before substituting them into the template, not after. The translation template is trusted (developer-authored), the params are not. Something like:
function tHtml(key: string | undefined, params?: TranslateParams): string {
    const escapedParams = params
        ? Object.fromEntries(Object.entries(params).map(([k, v]) => [k, typeof v === 'string' ? escapeHtml(v) : v]))
        : params;
    const text = t(key, escapedParams);
    try {
        return marked.parseInline(text, { async: false }) as string;
    } catch (err) {
        ...
    }
}

(There's already an escapeHtml helper in this codebase, used correctly in ConfigInfoPage.svelte's highlight() function, reusing it here keeps the fix consistent with existing practice.)

  1. isValidUsername() on the Grav core side should also exclude < > " ', defense in depth, and it closes this class of bug for any other current or future frontend/consumer that might display a username without escaping it, not just these two call sites. I'm not filing this as a separate report against getgrav/grav since it's not independently exploitable there (core doesn't render usernames as markdown/HTML anywhere I've found), but it's a cheap, high-value hardening step given how many places a username inevitably gets displayed across the whole ecosystem.

=========================================================== CWE FIELD =========================================================== CWE-79, Improper Neutralization of Input During Web Page Generation (Cross-site Scripting)

=========================================================== CVSS CALCULATOR SELECTIONS (v3.1) =========================================================== Attack Vector: Network Attack Complexity: Low Privileges Required: Low User Interaction: Required Scope: Changed Confidentiality: High Integrity: High Availability: None

Resulting vector: CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:H/I:H/A:N Approximate score: 8.0, severity High (hand-computed, please re-derive via the calculator UI)

Note for the maintainer: Privileges Required is Low, this needs some account, but not an elevated one, just the ability to set a username or lock a page for editing. If self-registration is enabled on a given site, treat this as effectively PR:None for that deployment. User Interaction is Required because an admin has to view the relevant prompt or notice, but for EditorLockNotice that's an extremely ordinary interaction (an admin trying to edit a page someone else has open), not something requiring the attacker to lure anyone anywhere unusual.

=========================================================== SEVERITY FIELD =========================================================== High

Affected versions

References

GitHub · getgrav/grav