1. Archivio CVE
  2. 2021
  3. CVE-2021-29474

CVE-2021-29474

5.8 Gravità media CVSS v3.1 · fonte NVD CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:C/C:L/I:N/A:N 5.8 – Media

Descrizione

HedgeDoc (formerly known as CodiMD) is an open-source collaborative markdown editor. An attacker can read arbitrary `.md` files from the server's filesystem due to an improper input validation, which results in the ability to perform a relative path traversal. To verify if you are affected, you can try to open the following URL: `http://localhost:3000/..%2F..%2FREADME#` (replace `http://localhost:3000` with your instance's base-URL e.g. `https://demo.hedgedoc.org/..%2F..%2FREADME#`). If you see a README page being rendered, you run an affected version. The attack works due the fact that the internal router passes the url-encoded alias to the `noteController.showNote`-function. This function passes the input directly to findNote() utility function, that will pass it on the the parseNoteId()-function, that tries to make sense out of the noteId/alias and check if a note already exists and if so, if a corresponding file on disk was updated. If no note exists the note creation-function is called, which pass this unvalidated alias, with a `.md` appended, into a path.join()-function which is read from the filesystem in the follow up routine and provides the pre-filled content of the new note. This allows an attacker to not only read arbitrary `.md` files from the filesystem, but also observes changes to them. The usefulness of this attack can be considered limited, since mainly markdown files are use the file-ending `.md` and all markdown files contained in the hedgedoc project, like the README, are public anyway. If other protections such as a chroot or container or proper file permissions are in place, this attack's usefulness is rather limited. On a reverse-proxy level one can force a URL-decode, which will prevent this attack because the router will not accept such a path.

Prodotti coinvolti

FornitoreProdottoVersionePiattaforma
hedgedoc hedgedoc < 1.8.0 –
Tutte le 19 vulnerabilità di Hedgedoc

Tipo di vulnerabilità

CWE-22 Path traversal: percorso non limitato alla cartella consentita

Il prodotto costruisce il percorso di un file o di una cartella con dati provenienti dall’esterno, senza neutralizzare gli elementi speciali (come «../») che permettono di uscire dalla cartella a cui l’accesso dovrebbe essere limitato.

Impatto sui sistemi

AmbitoImpatto
Integrità, Riservatezza, Disponibilità Eseguire codice o comandi non autorizzati
Integrità Modificare file o cartelle
Riservatezza Leggere file o cartelle
Disponibilità Interruzione del servizio: arresto o riavvio

Come si previene

Sviluppo · Validazione dell’input Considera ogni input come potenzialmente malevolo. Adotta una validazione per «elenco di valori ammessi»: accetta solo gli input che rispettano rigorosamente le specifiche e rifiuta, o trasforma in qualcosa di conforme, tutto il resto. Nel validare considera tutte le proprietà rilevanti: lunghezza, tipo, intervallo dei valori ammessi, input mancanti o in più, sintassi, coerenza tra campi collegati e rispetto delle regole di business (per esempio «barca» è sintatticamente valido perché contiene solo lettere, ma non lo è se il campo deve contenere solo colori come rosso o blu). Non limitarti a cercare input malevoli o malformati: quasi certamente ne sfuggirà qualcuno, soprattutto se l’ambiente del codice cambia, lasciando all’attaccante lo spazio per aggirare la validazione. Le liste di valori vietati restano utili per rilevare possibili attacchi o scartare subito gli input più malformati. Per i nomi di file usa elenchi rigorosi dei caratteri ammessi. Se possibile, consenti un solo carattere «.» nel nome, per evitare errori come CWE-23, ed escludi i separatori di cartella come «/», per evitare CWE-36. Usa un elenco delle estensioni ammesse, utile anche contro CWE-434. Non affidarti solo a un filtro che rimuove i caratteri pericolosi: equivale a una lista di valori vietati, che può essere incompleta (CWE-184). Per esempio, filtrare «/» non basta se il file system accetta anche «\» come separatore; e un filtro applicato male può produrre comunque dati pericolosi (CWE-182): se si rimuovono in sequenza le occorrenze di «../» dalla stringa «.../...//», i caratteri rimasti formano di nuovo «../».
Architettura e progettazione Per ogni controllo di sicurezza eseguito lato client, ripeti lo stesso controllo lato server, per evitare CWE-602: un attaccante può aggirare i controlli del client modificando i valori dopo il controllo o eliminando del tutto i controlli dal client, e inviare poi al server i valori modificati.
Sviluppo · Validazione dell’input Inputs should be decoded and canonicalized to the application's current internal representation before being validated (CWE-180). Make sure that the application does not decode the same input twice (CWE-174). Such errors could be used to bypass allowlist validation schemes by introducing dangerous inputs after they have been checked. Use a built-in path canonicalization function (such as realpath() in C) that produces the canonical version of the pathname, which effectively removes .. sequences and symbolic links (CWE-23, CWE-59). This includes: realpath() in C getCanonicalPath() in Java GetFullPath() in ASP.NET realpath() or abs_path() in Perl realpath() in PHP
Architettura e progettazione · Librerie o framework Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid.
Esercizio · Firewall Usa un firewall applicativo (WAF) in grado di rilevare attacchi contro questo tipo di vulnerabilità. È utile quando il codice non può essere corretto (perché controllato da terzi), come misura d’emergenza in attesa di interventi più completi, o come difesa aggiuntiva.
Architettura e progettazione · Esercizio · Hardening dell’ambiente Run your code using the lowest privileges that are required to accomplish the necessary tasks. If possible, create isolated accounts with limited privileges that are only used for a single task. That way, a successful attack will not immediately give the attacker access to the rest of the software or its environment. For example, database applications rarely need to run as the database administrator, especially in day-to-day operations.
Architettura e progettazione · Conversione in valori ammessi When the set of acceptable objects, such as filenames or URLs, is limited or known, create a mapping from a set of fixed input values (such as numeric IDs) to the actual filenames or URLs, and reject all other inputs. For example, ID 1 could map to inbox.txt and ID 2 could map to profile.txt. Features such as the ESAPI AccessReferenceMap provide this capability.
Architettura e progettazione · Esercizio · Sandbox o jail Esegui il codice in una «jail» o in una sandbox simile, che imponga confini rigidi tra il processo e il sistema operativo. Così si può limitare a quali file in una cartella si può accedere o quali comandi il software può eseguire (per esempio con chroot su Unix, AppArmor o SELinux). Il codice gestito può offrire una certa protezione: in Java, per esempio, java.io.FilePermission del SecurityManager permette di limitare le operazioni sui file. Non sempre è una soluzione praticabile e limita l’impatto solo sul sistema operativo: il resto dell’applicazione può comunque essere compromesso. Attenzione a CWE-243 e agli altri errori legati alle jail.

Le indicazioni di mitigazione sono riportate nella lingua originale del catalogo CWE del MITRE.

Tutte le 11.315 CVE di tipo CWE-22, con andamento e prevenzione

Fonte: catalogo CWE del MITRE · cwe.mitre.org