Render native PowerShell CP850 output with an explicit decoding choice
This commit is contained in:
@@ -37,3 +37,32 @@ it('shows unknown restart state for an incomplete Windows Update summary', () =>
|
||||
expect(rendered(windowsUpdateScriptHash)).toContain('Neustartbedarf: unknown');
|
||||
expect(rendered('ordinary script')).toContain('Neustartbedarf: false');
|
||||
});
|
||||
|
||||
|
||||
it('decodes native PowerShell CP850 umlauts while preserving raw bytes and control escapes', () => {
|
||||
const raw = outputBytes([132, 148, 129, 142, 153, 154, 225, 13, 10, 27]);
|
||||
const original = Array.from(raw);
|
||||
expect(outputText(raw, 'powershell').text).toBe('äöüÄÖÜß\\u000d\n\\u001b');
|
||||
expect(outputText(raw, 'powershell').encoding).toContain('Quellcodepage nicht separat belegt');
|
||||
expect(outputText(raw, 'utf-8').text).toContain('�');
|
||||
expect(Array.from(raw)).toEqual(original);
|
||||
expect(outputText(outputBytes([156, 255]), 'cp850').text).toBe('£ ');
|
||||
});
|
||||
|
||||
it('keeps valid UTF-8 PowerShell output and generic invalid UTF-8 on their existing paths', () => {
|
||||
const raw = new TextEncoder().encode('Grüße äöüÄÖÜß');
|
||||
expect(outputText(raw, 'powershell').text).toBe('Grüße äöüÄÖÜß');
|
||||
expect(outputText(outputBytes([148])).text).toBe('�');
|
||||
});
|
||||
|
||||
it('selects the native PowerShell text view only from matching result evidence', () => {
|
||||
const result = { stdout: [98, 148, 115, 97, 114, 116, 105, 103], stderr: [], evidence: {interpreter:'powershell',output_encoding:'raw_native_bytes'} };
|
||||
const before = JSON.stringify(result);
|
||||
const html = renderToStaticMarkup(<ConsoleOutput job={{state:'succeeded',attempt_id:'test',result}}/>);
|
||||
expect(html).toContain('bösartig');
|
||||
expect(html).toContain('stdout Textdekodierung');
|
||||
expect(html).toContain('Windows OEM 850');
|
||||
expect(JSON.stringify(result)).toBe(before);
|
||||
const other = renderToStaticMarkup(<ConsoleOutput job={{result:{...result,evidence:{interpreter:'posix_sh',output_encoding:'raw_native_bytes'}}}}/>);
|
||||
expect(other).not.toContain('bösartig');
|
||||
});
|
||||
|
||||
@@ -1,16 +1,32 @@
|
||||
import { useState } from 'react';
|
||||
import { windowsUpdateScriptHash } from './script-console';
|
||||
import { record, RecordValue, stringField } from './api';
|
||||
|
||||
/** The wire format carries bytes, not an encoding declaration. Never guess a
|
||||
* Windows code page or interpret output as HTML, terminal escapes or actions. */
|
||||
/** Preserve wire bytes. Text decoding is a labeled display choice, never HTML or terminal interpretation. */
|
||||
export function outputBytes(value: unknown): Uint8Array<ArrayBuffer> {
|
||||
if (!Array.isArray(value) || value.length > 262144 || value.some(byte => !Number.isInteger(byte) || byte < 0 || byte > 255)) throw new Error('Ungültige oder zu große Ausgabebytes');
|
||||
return new Uint8Array(value);
|
||||
}
|
||||
export function outputText(raw: Uint8Array<ArrayBuffer>): { text: string; encoding: string } {
|
||||
type OutputEncoding = 'utf-8' | 'cp850' | 'powershell';
|
||||
// Unicode mapping of bytes 0x80–0xff in IBM code page 850. ASCII bytes are unchanged.
|
||||
const cp850 = "ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø×ƒáíóúñѪº¿®¬½¼¡«»░▒▓│┤ÁÂÀ©╣║╗╝¢¥┐└┴┬├─┼ãÃ╚╔╩╦╠═╬¤ðÐÊËÈıÍÎÏ┘┌█▄¦Ì▀ÓßÔÒõÕµþÞÚÛÙýݯ´±‗¾¶§÷¸°¨·¹³²■ ";
|
||||
function cp850Text(raw: Uint8Array<ArrayBuffer>): string {
|
||||
return Array.from(raw, byte => byte < 128 ? String.fromCharCode(byte) : cp850[byte - 128]).join('');
|
||||
}
|
||||
export function outputText(raw: Uint8Array<ArrayBuffer>, selected: OutputEncoding = 'utf-8'): { text: string; encoding: string } {
|
||||
let decoded: string, encoding: string;
|
||||
try { decoded = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(raw); encoding = 'UTF-8 lesbar; Quellencoding nicht separat belegt'; }
|
||||
catch { decoded = new TextDecoder('utf-8', { ignoreBOM: true }).decode(raw); encoding = 'Encoding unbekannt / kein gültiges UTF-8: Ersatzzeichen nur in der Textansicht'; }
|
||||
if (selected === 'cp850') {
|
||||
decoded = cp850Text(raw); encoding = 'Textdekodierung Windows OEM 850 (CP850)';
|
||||
} else {
|
||||
try { decoded = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(raw); encoding = 'UTF-8 lesbar; Quellencoding nicht separat belegt'; }
|
||||
catch {
|
||||
if (selected === 'powershell') {
|
||||
decoded = cp850Text(raw); encoding = 'Textdekodierung Windows OEM 850 (CP850), automatisch für native PowerShell-Ausgabe gewählt; Quellcodepage nicht separat belegt';
|
||||
} else {
|
||||
decoded = new TextDecoder('utf-8', { ignoreBOM: true }).decode(raw); encoding = 'Encoding unbekannt / kein gültiges UTF-8: Ersatzzeichen nur in der Textansicht';
|
||||
}
|
||||
}
|
||||
}
|
||||
// Make terminal/control and bidi characters visible. In particular CR cannot
|
||||
// visually overwrite an earlier line; CRLF remains recognizable as such.
|
||||
return { text: decoded.replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069\ufeff]/g,
|
||||
@@ -22,10 +38,15 @@ function download(raw: Uint8Array<ArrayBuffer>, name: 'stdout' | 'stderr') {
|
||||
link.href = url; link.download = `${name}.bin`; link.click();
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||||
}
|
||||
function OutputChannel({ value, name }: { value: unknown; name: 'stdout' | 'stderr' }) {
|
||||
function OutputChannel({ value, name, nativePowerShell }: { value: unknown; name: 'stdout' | 'stderr'; nativePowerShell: boolean }) {
|
||||
const [selected, setSelected] = useState<'auto' | 'utf-8' | 'cp850'>('auto');
|
||||
try {
|
||||
const raw = outputBytes(value), view = outputText(raw);
|
||||
const raw = outputBytes(value), view = outputText(raw, selected === 'auto' ? nativePowerShell ? 'powershell' : 'utf-8' : selected);
|
||||
return <div><h4>{name} · {raw.length} gespeicherte Bytes</h4><p className="muted">{view.encoding}. Steuerzeichen werden sichtbar als \\uXXXX dargestellt; Rohbytes bleiben erhalten.</p>
|
||||
<label>{name} Textdekodierung<select aria-label={`${name} Textdekodierung`} value={selected} onChange={event => setSelected(event.target.value as typeof selected)}>
|
||||
<option value="auto">{nativePowerShell ? 'Automatisch · UTF-8, sonst CP850' : 'Automatisch · UTF-8'}</option>
|
||||
<option value="utf-8">UTF-8</option><option value="cp850">Windows OEM 850 (CP850)</option>
|
||||
</select></label>
|
||||
<pre className="console-output" aria-label={`${name} Textansicht`}>{raw.length ? view.text : '(leere Ausgabe)'}</pre>
|
||||
<button type="button" onClick={() => download(raw, name)}>{name} als unveränderte Rohbytes herunterladen</button>
|
||||
<details><summary>{name} Originalbytes (dezimal)</summary><pre>{JSON.stringify(Array.from(raw))}</pre></details>
|
||||
@@ -40,18 +61,19 @@ export function ConsoleOutput({ job }: { job: RecordValue }) {
|
||||
{state === 'unknown' && <p className="warning">Wirkung oder Ergebnis unklar. Keine automatische Wiederholung; ein neuer Versuch benötigt Abgleich und einen neuen genehmigten Plan.</p>}
|
||||
{job.cancel_requested === true && <p className="warning">Abbruch angefordert; dies bestätigt weder Prozessende noch Rollback.</p>}
|
||||
{job.pending_reason != null && <p>Wartegrund: {String(job.pending_reason)}</p>}
|
||||
{job.result == null ? <p>Noch kein gespeichertes Ergebnis. Fehlende Ausgabe ist kein Erfolgsnachweis.</p> : <Result value={job.result}/>}
|
||||
{job.result == null ? <p>Noch kein gespeichertes Ergebnis. Fehlende Ausgabe ist kein Erfolgsnachweis.</p> : <Result key={`${stringField(job, 'job_id')}/${stringField(job, 'attempt_id')}`} value={job.result}/>}
|
||||
</section>;
|
||||
}
|
||||
function Result({ value }: { value: unknown }) {
|
||||
try {
|
||||
const result = record(value, 'Jobergebnis');
|
||||
const evidence = result.evidence && typeof result.evidence === 'object' && !Array.isArray(result.evidence) ? record(result.evidence) : {};
|
||||
const nativePowerShell = evidence.interpreter === 'powershell' && evidence.output_encoding === 'raw_native_bytes';
|
||||
const reboot = evidence.script_sha256 === windowsUpdateScriptHash && Object.hasOwn(evidence, 'windows_update_reboot_required') ? evidence.windows_update_reboot_required : result.reboot_required;
|
||||
return <><p>Exitcode: {typeof result.exit_code === 'number' && Number.isInteger(result.exit_code) ? result.exit_code : 'unknown / nicht gemeldet'} · Fehlercode: {typeof result.error_code === 'string' && result.error_code ? result.error_code : 'nicht gemeldet'}</p>
|
||||
<p className={result.output_truncated === false ? 'muted' : 'warning'}>Ausgabe begrenzt: {typeof result.output_truncated === 'boolean' ? String(result.output_truncated) : 'unknown'}. {result.output_truncated === true ? 'Die gespeicherten Bytes sind nur ein Ausschnitt.' : result.output_truncated !== false ? 'Vollständigkeit ist nicht bestätigt.' : 'Keine Trunkierung gemeldet.'}</p>
|
||||
<p>Neustartbedarf: {typeof reboot === 'boolean' ? String(reboot) : 'unknown'}. Ein Exitcode 0 bestätigt Prozessabschluss; fachliche Nachbedingungen stehen im vollständigen Ergebnis.</p>
|
||||
<p>Ausgabe ist nicht vertrauenswürdiger Text vom Zielgerät. Sie erteilt keine Genehmigung und wird nicht als Anweisung ausgeführt.</p>
|
||||
<OutputChannel name="stdout" value={result.stdout}/><OutputChannel name="stderr" value={result.stderr}/></>;
|
||||
<OutputChannel name="stdout" value={result.stdout} nativePowerShell={nativePowerShell}/><OutputChannel name="stderr" value={result.stderr} nativePowerShell={nativePowerShell}/></>;
|
||||
} catch { return <p className="error" role="alert">Ergebnisformat unbekannt; vollständige Originaldaten stehen darunter.</p>; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user