-
Notifications
You must be signed in to change notification settings - Fork 788
/
Copy pathsecurity.xml
182 lines (160 loc) · 5.82 KB
/
security.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="mongodb.security" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Security</title>
<section xml:id="mongodb.security.request_injection">
<title>Request Injection Attacks</title>
<para>
If you are passing <literal>$_GET</literal> (or <literal>$_POST</literal>)
parameters to your queries, make sure that they are cast to strings first.
Users can insert associative arrays in GET and POST requests, which could
then become unwanted $-queries.
</para>
<para>
A fairly innocuous example: suppose you are looking up a user's information
with the request <emphasis>http://www.example.com?username=bob</emphasis>.
Your application creates the query
<literal>$q = new \MongoDB\Driver\Query( [ 'username' => $_GET['username'] ])</literal>.
</para>
<para>
Someone could subvert this by getting
<emphasis>http://www.example.com?username[$ne]=foo</emphasis>, which PHP
will magically turn into an associative array, turning your query into
<literal>$q = new \MongoDB\Driver\Query( [ 'username' => [ '$ne' => 'foo' ] ] )</literal>,
which will return all users not named "foo" (all of your users, probably).
</para>
<para>
This is a fairly easy attack to defend against: make sure $_GET and $_POST
parameters are the type you expect before you send them to the database.
PHP has the <function>filter_var</function> function to assist with this.
</para>
<para>
Note that this type of attack can be used with any database interaction that
locates a document, including updates, upserts, deletes, and findAndModify
commands.
</para>
<para>
See <link xlink:href="&url.mongodb.dochub.security;">the main documentation</link>
for more information about SQL-injection-like issues with MongoDB.
</para>
</section>
<section xml:id="mongodb.security.script_injection">
<title>Script Injection Attacks</title>
<para>
If you are using JavaScript, make sure that any variables that cross the PHP-
to-JavaScript boundry are passed in the <literal>scope</literal> field of
<classname>MongoDB\BSON\Javascript</classname>, not interpolated into the
JavaScript string. This can come up when using <literal>$where</literal>
clauses in queries, mapReduce and group commands, and any other time you may
pass JavaScript into the database.
</para>
<para>
For example, suppose we have some JavaScript to greet a user in the database
logs. We could do:
</para>
<programlisting role="php">
<![CDATA[
<?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 );
?>
]]>
</programlisting>
<para>
However, what if a malicious user passes in some JavaScript?
</para>
<programlisting role="php">
<![CDATA[
<?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 );
?>
]]>
</programlisting>
<para>
Now MongoDB executes the JavaScript string
<literal>"print('Hello, '); db.users.drop(); print('!');"</literal>.
This attack is easy to avoid: use <literal>args</literal> to pass
variables from PHP to JavaScript:
</para>
<programlisting role="php">
<![CDATA[
<?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 );
?>
]]>
</programlisting>
<para>
This adds an argument to the JavaScript scope, which gets used as argument
for the <literal>greet</literal> function. Now if
someone tries to send malicious code, MongoDB will harmlessly print
<literal>Hello, '); db.dropDatabase(); print('!</literal>.
</para>
<para>
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
<emphasis>any</emphasis> JavaScript on the server in the first place.
</para>
<para>
You are strongly recommended to stay clear of the <link
xlink:href="&url.mongodb.docs;reference/operator/query/where/#considerations">$where
clause</link> with queries, as it impacts performance significantly. Where
possible, use either normal query operators, or the <link
xlink:href="&url.mongodb.docs;core/aggregation-pipeline">Aggregation
Framework</link>.
</para>
<para>
As alternative to <link
xlink:href="&url.mongodb.dochub.mapreduce;">MapReduce</link>, which uses
JavaScript, consider using the <link
xlink:href="&url.mongodb.docs;core/aggregation-pipeline">Aggregation
Framework</link>. 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.
</para>
<para>
The <link
xlink:href="&url.mongodb.docs;reference/command/eval/">eval command</link>
has been deprecated since MongoDB 3.0, and should also be avoided.
</para>
</section>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->