Skip to content

DOCS-7589: Enable JavaScript Protection by default #2766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions source/includes/options-mongo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,56 @@ description: |
.. versionadded:: 3.2

Disables use of the JavaScript engine's JIT compiler.
---
program: mongo
name: disableJavaScriptProtection
directive: option
optional: true
args: null
description: |
.. versionadded:: 3.4

Allows fields of type :ref:`javascript<bson-types>` and
:ref:`javascriptWithScope<bson-types>` to be automatically
marshalled to JavaScript functions in the :program:`mongo`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we marshalling the whole documents to JavaScript functions? Because that's what this sentence states.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's only the fields of type javascript and javascriptWScope which are marshalled to the shell. I've pushed a new version of this branch with updated wording.

shell.

With the ``--disableJavaScriptProtection`` flag set, it is possible
to immediately execute JavaScript functions contained in documents.
The following example demonstrates this behavior within the shell:

.. code-block:: javascript

> db.test.insert({ _id: 1, jsFunc: function(){ print("hello") } } )
WriteResult({ "nInserted" : 1 })
> var doc = db.test.findOne({ _id: 1 })
> doc
{ "_id" : 1, "jsFunc" : function (){ print ("hello") } }
> typeof doc.jsFunc
function
> doc.jsFunc()
hello

The default behavior (when :program:`mongo` starts *without* the
``--disableJavaScriptProtection`` flag) is to convert embedded
JavaScript functions to the non-executable MongoDB shell type
``Code``. The following example demonstrates the default behavior
within the shell:

.. code-block:: javascript

> db.test.insert({ _id: 1, jsFunc: function(){ print("hello") } } )
WriteResult({ "nInserted" : 1 })
> var doc = db.test.findOne({ _id: 1 })
> doc
{ "_id" : 1, "jsFunc" : { "code" : "function (){print(\"hello\")}" } }
> typeof doc.func
object
> doc.func instanceof Code
true
> doc.jsFunc()
2016-11-09T12:30:36.808-0800 E QUERY [thread1] TypeError: doc.jsFunc is
not a function :
@(shell):1:1

...
2 changes: 2 additions & 0 deletions source/reference/program/mongo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Core Options

.. include:: /includes/option/option-mongo-disableJavaScriptJIT.rst

.. include:: /includes/option/option-mongo-disableJavaScriptProtection.rst

.. _mongo-shell-file:

.. include:: /includes/option/option-mongo-<file.js>.rst
Expand Down