18a2d737cSLawrence Tang /** 28a2d737cSLawrence Tang * A very basic, non-complete implementation of a validator for the JSON Schema specification, 38a2d737cSLawrence Tang * for validating CPER-JSON. 48a2d737cSLawrence Tang * 58a2d737cSLawrence Tang * Author: Lawrence.Tang@arm.com 68a2d737cSLawrence Tang **/ 78a2d737cSLawrence Tang 88a2d737cSLawrence Tang #include <stdio.h> 98a2d737cSLawrence Tang #include <string.h> 108a2d737cSLawrence Tang #include <unistd.h> 118a2d737cSLawrence Tang #include <libgen.h> 128a2d737cSLawrence Tang #include <limits.h> 138f793ac3SLawrence Tang #include <stdarg.h> 148a2d737cSLawrence Tang #include "json.h" 158a2d737cSLawrence Tang #include "json-schema.h" 168a2d737cSLawrence Tang #include "edk/BaseTypes.h" 178a2d737cSLawrence Tang 188f793ac3SLawrence Tang //Field definitions. 198f793ac3SLawrence Tang int json_validator_debug = 0; 208f793ac3SLawrence Tang 218a2d737cSLawrence Tang //Private pre-definitions. 228a2d737cSLawrence Tang int validate_field(const char* name, json_object* schema, json_object* object, char* error_message); 238a2d737cSLawrence Tang int validate_integer(const char* field_name, json_object* schema, json_object* object, char* error_message); 248a2d737cSLawrence Tang int validate_string(const char* field_name, json_object* schema, json_object* object, char* error_message); 258a2d737cSLawrence Tang int validate_object(const char* field_name, json_object* schema, json_object* object, char* error_message); 268a2d737cSLawrence Tang int validate_array(const char* field_name, json_object* schema, json_object* object, char* error_message); 278f793ac3SLawrence Tang void log_validator_error(char* error_message, const char* format, ...); 288f793ac3SLawrence Tang void log_validator_debug(const char* format, ...); 298f793ac3SLawrence Tang void log_validator_msg(const char* format, va_list args); 308a2d737cSLawrence Tang 318a2d737cSLawrence Tang //Validates a single JSON object against a provided schema file, returning 1 on success and 0 on failure to validate. 328a2d737cSLawrence Tang //Error message space must be allocated prior to call. 338a2d737cSLawrence Tang int validate_schema_from_file(const char* schema_file, json_object* object, char* error_message) 348a2d737cSLawrence Tang { 358a2d737cSLawrence Tang //Load schema IR from file. 368a2d737cSLawrence Tang json_object* schema_ir = json_object_from_file(schema_file); 378a2d737cSLawrence Tang if (schema_ir == NULL) 388a2d737cSLawrence Tang { 398f793ac3SLawrence Tang log_validator_error(error_message, "Failed to load schema from file '%s'.", schema_file); 408a2d737cSLawrence Tang return 0; 418a2d737cSLawrence Tang } 428a2d737cSLawrence Tang 438a2d737cSLawrence Tang //Get the directory of the file. 448a2d737cSLawrence Tang char* schema_file_copy = malloc(strlen(schema_file) + 1); 458a2d737cSLawrence Tang strcpy(schema_file_copy, schema_file); 468a2d737cSLawrence Tang char* schema_dir = dirname(schema_file_copy); 478a2d737cSLawrence Tang 488a2d737cSLawrence Tang int result = validate_schema(schema_ir, schema_dir, object, error_message); 498a2d737cSLawrence Tang 508a2d737cSLawrence Tang //Free memory from directory call. 518a2d737cSLawrence Tang free(schema_file_copy); 528a2d737cSLawrence Tang 538a2d737cSLawrence Tang return result; 548a2d737cSLawrence Tang } 558a2d737cSLawrence Tang 568a2d737cSLawrence Tang //Validates a single JSON object against a provided schema, returning 1 on success and 0 on failure to validate. 578a2d737cSLawrence Tang //Error message space must be allocated prior to call. 588a2d737cSLawrence Tang //If the schema does not include any other sub-schemas using "$ref", then leaving schema_directory as NULL is valid. 598a2d737cSLawrence Tang int validate_schema(json_object* schema, char* schema_directory, json_object* object, char* error_message) 608a2d737cSLawrence Tang { 618a2d737cSLawrence Tang //Check that the schema version is the same as this validator. 628a2d737cSLawrence Tang json_object* schema_ver = json_object_object_get(schema, "$schema"); 638a2d737cSLawrence Tang if (schema_ver == NULL || strcmp(json_object_get_string(schema_ver), JSON_SCHEMA_VERSION)) 648a2d737cSLawrence Tang { 658f793ac3SLawrence Tang log_validator_error(error_message, "Provided schema is not of the same version that is referenced by this validator, or is not a schema."); 668a2d737cSLawrence Tang return 0; 678a2d737cSLawrence Tang } 688a2d737cSLawrence Tang 698a2d737cSLawrence Tang //Change current directory into the schema directory. 708a2d737cSLawrence Tang char* original_cwd = malloc(PATH_MAX); 718a2d737cSLawrence Tang if (getcwd(original_cwd, PATH_MAX) == NULL) 728a2d737cSLawrence Tang { 738f793ac3SLawrence Tang log_validator_error(error_message, "Failed fetching the current directory."); 748a2d737cSLawrence Tang return 0; 758a2d737cSLawrence Tang } 768a2d737cSLawrence Tang if (chdir(schema_directory)) 778a2d737cSLawrence Tang { 788f793ac3SLawrence Tang log_validator_error(error_message, "Failed to chdir into schema directory."); 798a2d737cSLawrence Tang return 0; 808a2d737cSLawrence Tang } 818a2d737cSLawrence Tang 828a2d737cSLawrence Tang //Parse the top level structure appropriately. 838a2d737cSLawrence Tang int result = validate_field("parent", schema, object, error_message); 848a2d737cSLawrence Tang 858a2d737cSLawrence Tang //Change back to original CWD. 868a2d737cSLawrence Tang chdir(original_cwd); 878a2d737cSLawrence Tang free(original_cwd); 888a2d737cSLawrence Tang 898a2d737cSLawrence Tang return result; 908a2d737cSLawrence Tang } 918a2d737cSLawrence Tang 928a2d737cSLawrence Tang //Validates a single JSON field given a schema/object. 938f793ac3SLawrence Tang //Returns -1 on fatal/error failure, 0 on validation failure, and 1 on validation. 948a2d737cSLawrence Tang int validate_field(const char* field_name, json_object* schema, json_object* object, char* error_message) 958a2d737cSLawrence Tang { 968f793ac3SLawrence Tang log_validator_debug("Validating field '%s'...", field_name); 978f793ac3SLawrence Tang 988a2d737cSLawrence Tang //If there is a "$ref" field, attempt to load the referenced schema. 998a2d737cSLawrence Tang json_object* ref_schema = json_object_object_get(schema, "$ref"); 1008a2d737cSLawrence Tang if (ref_schema != NULL && json_object_get_type(ref_schema) == json_type_string) 1018a2d737cSLawrence Tang { 1028f793ac3SLawrence Tang log_validator_debug("$ref schema detected for field '%s'.", field_name); 1038f793ac3SLawrence Tang 1048a2d737cSLawrence Tang //Attempt to load. If loading fails, report error. 1058a2d737cSLawrence Tang const char* ref_path = json_object_get_string(ref_schema); 1068a2d737cSLawrence Tang schema = json_object_from_file(ref_path); 1078a2d737cSLawrence Tang if (schema == NULL) 1088a2d737cSLawrence Tang { 1098f793ac3SLawrence Tang log_validator_error(error_message, "Failed to open referenced schema file '%s'.", ref_path); 1108f793ac3SLawrence Tang return -1; 1118a2d737cSLawrence Tang } 1128f793ac3SLawrence Tang 1138f793ac3SLawrence Tang log_validator_debug("loaded schema path '%s' for field '%s'.", ref_path, field_name); 1148a2d737cSLawrence Tang } 1158a2d737cSLawrence Tang 1168a2d737cSLawrence Tang //Get the schema field type. 1178a2d737cSLawrence Tang json_object* desired_field_type = json_object_object_get(schema, "type"); 1188a2d737cSLawrence Tang if (desired_field_type == NULL || !json_object_is_type(desired_field_type, json_type_string)) 1198a2d737cSLawrence Tang { 1208f793ac3SLawrence Tang log_validator_error(error_message, "Desired field type not provided within schema/is not a string for field '%s' (schema violation).", field_name); 1218f793ac3SLawrence Tang return -1; 1228a2d737cSLawrence Tang } 1238a2d737cSLawrence Tang 1248a2d737cSLawrence Tang //Check the field types are actually equal. 1258a2d737cSLawrence Tang const char* desired_field_type_str = json_object_get_string(desired_field_type); 1268a2d737cSLawrence Tang if (!( 1278a2d737cSLawrence Tang (!strcmp(desired_field_type_str, "object") && json_object_is_type(object, json_type_object)) 1288a2d737cSLawrence Tang || (!strcmp(desired_field_type_str, "array") && json_object_is_type(object, json_type_array)) 1298a2d737cSLawrence Tang || (!strcmp(desired_field_type_str, "integer") && json_object_is_type(object, json_type_int)) 1308a2d737cSLawrence Tang || (!strcmp(desired_field_type_str, "string") && json_object_is_type(object, json_type_string)) 1318a2d737cSLawrence Tang || (!strcmp(desired_field_type_str, "boolean") && json_object_is_type(object, json_type_boolean)) 1328a2d737cSLawrence Tang || (!strcmp(desired_field_type_str, "double") && json_object_is_type(object, json_type_double)) 1338a2d737cSLawrence Tang )) 1348a2d737cSLawrence Tang { 1358f793ac3SLawrence Tang log_validator_error(error_message, "Field type match failed for field '%s'.", field_name); 1368a2d737cSLawrence Tang return 0; 1378a2d737cSLawrence Tang } 1388a2d737cSLawrence Tang 1398f793ac3SLawrence Tang //If the schema contains a "oneOf" array, we need to validate the field against each of the 1408f793ac3SLawrence Tang //possible options in turn. 1418f793ac3SLawrence Tang json_object* one_of = json_object_object_get(schema, "oneOf"); 1428f793ac3SLawrence Tang if (one_of != NULL && json_object_get_type(one_of) == json_type_array) 1438f793ac3SLawrence Tang { 1448f793ac3SLawrence Tang log_validator_debug("oneOf options detected for field '%s'.", field_name); 1458f793ac3SLawrence Tang 1468f793ac3SLawrence Tang int len = json_object_array_length(one_of); 1478f793ac3SLawrence Tang int validated = 0; 1488f793ac3SLawrence Tang for (int i=0; i<len; i++) 1498f793ac3SLawrence Tang { 1508f793ac3SLawrence Tang //If the "oneOf" member isn't an object, warn on schema violation. 1518f793ac3SLawrence Tang json_object* one_of_option = json_object_array_get_idx(one_of, i); 1528f793ac3SLawrence Tang if (one_of_option == NULL || json_object_get_type(one_of_option) != json_type_object) 1538f793ac3SLawrence Tang { 1548f793ac3SLawrence Tang log_validator_debug("Schema Warning: 'oneOf' member for field '%s' is not an object, schema violation.", field_name); 1558f793ac3SLawrence Tang continue; 1568f793ac3SLawrence Tang } 1578f793ac3SLawrence Tang 1588f793ac3SLawrence Tang //Validate field with schema. 1598f793ac3SLawrence Tang validated = validate_field(field_name, one_of_option, object, error_message); 1608f793ac3SLawrence Tang if (validated == -1) 1618f793ac3SLawrence Tang return -1; 1628f793ac3SLawrence Tang if (validated) 1638f793ac3SLawrence Tang break; 1648f793ac3SLawrence Tang } 1658f793ac3SLawrence Tang 1668f793ac3SLawrence Tang //Return if failed all checks. 1678f793ac3SLawrence Tang if (!validated) 1688f793ac3SLawrence Tang { 1698f793ac3SLawrence Tang log_validator_error(error_message, "No schema object structures matched provided object for field '%s'.", field_name); 1708f793ac3SLawrence Tang return 0; 1718f793ac3SLawrence Tang } 1728f793ac3SLawrence Tang } 17345e04b0aSLawrence Tang 1748a2d737cSLawrence Tang //Switch and validate each type in turn. 1758a2d737cSLawrence Tang switch (json_object_get_type(object)) 1768a2d737cSLawrence Tang { 1778a2d737cSLawrence Tang case json_type_int: 1788a2d737cSLawrence Tang return validate_integer(field_name, schema, object, error_message); 1798a2d737cSLawrence Tang case json_type_string: 1808a2d737cSLawrence Tang return validate_string(field_name, schema, object, error_message); 1818a2d737cSLawrence Tang case json_type_object: 1828a2d737cSLawrence Tang return validate_object(field_name, schema, object, error_message); 1838a2d737cSLawrence Tang case json_type_array: 1848f793ac3SLawrence Tang return validate_array(field_name, schema, object, error_message); 1858a2d737cSLawrence Tang 1868a2d737cSLawrence Tang //We don't perform extra validation on this type. 1878a2d737cSLawrence Tang default: 1888f793ac3SLawrence Tang log_validator_debug("validation passed for '%s' (no extra validation).", field_name); 1898a2d737cSLawrence Tang return 1; 1908a2d737cSLawrence Tang } 1918a2d737cSLawrence Tang } 1928a2d737cSLawrence Tang 1938a2d737cSLawrence Tang //Validates a single integer value according to the given specification. 1948a2d737cSLawrence Tang int validate_integer(const char* field_name, json_object* schema, json_object* object, char* error_message) 1958a2d737cSLawrence Tang { 1968a2d737cSLawrence Tang //Is there a minimum/maximum specified? If so, check those. 1978a2d737cSLawrence Tang //Validate minimum. 1988a2d737cSLawrence Tang json_object* min_value = json_object_object_get(schema, "minimum"); 1998a2d737cSLawrence Tang if (min_value != NULL && json_object_is_type(min_value, json_type_int)) 2008a2d737cSLawrence Tang { 2018a2d737cSLawrence Tang int min_value_int = json_object_get_int(min_value); 2028a2d737cSLawrence Tang if (json_object_get_uint64(object) < min_value_int) 2038a2d737cSLawrence Tang { 2048f793ac3SLawrence Tang log_validator_error(error_message, "Failed to validate integer field '%s'. Value was below minimum of %d.", field_name, min_value_int); 2058a2d737cSLawrence Tang return 0; 2068a2d737cSLawrence Tang } 2078a2d737cSLawrence Tang } 2088a2d737cSLawrence Tang 2098a2d737cSLawrence Tang //Validate maximum. 2108a2d737cSLawrence Tang json_object* max_value = json_object_object_get(schema, "maximum"); 2118a2d737cSLawrence Tang if (max_value != NULL && json_object_is_type(max_value, json_type_int)) 2128a2d737cSLawrence Tang { 2138a2d737cSLawrence Tang int max_value_int = json_object_get_int(max_value); 2148a2d737cSLawrence Tang if (json_object_get_uint64(object) > max_value_int) 2158a2d737cSLawrence Tang { 2168f793ac3SLawrence Tang log_validator_error(error_message, "Failed to validate integer field '%s'. Value was above maximum of %d.", field_name, max_value_int); 2178a2d737cSLawrence Tang return 0; 2188a2d737cSLawrence Tang } 2198a2d737cSLawrence Tang } 2208a2d737cSLawrence Tang 2218a2d737cSLawrence Tang return 1; 2228a2d737cSLawrence Tang } 2238a2d737cSLawrence Tang 2248a2d737cSLawrence Tang //Validates a single string value according to the given specification. 2258a2d737cSLawrence Tang int validate_string(const char* field_name, json_object* schema, json_object* object, char* error_message) 2268a2d737cSLawrence Tang { 2278a2d737cSLawrence Tang //todo: if there is a "pattern" field, verify the string with RegEx. 2288a2d737cSLawrence Tang return 1; 2298a2d737cSLawrence Tang } 2308a2d737cSLawrence Tang 2318a2d737cSLawrence Tang //Validates a single object value according to the given specification. 2328a2d737cSLawrence Tang int validate_object(const char* field_name, json_object* schema, json_object* object, char* error_message) 2338a2d737cSLawrence Tang { 2348a2d737cSLawrence Tang //Are there a set of "required" fields? If so, check they all exist. 2358a2d737cSLawrence Tang json_object* required_fields = json_object_object_get(schema, "required"); 2368a2d737cSLawrence Tang if (required_fields != NULL && json_object_get_type(required_fields) == json_type_array) 2378a2d737cSLawrence Tang { 2388f793ac3SLawrence Tang log_validator_debug("Required fields found for '%s', matching...", field_name); 2398f793ac3SLawrence Tang 2408a2d737cSLawrence Tang int len = json_object_array_length(required_fields); 2418a2d737cSLawrence Tang for (int i=0; i<len; i++) 2428a2d737cSLawrence Tang { 2438a2d737cSLawrence Tang //Get the required field from schema. 2448a2d737cSLawrence Tang json_object* required_field = json_object_array_get_idx(required_fields, i); 2458a2d737cSLawrence Tang if (json_object_get_type(required_field) != json_type_string) 2468a2d737cSLawrence Tang { 2478f793ac3SLawrence Tang log_validator_error(error_message, "Required field for object '%s' is not a string (schema violation).", field_name); 2488a2d737cSLawrence Tang return 0; 2498a2d737cSLawrence Tang } 2508a2d737cSLawrence Tang 2518a2d737cSLawrence Tang //Does it exist in the object? 2528a2d737cSLawrence Tang const char* required_field_str = json_object_get_string(required_field); 2538a2d737cSLawrence Tang if (json_object_object_get(object, required_field_str) == NULL) 2548a2d737cSLawrence Tang { 2558f793ac3SLawrence Tang log_validator_error(error_message, "Required field '%s' was not present in object '%s'.", required_field_str, field_name); 2568a2d737cSLawrence Tang return 0; 2578a2d737cSLawrence Tang } 2588a2d737cSLawrence Tang } 2598a2d737cSLawrence Tang } 2608a2d737cSLawrence Tang 261c4814594SLawrence Tang //Get additional properties value in advance. 262c4814594SLawrence Tang json_object* additional_properties = json_object_object_get(schema, "additionalProperties"); 263c4814594SLawrence Tang int additional_properties_allowed = 0; 264c4814594SLawrence Tang if (additional_properties != NULL && json_object_get_type(additional_properties) == json_type_boolean) 265c4814594SLawrence Tang additional_properties_allowed = json_object_get_boolean(additional_properties); 2668a2d737cSLawrence Tang 2678a2d737cSLawrence Tang //Run through the "properties" object and validate each of those in turn. 2688a2d737cSLawrence Tang json_object* properties = json_object_object_get(schema, "properties"); 2698a2d737cSLawrence Tang if (properties != NULL && json_object_get_type(properties) == json_type_object) 2708a2d737cSLawrence Tang { 2718a2d737cSLawrence Tang json_object_object_foreach(properties, key, value) { 2728a2d737cSLawrence Tang 2738a2d737cSLawrence Tang //If the given property name does not exist on the target object, ignore and continue next. 2748a2d737cSLawrence Tang json_object* object_prop = json_object_object_get(object, key); 2758a2d737cSLawrence Tang if (object_prop == NULL) 2768a2d737cSLawrence Tang continue; 2778a2d737cSLawrence Tang 2788a2d737cSLawrence Tang //Validate against the schema. 2798a2d737cSLawrence Tang if (!validate_field(key, value, object_prop, error_message)) 2808a2d737cSLawrence Tang return 0; 2818a2d737cSLawrence Tang } 282c4814594SLawrence Tang 283c4814594SLawrence Tang //If additional properties are banned, validate that no additional properties exist. 284c4814594SLawrence Tang if (!additional_properties_allowed) 285c4814594SLawrence Tang { 286c4814594SLawrence Tang json_object_object_foreach(object, key, value) { 287c4814594SLawrence Tang 288c4814594SLawrence Tang //If the given property name does not exist on the schema object, fail validation. 289c4814594SLawrence Tang json_object* schema_prop = json_object_object_get(properties, key); 290c4814594SLawrence Tang if (schema_prop == NULL) 291c4814594SLawrence Tang { 292c4814594SLawrence Tang log_validator_error(error_message, "Invalid additional property '%s' detected on field '%s'.", key, field_name); 293c4814594SLawrence Tang return 0; 294c4814594SLawrence Tang } 295c4814594SLawrence Tang } 296c4814594SLawrence Tang } 2978a2d737cSLawrence Tang } 2988a2d737cSLawrence Tang 2998a2d737cSLawrence Tang return 1; 3008a2d737cSLawrence Tang } 3018a2d737cSLawrence Tang 3028a2d737cSLawrence Tang //Validates a single array value according to the given specification. 3038a2d737cSLawrence Tang int validate_array(const char* field_name, json_object* schema, json_object* object, char* error_message) 3048a2d737cSLawrence Tang { 30545e04b0aSLawrence Tang //Iterate all items in the array, and validate according to the "items" schema. 30645e04b0aSLawrence Tang json_object* items_schema = json_object_object_get(schema, "items"); 30745e04b0aSLawrence Tang if (items_schema != NULL && json_object_get_type(items_schema) == json_type_object) 30845e04b0aSLawrence Tang { 30945e04b0aSLawrence Tang int array_len = json_object_array_length(object); 31045e04b0aSLawrence Tang for (int i=0; i<array_len; i++) 31145e04b0aSLawrence Tang { 31245e04b0aSLawrence Tang if (!validate_field(field_name, items_schema, json_object_array_get_idx(object, i), error_message)) 31345e04b0aSLawrence Tang return 0; 31445e04b0aSLawrence Tang } 31545e04b0aSLawrence Tang } 31645e04b0aSLawrence Tang 3178a2d737cSLawrence Tang return 1; 3188a2d737cSLawrence Tang } 3198f793ac3SLawrence Tang 3208f793ac3SLawrence Tang //Enables/disables debugging globally for the JSON validator. 3218f793ac3SLawrence Tang void validate_schema_debug_enable() { json_validator_debug = 1; } 3228f793ac3SLawrence Tang void validate_schema_debug_disable() { json_validator_debug = 0; } 3238f793ac3SLawrence Tang 324*7cd13908SLawrence Tang //Logs an error message to the given error message location and (optionally) provides debug output. 3258f793ac3SLawrence Tang void log_validator_error(char* error_message, const char* format, ...) 3268f793ac3SLawrence Tang { 3278f793ac3SLawrence Tang va_list args; 3288f793ac3SLawrence Tang 3298f793ac3SLawrence Tang //Log error to error out. 3308f793ac3SLawrence Tang va_start(args, format); 3318f793ac3SLawrence Tang vsnprintf(error_message, JSON_ERROR_MSG_MAX_LEN, format, args); 3328f793ac3SLawrence Tang va_end(args); 3338f793ac3SLawrence Tang 3348f793ac3SLawrence Tang //Debug message if necessary. 3358f793ac3SLawrence Tang va_start(args, format); 3368f793ac3SLawrence Tang log_validator_msg(format, args); 3378f793ac3SLawrence Tang va_end(args); 3388f793ac3SLawrence Tang } 3398f793ac3SLawrence Tang 340*7cd13908SLawrence Tang //Logs a debug message to stdout, if validator debug is enabled. 3418f793ac3SLawrence Tang void log_validator_debug(const char* format, ...) 3428f793ac3SLawrence Tang { 3438f793ac3SLawrence Tang va_list args; 3448f793ac3SLawrence Tang va_start(args, format); 3458f793ac3SLawrence Tang log_validator_msg(format, args); 3468f793ac3SLawrence Tang va_end(args); 3478f793ac3SLawrence Tang } 3488f793ac3SLawrence Tang 3498f793ac3SLawrence Tang //Logs a single validator debug/error message. 3508f793ac3SLawrence Tang void log_validator_msg(const char* format, va_list args) 3518f793ac3SLawrence Tang { 3528f793ac3SLawrence Tang //Print debug output if debug is on. 3538f793ac3SLawrence Tang if (json_validator_debug) 3548f793ac3SLawrence Tang { 3558f793ac3SLawrence Tang //Make new format string for error. 3568f793ac3SLawrence Tang const char* header = "json_validator: "; 3578f793ac3SLawrence Tang char* new_format = malloc(strlen(header) + strlen(format) + 2); 3588f793ac3SLawrence Tang strcpy(new_format, header); 3598f793ac3SLawrence Tang strcat(new_format, format); 3608f793ac3SLawrence Tang strcat(new_format, "\n"); 3618f793ac3SLawrence Tang 3628f793ac3SLawrence Tang //Print & free format. 3638f793ac3SLawrence Tang vfprintf(stdout, new_format, args); 3648f793ac3SLawrence Tang free(new_format); 3658f793ac3SLawrence Tang } 3668f793ac3SLawrence Tang }