Skip to content

Commit 44cbf18

Browse files
(DOCSP-7954): Whitelist filter fields for unauthenticated charts (#276)
* (DOCSP-7954): Whitelist filter fields for unauthenticated charts * Updates per review * Updates per review pt 2
1 parent e692340 commit 44cbf18

File tree

6 files changed

+229
-60
lines changed

6 files changed

+229
-60
lines changed

source/dashboard-permissions.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ following dashboard permissions are available:
3535
``Viewers`` cannot make any modifications to the dashboard.
3636

3737
* - Author
38-
- Can add, modify, or delete charts, change dashboard layout, and
39-
rename the dashboard.
38+
- Can perform the following actions:
39+
40+
- Add, modify, and delete charts,
41+
- Specify which fields can be used for
42+
:ref:`Unauthenticated embedded chart filters
43+
<specify-filter-fields>`,
44+
- Change dashboard layout,
45+
- Rename the dashboard.
4046

4147
* - Owner
4248
- Has all privileges of ``Author`` and can also

source/embedded-chart-options.txt

Lines changed: 173 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,77 +21,197 @@ query parameters to their iframe URLs.
2121

2222
.. _embed-options-filter:
2323

24-
Specify a Filter
25-
----------------
24+
Filter Data on Embedded Charts
25+
------------------------------
2626

2727
Use the ``filter`` query parameter to only display data in your
28-
embedded chart which matches a specified :abbr:`MQL (MongoDB Query Language)`
29-
filter.
28+
embedded chart which matches a specified
29+
:abbr:`MQL (MongoDB Query Language)` filter.
3030

31-
.. important::
31+
You can use the ``filter`` query parameter on both Unauthenticated
32+
charts and charts which require a Verified Signature. The filtering
33+
behavior differs with each authentication setting:
3234

33-
For security reasons, the ``filter`` query parameter can only be used
34-
on charts embedded with a
35-
:ref:`Verified Signature <embedding-procedure>`. You should generate
36-
your ``filter`` in your server-side code, rather than receiving
37-
it from the client. Generating the filter from the server
38-
prevents the client from modifying the filter and potentially
39-
accessing restricted data.
35+
- With Unauthenticated charts, the chart
36+
:ref:`Author <dashboard-roles>` specifies the fields that can be
37+
included in filters set by the embedding application code or added by
38+
chart viewers. To learn how to specify filterable fields, see
39+
:ref:`Specify Filterable Fields <specify-filter-fields>`.
4040

41-
Syntax
42-
~~~~~~
41+
- With charts which require a Verified Signature, all document
42+
fields can be filtered upon, however you must generate
43+
the filter in the server-side code and include the filter as part of
44+
your signed payload.
4345

44-
Specify an MQL document as your ``filter`` query parameter. Use the
45-
same syntax used in the :ref:`query bar <query-bar>` in the Chart
46-
Builder.
46+
.. _specify-filter-fields:
4747

48-
MQL queries contain characters that must be URL-encoded before
49-
your server-side code calculates the verified signature.
50-
When |charts-short| verifies the signature, it URL-encodes the filter
51-
again using the JavaScript
52-
`encodeURIComponent
53-
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent>`__
54-
function. You must use the same encoding algorithm to encode your
55-
filter.
48+
Specify Filterable Fields (Unauthenticated Charts Only)
49+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5650

57-
.. important::
51+
A chart :ref:`Author <dashboard-roles>` specifies the fields that can be
52+
included in filters set by the embedding application code or added by
53+
chart viewers. Use these fields to restrict the data that viewers can
54+
access. By default, no fields are whitelisted, meaning the chart cannot
55+
be filtered until you explicitly whitelist at least one field.
5856

59-
You must encode spaces in your filter as ``%20``, rather than ``+``
60-
or a raw space.
57+
To define filterable fields:
6158

62-
To see how correctly encode an MQL filter using different server-side
63-
programming languages, see `MongoDB Charts Embedding Examples
64-
<https://github.com/mongodb/charts-embedding-examples>`__ on GitHub.
59+
1. Navigate to the :ref:`dashboard <dashboards>` that contains the chart
60+
where you wish to define filterable fields.
6561

66-
Example
67-
~~~~~~~
62+
#. For the desired chart, click the :icon-fa5:`ellipsis-h` button and
63+
select :guilabel:`Embed Chart` from the dropdown.
64+
65+
#. In the :guilabel:`User Specified Filters` section, use the
66+
dropdown to select which fields chart viewers can use filter data
67+
in the chart. You can also manually type values to add fields not
68+
listed in the dropdown.
69+
70+
.. note::
71+
72+
This option only appears if you already have
73+
:guilabel:`Unauthenticated` embedding access enabled.
74+
75+
#. When you have selected all desired fields, click :guilabel:`Save`
76+
below the dropdown.
77+
78+
Chart :ref:`viewers <dashboard-roles>` and applications which render
79+
the chart can now specify filters based on the whitelisted fields to
80+
display subsets of the original chart data. If a viewer attempts to
81+
specify a filter using a field not in the whitelist, an
82+
:ref:`error <embedded-errors>` is returned.
83+
84+
.. topic:: Filters for Embedded Documents
85+
86+
When you whitelist a field whose value is an embedded document, you
87+
must specify each individual sub-field you wish to add to the
88+
whitelist.
89+
90+
.. example::
91+
92+
Consider the following document:
93+
94+
.. code-block:: json
95+
96+
{
97+
"name": "Alice",
98+
"favorites" :
99+
{
100+
"color": "green",
101+
"animal": "turtle",
102+
"season": "autumn"
103+
}
104+
}
105+
106+
If you only add the ``favorites`` field to the whitelist, it does
107+
*not* grant viewers permission to filter upon any of the sub-fields
108+
of ``favorites``. Instead, you may add one or more of the sub-fields
109+
individually to the whitelist by specifying ``favorites.color``,
110+
``favorites.animal``, or ``favorites.season``.
111+
112+
113+
114+
Filter Syntax
115+
~~~~~~~~~~~~~
116+
117+
Select the appropriate tab to see an example of how to filter data in
118+
an Unauthenticated chart and a Verified Signature chart:
119+
120+
.. tabs::
121+
122+
tabs:
123+
- id: unauthenticated
124+
name: Unauthenticated External Sharing
125+
content: |
126+
127+
You can specify an MQL document as your ``filter`` query
128+
parameter provided that the fields used in your filter are in
129+
the
130+
:ref:`whitelist of filterable fields <specify-filter-fields>`.
131+
132+
.. include:: /includes/fact-embedded-filter-examples-unauth.rst
133+
134+
.. note::
135+
136+
You must URL-encode special characters of the filter
137+
parameter.
138+
139+
Example
140+
```````
141+
142+
The following iframe ``src`` URL renders a chart which only
143+
displays documents with an ``imdb.rating`` greater than or
144+
equal to ``8``:
145+
146+
.. code-block:: none
147+
:emphasize-lines: 4
148+
149+
https://charts.mongodb.com/charts-atlasproject1-piocy/embed/charts?
150+
id=93584ddb-1115-4a12-afd9-5129e47bbb0d&
151+
tenant=3397ee6d-5079-4a20-b097-cedd475220b5&
152+
filter={"imdb.rating":%20{$gte:%208}}&
153+
autorefresh=30
154+
155+
The URL uses an encoded ``filter`` parameter of
156+
``{"imdb.rating":%20{$gte:%208}}``. Decoded,
157+
this filter is:
158+
159+
.. code-block:: json
160+
161+
{"imdb.rating": {$gte: 8}}
162+
163+
- id: verified-signature
164+
name: Verified Signature Required
165+
content: |
166+
167+
Specify an MQL document as your ``filter`` query parameter.
168+
169+
.. include:: /includes/fact-embedded-filter-examples-verified.rst
170+
171+
When using filters in a Verified Signature, MQL queries contain
172+
characters that must be URL-encoded before your server-side
173+
code calculates the signature. When |charts-short| verifies the
174+
signature, it URL-encodes the filter again using the JavaScript
175+
`encodeURIComponent
176+
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent>`__
177+
function. You must use the same encoding algorithm to encode
178+
your filter.
179+
180+
.. important::
181+
182+
You must encode spaces in your filter as ``%20``, rather than ``+``
183+
or a raw space.
184+
185+
To see how correctly encode an MQL filter using different server-side
186+
programming languages, see `MongoDB Charts Embedding Examples
187+
<https://github.com/mongodb/charts-embedding-examples>`__ on GitHub.
68188

69-
The following iframe ``src`` URL renders a chart which only displays
70-
documents with an ``imdb.rating`` greater than or equal to ``8``:
189+
Example
190+
```````
71191

72-
.. code-block:: none
73-
:emphasize-lines: 6
192+
The following iframe ``src`` URL renders a chart which only
193+
displays documents with an ``imdb.rating`` greater than or
194+
equal to ``8``:
74195

75-
https://charts.mongodb.com/charts-atlasproject1-piocy/embed/charts?
76-
id=93584ddb-1115-4a12-afd9-5129e47bbb0d&
77-
tenant=3397ee6d-5079-4a20-b097-cedd475220b5&
78-
timestamp=1564156636&
79-
expires-in=300&
80-
filter=%7B%22imdb.rating%22%3A%20%7B%24gte%3A%208%7D%7D&
81-
autorefresh=30&
82-
signature=8e0d92b33934c928f6c6974e2f0102ace77f56d851cb0d33893e84c359ab1043
196+
.. code-block:: none
197+
:emphasize-lines: 6
83198

84-
The URL uses an encoded ``filter`` parameter of
85-
``%7B%22imdb.rating%22%3A%20%7B%24gte%3A%208%7D%7D``. Decoded, this
86-
filter is:
199+
https://charts.mongodb.com/charts-atlasproject1-piocy/embed/charts?
200+
id=93584ddb-1115-4a12-afd9-5129e47bbb0d&
201+
tenant=3397ee6d-5079-4a20-b097-cedd475220b5&
202+
timestamp=1564156636&
203+
expires-in=300&
204+
filter=%7B%22imdb.rating%22%3A%20%7B%24gte%3A%208%7D%7D&
205+
autorefresh=30&
206+
signature=8e0d92b33934c928f6c6974e2f0102ace77f56d851cb0d33893e84c359ab1043
87207

88-
.. code-block:: json
208+
The URL uses an encoded ``filter`` parameter of
209+
``%7B%22imdb.rating%22%3A%20%7B%24gte%3A%208%7D%7D``. Decoded,
210+
this filter is:
89211

90-
{"imdb.rating": {$gte: 8}}
212+
.. code-block:: json
91213

92-
The server-side code creates a payload with the necessary information
93-
to render the chart, including the filter. The payload ensures that
94-
the verified signature is valid before rendering the chart.
214+
{"imdb.rating": {$gte: 8}}
95215

96216
.. _embed-options-refresh:
97217

source/embedding-charts.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ is non-sensitive and you prefer a simpler external sharing solution, use the
2929
You can enable external sharing for your chart with the verified
3030
signature required option even if you don't currently have an
3131
:ref:`external sharing key <admin-settings>`, but you'll need a key
32-
to generate a verified signature. If you
33-
:atlas:`own </reference/user-roles#project-roles>` for your
32+
to generate a verified signature. If you are a
33+
:atlas:`Project Owner </reference/user-roles#project-roles>` of your
3434
|service| project, you can generate an external sharing key on the
3535
:ref:`Admin Settings <admin-settings>` page. Otherwise, you must
3636
contact the |service| Project Owner and ask for an external sharing
@@ -100,9 +100,15 @@ If an shared chart fails to render, an error code is displayed:
100100
value must be an integer greater than 0.
101101

102102
* - ``7``
103-
- :ref:`Filter <embed-options-filter>` not allowed. A ``filter``
104-
parameter was specified for a chart using anonymous embedding.
105-
Filters can only be used with verified signatures.
103+
- :ref:`Filter <embed-options-filter>` not allowed. When filtering
104+
an Unauthenticated embedded chart, the fields used in the filter
105+
must be present in the
106+
:ref:`filter whitelist <specify-filter-fields>`. Additionally,
107+
filters on both Unauthenticated and Verified Signature embedded
108+
charts cannot use non-logical operators before a field name
109+
(e.g., :manual:`$expr </reference/operator/query/expr/>`,
110+
:manual:`$where </reference/operator/query/where/>`, or
111+
:manual:`$text </reference/operator/query/text/>`).
106112

107113
* - ``8``
108114
- :ref:`Filter <embed-options-filter>` not valid. A ``filter``

source/images/charts/embed-chart2.png

197 KB
Loading
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Your filter must match the format used in a
2+
:manual:`$match </reference/operator/aggregation/match/>` query and
3+
be either a:
4+
5+
- Top level query
6+
7+
.. example::
8+
9+
.. code-block:: json
10+
11+
{ "quantity": { $gte: 20 } }
12+
13+
- Or within boolean expressions (
14+
:manual:`$and </reference/operator/query/and/>`,
15+
:manual:`$nor </reference/operator/query/nor/>`,
16+
:manual:`$or </reference/operator/query/or/>`)
17+
18+
.. example::
19+
20+
.. code-block:: json
21+
22+
{ $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Your filter must match the format used in a
2+
:manual:`$match </reference/operator/aggregation/match/>` query as
3+
shown in the following examples:
4+
5+
.. example::
6+
7+
.. code-block:: json
8+
9+
{ "quantity": { $gte: 20 } }
10+
11+
.. example::
12+
13+
.. code-block:: json
14+
15+
{ $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] }

0 commit comments

Comments
 (0)