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
183
184
185
186
187
188
189
190
191
192
193
|
defmodule ViewSandboxingTest do
use CouchTestCase
@moduletag kind: :single_node
@document %{integer: 1, string: "1", array: [1, 2, 3]}
@tag :with_db
test "attempting to change the document has no effect", context do
db_name = context[:db_name]
{:ok, _} = create_doc(db_name, @document)
map_fun = """
function(doc) {
doc.integer = 2;
emit(null, doc);
}
"""
resp = query(db_name, map_fun, nil, %{include_docs: true})
rows = resp["rows"]
# either we have an error or our doc is unchanged
assert resp["total_rows"] == 0 or Enum.at(rows, 0)["doc"]["integer"] == 1
map_fun = """
function(doc) {
doc.array[0] = 0;
emit(null, doc);
}
"""
resp = query(db_name, map_fun, nil, %{include_docs: true})
row = Enum.at(resp["rows"], 0)
# either we have an error or our doc is unchanged
assert resp["total_rows"] == 0 or Enum.at(row["doc"]["array"], 0) == 1
end
@tag :with_db
test "view cannot invoke interpreter internals", context do
db_name = context[:db_name]
{:ok, _} = create_doc(db_name, @document)
map_fun = """
function(doc) {
gc();
emit(null, doc);
}
"""
# make sure that a view cannot invoke interpreter internals such as the
# garbage collector
resp = query(db_name, map_fun)
assert resp["total_rows"] == 0
end
@tag :with_db
test "view cannot access the map_funs and map_results array", context do
db_name = context[:db_name]
{:ok, _} = create_doc(db_name, @document)
map_fun = """
function(doc) {
map_funs.push(1);
emit(null, doc);
}
"""
resp = query(db_name, map_fun)
assert resp["total_rows"] == 0
map_fun = """
function(doc) {
map_results.push(1);
emit(null, doc);
}
"""
resp = query(db_name, map_fun)
assert resp["total_rows"] == 0
end
@tag :with_db
test "COUCHDB-925 - altering 'doc' variable in map function affects other map functions",
context do
db_name = context[:db_name]
ddoc = %{
_id: "_design/foobar",
language: "javascript",
views: %{
view1: %{
map: """
function(doc) {
if (doc.values) {
doc.values = [666];
}
if (doc.tags) {
doc.tags.push("qwerty");
}
if (doc.tokens) {
doc.tokens["c"] = 3;
}
}
"""
},
view2: %{
map: """
function(doc) {
if (doc.values) {
emit(doc._id, doc.values);
}
if (doc.tags) {
emit(doc._id, doc.tags);
}
if (doc.tokens) {
emit(doc._id, doc.tokens);
}
}
"""
}
}
}
doc1 = %{
_id: "doc1",
values: [1, 2, 3]
}
doc2 = %{
_id: "doc2",
tags: ["foo", "bar"],
tokens: %{a: 1, b: 2}
}
{:ok, _} = create_doc(db_name, ddoc)
{:ok, _} = create_doc(db_name, doc1)
{:ok, _} = create_doc(db_name, doc2)
resp1 = view(db_name, "foobar/view1")
resp2 = view(db_name, "foobar/view2")
assert Enum.empty?(resp1.body["rows"])
assert length(resp2.body["rows"]) == 3
assert doc1[:_id] == Enum.at(resp2.body["rows"], 0)["key"]
assert doc2[:_id] == Enum.at(resp2.body["rows"], 1)["key"]
assert doc2[:_id] == Enum.at(resp2.body["rows"], 2)["key"]
assert length(Enum.at(resp2.body["rows"], 0)["value"]) == 3
row0_values = Enum.at(resp2.body["rows"], 0)["value"]
assert Enum.at(row0_values, 0) == 1
assert Enum.at(row0_values, 1) == 2
assert Enum.at(row0_values, 2) == 3
row1_values = Enum.at(resp2.body["rows"], 1)["value"]
row2_values = Enum.at(resp2.body["rows"], 2)["value"]
# we can't be 100% sure about the order for the same key
assert (is_map(row1_values) and row1_values["a"] == 1) or
(is_list(row1_values) and Enum.at(row1_values, 0) == "foo")
assert (is_map(row1_values) and row1_values["b"] == 2) or
(is_list(row1_values) and Enum.at(row1_values, 1) == "bar")
assert (is_map(row2_values) and row2_values["a"] == 1) or
(is_list(row2_values) and Enum.at(row2_values, 0) == "foo")
assert (is_map(row2_values) and row2_values["b"] == 2) or
(is_list(row2_values) and Enum.at(row2_values, 1) == "bar")
assert is_list(row1_values) or !Map.has_key?(row1_values, "c")
assert is_list(row2_values) or !Map.has_key?(row2_values, "c")
end
@tag :with_db
test "runtime code evaluation can be prevented", context do
db_name = context[:db_name]
{:ok, _} = create_doc(db_name, @document)
map_fun = """
function(doc) {
var glob = emit.constructor('return this')();
emit(doc._id, null);
}
"""
resp = query(db_name, map_fun)
assert resp["total_rows"] == 0
end
end
|