Skip to content

Commit acf5a07

Browse files
authored
Merge pull request #357 from lutovich/1.3-routing-ctx-parsing
Prohibit empty keys in routing context
2 parents 71acb4b + 598acc5 commit acf5a07

File tree

2 files changed

+63
-27
lines changed

2 files changed

+63
-27
lines changed

driver/src/main/java/org/neo4j/driver/internal/cluster/RoutingContext.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public Map<String,String> asMap()
5151
return context;
5252
}
5353

54+
@Override
55+
public String toString()
56+
{
57+
return "RoutingContext" + context;
58+
}
59+
5460
private static Map<String,String> parseParameters( URI uri )
5561
{
5662
String query = uri.getQuery();
@@ -71,8 +77,12 @@ private static Map<String,String> parseParameters( URI uri )
7177
"Invalid parameters: '" + pair + "' in URI '" + uri + "'" );
7278
}
7379

74-
String key = keyValue[0];
75-
String value = keyValue[1];
80+
String key = trimAndVerify( keyValue[0], "key", uri );
81+
String value = trimAndVerify( keyValue[1], "value", uri );
82+
if ( value.isEmpty() )
83+
{
84+
throw new IllegalArgumentException( "Illegal empty value in URI query '" + uri + "'" );
85+
}
7686
String previousValue = parameters.put( key, value );
7787

7888
if ( previousValue != null )
@@ -83,4 +93,14 @@ private static Map<String,String> parseParameters( URI uri )
8393
}
8494
return parameters;
8595
}
96+
97+
private static String trimAndVerify( String string, String name, URI uri )
98+
{
99+
String result = string.trim();
100+
if ( result.isEmpty() )
101+
{
102+
throw new IllegalArgumentException( "Illegal empty " + name + " in URI query '" + uri + "'" );
103+
}
104+
return result;
105+
}
86106
}

driver/src/test/java/org/neo4j/driver/internal/cluster/RoutingContextTest.java

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ public void emptyContextInEmptyMap()
4949
@Test
5050
public void uriWithoutQueryIsParsedToEmptyContext()
5151
{
52-
URI uri = URI.create( "bolt+routing://localhost:7687/" );
53-
RoutingContext context = new RoutingContext( uri );
52+
testEmptyRoutingContext( URI.create( "bolt+routing://localhost:7687/" ) );
53+
}
5454

55-
assertFalse( context.isDefined() );
56-
assertTrue( context.asMap().isEmpty() );
55+
@Test
56+
public void uriWithEmptyQueryIsParsedToEmptyContext()
57+
{
58+
testEmptyRoutingContext( URI.create( "bolt+routing://localhost:7687?" ) );
59+
testEmptyRoutingContext( URI.create( "bolt+routing://localhost:7687/?" ) );
5760
}
5861

5962
@Test
@@ -73,33 +76,25 @@ public void uriWithQueryIsParsed()
7376
@Test
7477
public void throwsForInvalidUriQuery()
7578
{
76-
URI uri = URI.create( "bolt+routing://localhost:7687/?justKey" );
79+
testIllegalUri( URI.create( "bolt+routing://localhost:7687/?justKey" ) );
80+
}
7781

78-
try
79-
{
80-
new RoutingContext( uri );
81-
fail( "Exception expected" );
82-
}
83-
catch ( Exception e )
84-
{
85-
assertThat( e, instanceOf( IllegalArgumentException.class ) );
86-
}
82+
@Test
83+
public void throwsForInvalidUriQueryKey()
84+
{
85+
testIllegalUri( URI.create( "bolt+routing://localhost:7687/?=value1&key2=value2" ) );
8786
}
8887

8988
@Test
90-
public void throwsForDuplicatedUriQueryParameters()
89+
public void throwsForInvalidUriQueryValue()
9190
{
92-
URI uri = URI.create( "bolt+routing://localhost:7687/?key1=value1&key2=value2&key1=value2" );
91+
testIllegalUri( URI.create( "bolt+routing://localhost:7687/key1?=value1&key2=" ) );
92+
}
9393

94-
try
95-
{
96-
new RoutingContext( uri );
97-
fail( "Exception expected" );
98-
}
99-
catch ( Exception e )
100-
{
101-
assertThat( e, instanceOf( IllegalArgumentException.class ) );
102-
}
94+
@Test
95+
public void throwsForDuplicatedUriQueryParameters()
96+
{
97+
testIllegalUri( URI.create( "bolt+routing://localhost:7687/?key1=value1&key2=value2&key1=value2" ) );
10398
}
10499

105100
@Test
@@ -122,4 +117,25 @@ public void mapRepresentationIsUnmodifiable()
122117

123118
assertEquals( singletonMap( "key1", "value1" ), context.asMap() );
124119
}
120+
121+
private static void testIllegalUri( URI uri )
122+
{
123+
try
124+
{
125+
new RoutingContext( uri );
126+
fail( "Exception expected" );
127+
}
128+
catch ( Exception e )
129+
{
130+
assertThat( e, instanceOf( IllegalArgumentException.class ) );
131+
}
132+
}
133+
134+
private static void testEmptyRoutingContext( URI uri )
135+
{
136+
RoutingContext context = new RoutingContext( uri );
137+
138+
assertFalse( context.isDefined() );
139+
assertTrue( context.asMap().isEmpty() );
140+
}
125141
}

0 commit comments

Comments
 (0)