summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/js_protection.js
blob: eda42395cd96787a3fc730a3f0c15183d5be4349 (plain)
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
/**
 * Positive tests for the behavior of --enableJavaScriptProtection (the flag).
 *
 * Ensure that:
 * 1. Simple functions stored in documents are not automatically marshalled
 *    when the flag is on in the shell.
 * 2. $where is unable to use stored functions when the flag is set on the
 *    server.
 * 3. db.loadServerScripts performs as expected even with the flag is set in
 *    the shell.
 */

(function() {
    "use strict";

    var testServer = MongoRunner.runMongod({setParameter: "javascriptProtection=true"});
    assert.neq(
        null, testServer, "failed to start mongod with --setParameter=javascriptProtection=true");

    var db = testServer.getDB("test");
    var t = db.js_protection;

    function assertMongoClientCorrect() {
        var functionToEval = function() {
            var doc = db.js_protection.findOne({_id: 0});
            assert.neq(null, doc);
            assert(doc.hasOwnProperty("myFunc"));
            assert.neq("function",
                       typeof doc.myFunc,
                       "value of BSON type Code shouldn't have been eval()ed automatically");

            assert.eq("undefined", typeof addOne, "addOne function has already been defined");
            db.loadServerScripts();
            assert.neq(
                "undefined", typeof addOne, "addOne function should have been eval()ed locally");
            assert.eq(5, addOne(4));
        };

        var exitCode = runMongoProgram("mongo",
                                       "--port",
                                       testServer.port,
                                       "--enableJavaScriptProtection",
                                       "--eval",
                                       "(" + functionToEval.toString() + ")();");
        assert.eq(0, exitCode);
    }

    function assertNoStoredWhere() {
        t.insertOne({name: "testdoc", val: 0, y: 0});

        var res = t.update({$where: "addOne(this.val) === 1"}, {$set: {y: 100}}, false, true);
        assert.writeError(res);

        var doc = t.findOne({name: "testdoc"});
        assert.neq(null, doc);
        assert.eq(0, doc.y, tojson(doc));

        res = t.update({
            $where: function() {
                return this.val === 0;
            }
        },
                       {$set: {y: 100}},
                       false,
                       true);
        assert.writeOK(res);

        doc = t.findOne({name: "testdoc"});
        assert.neq(null, doc);
        assert.eq(100, doc.y, tojson(doc));
    }

    /**
     *  ACTUAL TEST
     */

    db.system.js.insertOne({
        _id: "addOne",
        value: function(x) {
            return x + 1;
        }
    });

    t.insertOne({
        _id: 0,
        myFunc: function() {
            return "testval";
        }
    });

    assertMongoClientCorrect();
    assertNoStoredWhere();

    MongoRunner.stopMongod(testServer);
})();