-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TestWhiteListJavaWrapper.java
135 lines (108 loc) · 4.37 KB
/
TestWhiteListJavaWrapper.java
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
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.scripting.javascript;
import org.junit.After;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeJavaMethod;
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.UniqueTag;
import java.util.HashSet;
import java.util.Set;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.server.scripting.UserScriptClassWhiteList;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class TestWhiteListJavaWrapper
{
@After
public void exitContext()
{
try
{
Context.exit();
}
catch ( IllegalStateException e )
{
// Om nom nom
}
}
@Test( expected = SecurityException.class )
public void shouldBlockAttemptsAtAccessingClassLoader() throws Exception
{
// Given
WhiteListJavaWrapper wrapper = new WhiteListJavaWrapper( new WhiteListClassShutter( new HashSet<String>() ) );
// When
wrapper.wrap( null, null, getClass().getClassLoader(), null );
}
@Test
public void shouldDownCastSubclassesToAllowedParentClass() throws Exception
{
// Given
Set<String> whiteList = new HashSet<String>( );
whiteList.add( Object.class.getName() );
WhiteListJavaWrapper wrapper = new WhiteListJavaWrapper( new WhiteListClassShutter( whiteList ) );
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
// When
Object wrapped = wrapper.wrap( cx, scope, new TestWhiteListJavaWrapper(), null );
// Then
assertThat( wrapped, is( instanceOf( NativeJavaObject.class ) ) );
NativeJavaObject obj = (NativeJavaObject)wrapped;
assertThat( obj.has( "aGetter", scope ), is( false ));
assertThat( obj.get( "aGetter", scope ), is( UniqueTag.NOT_FOUND ) );
}
@Test( expected = SecurityException.class )
public void shouldThrowSecurityExceptionWhenAccessingLockedClasses() throws Exception
{
// Given
Set<String> whiteList = new HashSet<String>( );
whiteList.add( Object.class.getName() );
WhiteListJavaWrapper wrapper = new WhiteListJavaWrapper( new WhiteListClassShutter( whiteList ) );
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
// When
Object wrapped = wrapper.wrap( cx, scope, TestWhiteListJavaWrapper.class, null );
}
@Test
public void shouldAllowAccessToWhiteListedClassMembers() throws Exception
{
// XXX: The Rhino security stuff can only be set globally, unfortunately. That means
// that we need to use a class here that is white-listed in the "real" white list, because
// other tests will already have configured global security settings that we cannot override.
// Given
WhiteListJavaWrapper wrapper =
new WhiteListJavaWrapper( new WhiteListClassShutter( UserScriptClassWhiteList.getWhiteList() ) );
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
// When
Object wrapped = wrapper.wrap( cx, scope, RelationshipType.withName( "blah" ), null );
// Then
assertThat( wrapped, is( instanceOf( NativeJavaObject.class ) ) );
NativeJavaObject obj = (NativeJavaObject)wrapped;
assertThat( obj.get( "name", scope ),
is( instanceOf( NativeJavaMethod.class ) ) );
}
public void aGetter()
{
}
}