Script Injection Attacks

If you are using JavaScript, make sure that any variables that cross the PHP- to-JavaScript boundry are passed in the scope field of MongoDB\BSON\Javascript, not interpolated into the JavaScript string. This can come up when using $where clauses in queries, mapReduce and group commands, and any other time you may pass JavaScript into the database.

For example, suppose we have some JavaScript to greet a user in the database logs. We could do:

<?php
$m 
= new MongoDB\Driver\Manager;

// Don't do this!!!
$username $_GET['field']; 

$cmd = new \MongoDB\Driver\Command( [
    
'eval' => "print('Hello, $username!');"
] );

$r $m->executeCommand'dramio'$cmd );
?>

However, what if a malicious user passes in some JavaScript?

<?php
$m 
= new MongoDB\Driver\Manager;

// Don't do this!!!
$username $_GET['field']; 
// $username is set to "'); db.users.drop(); print('"

$cmd = new \MongoDB\Driver\Command( [
    
'eval' => "print('Hello, $username!');"
] );

$r $m->executeCommand'dramio'$cmd );
?>

Now MongoDB executes the JavaScript string "print('Hello, '); db.users.drop(); print('!');". This attack is easy to avoid: use args to pass variables from PHP to JavaScript:

<?php
$m 
= new MongoDB\Driver\Manager;

$_GET['field'] = 'derick';
$args = [ $_GET['field'] ];

$cmd = new \MongoDB\Driver\Command( [
    
'eval' => "function greet(username) { print('Hello, ' + username + '!'); }",
    
'args' => $args,
] );

$r $m->executeCommand'dramio'$cmd );
?>

This adds an argument to the JavaScript scope, which gets used as argument for the greet function. Now if someone tries to send malicious code, MongoDB will harmlessly print Hello, '); db.dropDatabase(); print('!.

Using arguments helps to prevent malicious input from being executed by the database. However, you must make sure that your code does not turn around and execute the input anyway! It is best to avoid executing any JavaScript on the server in the first place.

You are strongly recommended to stay clear of the » $where clause with queries, as it impacts performance significantly. Where possible, use either normal query operators, or the » Aggregation Framework.

As alternative to » MapReduce, which uses JavaScript, consider using the » Aggregation Framework. Unlike Map/Reduce, it uses an idiomatic language to construct queries, without having to write, and use, the slower JavaScript approach that Map/Reduce requires.

The » eval command has been deprecated since MongoDB 3.0, and should also be avoided.

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Spielplan für 4 Gruppen zu je 6 Teams auf 2 Feldern

Hey, hallöchen! Du musst die Spiele so durchgehen, dass die Platzierung kontrolliert wechseln, dass jedes Team auf den Plätzen spielt. Hier ist ...

Geschrieben von Nico_Schubert am 14.05.2024 07:17:28
Forum: PHP Developer Forum
Bild drehen

Hallo, Du hast den Drehwinkel und den Pfad zum Bild bereits richtig im Post-Array. Hier ist ein vollständiges Beispiel, wie du das Bild drehst u ...

Geschrieben von Nico_Schubert am 14.05.2024 06:55:15
Forum: PHP Developer Forum
Berechnungen durchführen

Hallo Matze, gerne helfe ich dir bei deinem Projekt. Lass uns deine Fragen Schritt für Schritt angehen: Erste Frage: Ist das Konstrukt so richt ...

Geschrieben von Nico_Schubert am 14.05.2024 06:43:13
Forum: PHP Developer Forum
Bild drehen

Ich denke, dass die Aussage von scatello richtig ist und stimme ihm zu.

Geschrieben von LucyCoffee am 10.05.2024 12:04:18
Forum: PHP Developer Forum