Escape brackets in occ output before rendering

jQuery Terminal reads "[" and "]" as its own formatting syntax. occ
output was echoed as is, so literal brackets, such as Symfony's
"[alias]" in `occ list`, closed the color formatting of the next line
early and moved text to the wrong place. Text from the output, like a
display name, could also inject terminal formatting.

Escape the brackets in the text and keep the ANSI sequences, so the
colors still work.
This commit is contained in:
2026-09-30 02:07:46 +02:00
parent 76368f154e
commit 635cdd4b24
+11 -1
View File
@@ -14,6 +14,16 @@
}
var baseUrl = OC.generateUrl('/apps/extended_occweb');
// occ output is plain text with ANSI colors, but jQuery Terminal reads
// "[" and "]" as its own formatting syntax. Escape the brackets in the
// text (e.g. Symfony's "[alias]" in `occ list`) and leave the ANSI
// sequences alone, so from_ansi can still turn them into colors.
function escapeOutputBrackets(output) {
return String(output).replace(/(\x1B\[[0-9;]*[A-Za-z])|[\[\]]/g, function (match, ansi) {
return ansi ? ansi : (match === '[' ? '[' : ']');
});
}
// Current terminal mode: 'occ' (regular occ commands) or 'sql'
// (running arbitrary SQL queries via /db/query).
var mode = 'occ';
@@ -386,7 +396,7 @@
headers: { requesttoken: OC.requestToken },
data: JSON.stringify(occCommand)
}).done(function (response) {
term.echo('\n' + response).resume();
term.echo('\n' + escapeOutputBrackets(response)).resume();
}).fail(function (xhr, status) {
term.echo('\n[[;#ff5555;]Request failed: ]' + $.terminal.escape_formatting(xhr.status + ' ' + xhr.statusText)).resume();
});