Skip to content

fixed some strict mode issues #999

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
this();
char c;
String key;
Object obj;

boolean isInitial = x.getPrevious() == 0;

Expand All @@ -230,7 +231,20 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
}
return;
default:
key = x.nextSimpleValue(c).toString();
obj = x.nextSimpleValue(c);
key = obj.toString();
}

if (jsonParserConfiguration != null && jsonParserConfiguration.isStrictMode()) {
if(obj instanceof Boolean) {
throw x.syntaxError(String.format("Strict mode error: key '%s' cannot be boolean", key));
}
if(obj == JSONObject.NULL) {
throw x.syntaxError(String.format("Strict mode error: key '%s' cannot be null", key));
}
if(obj instanceof Number) {
throw x.syntaxError(String.format("Strict mode error: key '%s' cannot be number", key));
}
}

// The key is followed by ':'.
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/org/json/JSONTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,21 @@ Object nextSimpleValue(char c) {
throw this.syntaxError("Missing value");
}
Object obj = JSONObject.stringToValue(string);
// Strict mode only allows strings with explicit double quotes
// if obj is a boolean, look at string
if (jsonParserConfiguration != null &&
jsonParserConfiguration.isStrictMode() &&
obj instanceof String) {
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not surrounded by quotes", obj));
jsonParserConfiguration.isStrictMode()) {
if (obj instanceof Boolean && !"true".equals(string) && !"false".equals(string)) {
// Strict mode only allows lowercase true or false
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not lowercase boolean", obj));
}
else if (obj == JSONObject.NULL && !"null".equals(string)) {
// Strint mode only allows lowercase null
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not lowercase null", obj));
}
else if (obj instanceof String) {
// Strict mode only allows strings with explicit double quotes
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not surrounded by quotes", obj));
}
}
return obj;
}
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3997,6 +3997,45 @@ public void testStrictModeJSONTokener_expectException(){
assertThrows(JSONException.class, () -> { new JSONObject(tokener); });
}

@Test
public void test_strictModeWithMisCasedBooleanOrNullValue(){
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();

try{
JSONObject j1 = new JSONObject("{\"a\":True}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }
try{
JSONObject j2 = new JSONObject("{\"a\":TRUE}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }
try{
JSONObject j2 = new JSONObject("{\"a\":nUlL}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }
}

@Test
public void test_strictModeWithInappropriateKey(){
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();

// Parsing the following objects should fail
try{
JSONObject j3 = new JSONObject("{true : 3}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }
try{
JSONObject j4 = new JSONObject("{TRUE : 3}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }
try{
JSONObject j5 = new JSONObject("{1 : 3}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }

}


/**
* Method to build nested map of max maxDepth
*
Expand Down