Skip to content
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

fix: core config validation #894

Merged
merged 10 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [7.0.16] - 2023-12-04

- Returns 400, instead of 500, for badly typed core config while creating CUD, App or Tenant

## [7.0.15] - 2023-11-28

- Adds test for user pagination from old version
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ compileTestJava { options.encoding = "UTF-8" }
// }
//}

version = "7.0.15"
version = "7.0.16"


repositories {
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/io/supertokens/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.supertokens.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -57,9 +58,16 @@ private Config(Main main, String configFilePath) throws InvalidConfigException,
private Config(Main main, JsonObject jsonConfig) throws IOException, InvalidConfigException {
this.main = main;
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
CoreConfig config = mapper.readValue(jsonConfig.toString(), CoreConfig.class);
config.normalizeAndValidate(main);
this.core = config;
try {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
CoreConfig config = mapper.readValue(jsonConfig.toString(), CoreConfig.class);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
config.normalizeAndValidate(main);
this.core = config;
} catch (InvalidFormatException e) {
throw new InvalidConfigException(
"Cannot set value " + e.getValue().toString() + " for field " + e.getPath().get(0).getFieldName()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add ' around the config name and value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the message to Invalid core config: 'access_token_validity' must be of type long

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

+ " of type " + e.getTargetType().getSimpleName()
);
}
}

public static Config getInstance(TenantIdentifier tenantIdentifier, Main main)
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/io/supertokens/test/multitenant/api/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,61 @@ public void testDefaultRecipesEnabledWhileCreatingApp() throws Exception {
assertTrue(tenant.get("thirdParty").getAsJsonObject().get("enabled").getAsBoolean());
assertTrue(tenant.get("passwordless").getAsJsonObject().get("enabled").getAsBoolean());
}

@Test
public void testInvalidTypedValueInCoreConfigWhileCreatingApp() throws Exception {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

if (StorageLayer.isInMemDb(process.getProcess())) {
return;
}

String[] properties = new String[]{
"access_token_validity", // long
"disable_telemetry", // boolean
"postgresql_connection_pool_size", // int
"mysql_connection_pool_size", // int
};
Object[] values = new Object[]{
"abcd", // access_token_validity
"abcd", // disable_telemetry
"abcd", // postgresql_connection_pool_size
"abcd", // mysql_connection_pool_size
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirm that the same message type is shown when refresh token is < access token.

System.out.println(StorageLayer.getStorage(process.getProcess()).getClass().getCanonicalName());

for (int i = 0; i < properties.length; i++) {
try {
System.out.println("Test case " + i);
JsonObject config = new JsonObject();
if (values[i] instanceof String) {
config.addProperty(properties[i], (String) values[i]);
} else if (values[i] instanceof Boolean) {
config.addProperty(properties[i], (Boolean) values[i]);
} else if (values[i] instanceof Number) {
config.addProperty(properties[i], (Number) values[i]);
} else {
throw new RuntimeException("Invalid type");
}
StorageLayer.getBaseStorage(process.getProcess()).modifyConfigToAddANewUserPoolForTesting(config, 1);

JsonObject response = TestMultitenancyAPIHelper.createApp(
process.getProcess(),
new TenantIdentifier(null, null, null),
"a1", null, null, null,
config);
fail();
} catch (HttpResponseException e) {
System.out.println(e.getMessage());
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(400, e.statusCode);
if (!e.getMessage().contains("Invalid config key")) {
assertTrue(e.getMessage().contains("Cannot set value"));
assertTrue(e.getMessage().contains("for field " + properties[i]));
}
}
}
}
}
Loading