1 /** 2 * A very basic, non-complete implementation of a validator for the JSON Schema specification, 3 * for validating CPER-JSON. 4 * 5 * Author: Lawrence.Tang@arm.com 6 **/ 7 8 #include <stdio.h> 9 #include <string.h> 10 #include <unistd.h> 11 #include <libgen.h> 12 #include <limits.h> 13 #include <stdarg.h> 14 #include <json.h> 15 #include "json-schema.h" 16 #include "edk/BaseTypes.h" 17 18 //Field definitions. 19 int json_validator_debug = 0; 20 21 //Private pre-definitions. 22 int validate_field(const char *name, json_object *schema, json_object *object, 23 char *error_message); 24 int validate_integer(const char *field_name, json_object *schema, 25 json_object *object, char *error_message); 26 int validate_string(const char *field_name, json_object *schema, 27 json_object *object, const char *error_message); 28 int validate_object(const char *field_name, json_object *schema, 29 json_object *object, char *error_message); 30 int validate_array(const char *field_name, json_object *schema, 31 json_object *object, char *error_message); 32 void log_validator_error(char *error_message, const char *format, ...); 33 void log_validator_debug(const char *format, ...); 34 void log_validator_msg(const char *format, va_list args); 35 36 //Validates a single JSON object against a provided schema file, returning 1 on success and 0 on failure to validate. 37 //Error message space must be allocated prior to call. 38 int validate_schema_from_file(const char *schema_file, json_object *object, 39 char *error_message) 40 { 41 //Load schema IR from file. 42 json_object *schema_ir = json_object_from_file(schema_file); 43 if (schema_ir == NULL) { 44 log_validator_error(error_message, 45 "Failed to load schema from file '%s'.", 46 schema_file); 47 return 0; 48 } 49 50 //Get the directory of the file. 51 char *schema_file_copy = malloc(strlen(schema_file) + 1); 52 strcpy(schema_file_copy, schema_file); 53 char *schema_dir = dirname(schema_file_copy); 54 55 int result = 56 validate_schema(schema_ir, schema_dir, object, error_message); 57 58 //Free memory from directory call. 59 free(schema_file_copy); 60 json_object_put(schema_ir); 61 62 return result; 63 } 64 65 //Validates a single JSON object against a provided schema, returning 1 on success and 0 on failure to validate. 66 //Error message space must be allocated prior to call. 67 //If the schema does not include any other sub-schemas using "$ref", then leaving schema_directory as NULL is valid. 68 int validate_schema(json_object *schema, char *schema_directory, 69 json_object *object, char *error_message) 70 { 71 //Check that the schema version is the same as this validator. 72 json_object *schema_ver = json_object_object_get(schema, "$schema"); 73 if (schema_ver == NULL || strcmp(json_object_get_string(schema_ver), 74 JSON_SCHEMA_VERSION) != 0) { 75 log_validator_error( 76 error_message, 77 "Provided schema is not of the same version that is referenced by this validator, or is not a schema."); 78 return 0; 79 } 80 81 //Change current directory into the schema directory. 82 char *original_cwd = malloc(PATH_MAX); 83 if (getcwd(original_cwd, PATH_MAX) == NULL) { 84 log_validator_error(error_message, 85 "Failed fetching the current directory."); 86 if (original_cwd) { 87 free(original_cwd); 88 } 89 return 0; 90 } 91 if (chdir(schema_directory)) { 92 log_validator_error(error_message, 93 "Failed to chdir into schema directory."); 94 if (original_cwd) { 95 free(original_cwd); 96 } 97 return 0; 98 } 99 100 //Parse the top level structure appropriately. 101 int result = validate_field("parent", schema, object, error_message); 102 103 //Change back to original CWD. 104 if (chdir(original_cwd)) { 105 log_validator_error(error_message, 106 "Failed to chdir into original directory."); 107 } 108 109 free(original_cwd); 110 111 if (result) { 112 log_validator_debug( 113 "Successfully validated the provided object against schema."); 114 } 115 return result; 116 } 117 118 //Validates a single JSON field given a schema/object. 119 //Returns -1 on fatal/error failure, 0 on validation failure, and 1 on validation. 120 int validate_field(const char *field_name, json_object *schema, 121 json_object *object, char *error_message) 122 { 123 log_validator_debug("Validating field '%s'...", field_name); 124 125 //If there is a "$ref" field, attempt to load the referenced schema. 126 json_object *ref_schema = json_object_object_get(schema, "$ref"); 127 if (ref_schema != NULL && 128 json_object_get_type(ref_schema) == json_type_string) { 129 log_validator_debug("$ref schema detected for field '%s'.", 130 field_name); 131 132 //Attempt to load. If loading fails, report error. 133 const char *ref_path = json_object_get_string(ref_schema); 134 json_object *tmp = json_object_from_file(ref_path); 135 if (tmp == NULL) { 136 log_validator_error( 137 error_message, 138 "Failed to open referenced schema file '%s'.", 139 ref_path); 140 return -1; 141 } 142 json_object_put(tmp); 143 144 log_validator_debug("loaded schema path '%s' for field '%s'.", 145 ref_path, field_name); 146 } 147 148 //Get the schema field type. 149 json_object *desired_field_type = 150 json_object_object_get(schema, "type"); 151 if (desired_field_type == NULL || 152 !json_object_is_type(desired_field_type, json_type_string)) { 153 log_validator_error( 154 error_message, 155 "Desired field type not provided within schema/is not a string for field '%s' (schema violation).", 156 field_name); 157 return -1; 158 } 159 160 //Check the field types are actually equal. 161 const char *desired_field_type_str = 162 json_object_get_string(desired_field_type); 163 if (!((!strcmp(desired_field_type_str, "object") && 164 json_object_is_type(object, json_type_object)) || 165 (!strcmp(desired_field_type_str, "array") && 166 json_object_is_type(object, json_type_array)) || 167 (!strcmp(desired_field_type_str, "integer") && 168 json_object_is_type(object, json_type_int)) || 169 (!strcmp(desired_field_type_str, "string") && 170 json_object_is_type(object, json_type_string)) || 171 (!strcmp(desired_field_type_str, "boolean") && 172 json_object_is_type(object, json_type_boolean)) || 173 (!strcmp(desired_field_type_str, "double") && 174 json_object_is_type(object, json_type_double)))) { 175 log_validator_error(error_message, 176 "Field type match failed for field '%s'.", 177 field_name); 178 return 0; 179 } 180 181 //If the schema contains a "oneOf" array, we need to validate the field against each of the 182 //possible options in turn. 183 json_object *one_of = json_object_object_get(schema, "oneOf"); 184 if (one_of != NULL && json_object_get_type(one_of) == json_type_array) { 185 log_validator_debug("oneOf options detected for field '%s'.", 186 field_name); 187 188 int len = json_object_array_length(one_of); 189 int validated = 0; 190 for (int i = 0; i < len; i++) { 191 //If the "oneOf" member isn't an object, warn on schema violation. 192 json_object *one_of_option = 193 json_object_array_get_idx(one_of, i); 194 if (one_of_option == NULL || 195 json_object_get_type(one_of_option) != 196 json_type_object) { 197 log_validator_debug( 198 "Schema Warning: 'oneOf' member for field '%s' is not an object, schema violation.", 199 field_name); 200 continue; 201 } 202 203 //Validate field with schema. 204 validated = validate_field(field_name, one_of_option, 205 object, error_message); 206 if (validated == -1) { 207 return -1; 208 } 209 if (validated) { 210 break; 211 } 212 } 213 214 //Return if failed all checks. 215 if (!validated) { 216 log_validator_error( 217 error_message, 218 "No schema object structures matched provided object for field '%s'.", 219 field_name); 220 return 0; 221 } 222 } 223 224 //Switch and validate each type in turn. 225 switch (json_object_get_type(object)) { 226 case json_type_int: 227 return validate_integer(field_name, schema, object, 228 error_message); 229 case json_type_string: 230 return validate_string(field_name, schema, object, 231 error_message); 232 case json_type_object: 233 return validate_object(field_name, schema, object, 234 error_message); 235 case json_type_array: 236 return validate_array(field_name, schema, object, 237 error_message); 238 239 //We don't perform extra validation on this type. 240 default: 241 log_validator_debug( 242 "validation passed for '%s' (no extra validation).", 243 field_name); 244 return 1; 245 } 246 } 247 248 //Validates a single integer value according to the given specification. 249 int validate_integer(const char *field_name, json_object *schema, 250 json_object *object, char *error_message) 251 { 252 //Is there a minimum/maximum specified? If so, check those. 253 //Validate minimum. 254 json_object *min_value = json_object_object_get(schema, "minimum"); 255 if (min_value != NULL && 256 json_object_is_type(min_value, json_type_int)) { 257 int min_value_int = json_object_get_int(min_value); 258 if (json_object_get_uint64(object) < (uint64_t)min_value_int) { 259 log_validator_error( 260 error_message, 261 "Failed to validate integer field '%s'. Value was below minimum of %d.", 262 field_name, min_value_int); 263 return 0; 264 } 265 } 266 267 //Validate maximum. 268 json_object *max_value = json_object_object_get(schema, "maximum"); 269 if (max_value != NULL && 270 json_object_is_type(max_value, json_type_int)) { 271 int max_value_int = json_object_get_int(max_value); 272 if (json_object_get_uint64(object) > (uint64_t)max_value_int) { 273 log_validator_error( 274 error_message, 275 "Failed to validate integer field '%s'. Value was above maximum of %d.", 276 field_name, max_value_int); 277 return 0; 278 } 279 } 280 281 return 1; 282 } 283 284 //Validates a single string value according to the given specification. 285 int validate_string(const char *field_name, json_object *schema, 286 json_object *object, const char *error_message) 287 { 288 //todo: if there is a "pattern" field, verify the string with RegEx. 289 (void)field_name; 290 (void)schema; 291 (void)object; 292 (void)error_message; 293 294 return 1; 295 } 296 297 //Validates a single object value according to the given specification. 298 int validate_object(const char *field_name, json_object *schema, 299 json_object *object, char *error_message) 300 { 301 //Are there a set of "required" fields? If so, check they all exist. 302 json_object *required_fields = 303 json_object_object_get(schema, "required"); 304 if (required_fields != NULL && 305 json_object_get_type(required_fields) == json_type_array) { 306 log_validator_debug( 307 "Required fields found for '%s', matching...", 308 field_name); 309 310 int len = json_object_array_length(required_fields); 311 for (int i = 0; i < len; i++) { 312 //Get the required field from schema. 313 json_object *required_field = 314 json_object_array_get_idx(required_fields, i); 315 if (json_object_get_type(required_field) != 316 json_type_string) { 317 log_validator_error( 318 error_message, 319 "Required field for object '%s' is not a string (schema violation).", 320 field_name); 321 return 0; 322 } 323 324 //Does it exist in the object? 325 const char *required_field_str = 326 json_object_get_string(required_field); 327 if (json_object_object_get( 328 object, required_field_str) == NULL) { 329 log_validator_error( 330 error_message, 331 "Required field '%s' was not present in object '%s'.", 332 required_field_str, field_name); 333 return 0; 334 } 335 } 336 } 337 338 //Get additional properties value in advance. 339 json_object *additional_properties = 340 json_object_object_get(schema, "additionalProperties"); 341 int additional_properties_allowed = 0; 342 if (additional_properties != NULL && 343 json_object_get_type(additional_properties) == json_type_boolean) { 344 additional_properties_allowed = 345 json_object_get_boolean(additional_properties); 346 } 347 348 //Run through the "properties" object and validate each of those in turn. 349 json_object *properties = json_object_object_get(schema, "properties"); 350 if (properties != NULL && 351 json_object_get_type(properties) == json_type_object) { 352 json_object_object_foreach(properties, key, value) 353 { 354 //If the given property name does not exist on the target object, ignore and continue next. 355 json_object *object_prop = 356 json_object_object_get(object, key); 357 if (object_prop == NULL) { 358 continue; 359 } 360 361 //Validate against the schema. 362 if (!validate_field(key, value, object_prop, 363 error_message)) { 364 return 0; 365 } 366 } 367 368 //If additional properties are banned, validate that no additional properties exist. 369 if (!additional_properties_allowed) { 370 json_object_object_foreach(object, key, value) 371 { 372 //Avoid compiler warning 373 (void)value; 374 375 //If the given property name does not exist on the schema object, fail validation. 376 const json_object *schema_prop = 377 json_object_object_get(properties, key); 378 if (schema_prop == NULL) { 379 log_validator_error( 380 error_message, 381 "Invalid additional property '%s' detected on field '%s'.", 382 key, field_name); 383 return 0; 384 } 385 } 386 } 387 } 388 389 return 1; 390 } 391 392 //Validates a single array value according to the given specification. 393 int validate_array(const char *field_name, json_object *schema, 394 json_object *object, char *error_message) 395 { 396 //Iterate all items in the array, and validate according to the "items" schema. 397 json_object *items_schema = json_object_object_get(schema, "items"); 398 if (items_schema != NULL && 399 json_object_get_type(items_schema) == json_type_object) { 400 int array_len = json_object_array_length(object); 401 for (int i = 0; i < array_len; i++) { 402 if (!validate_field(field_name, items_schema, 403 json_object_array_get_idx(object, 404 i), 405 error_message)) { 406 return 0; 407 } 408 } 409 } 410 411 return 1; 412 } 413 414 //Enables/disables debugging globally for the JSON validator. 415 void validate_schema_debug_enable() 416 { 417 json_validator_debug = 1; 418 } 419 void validate_schema_debug_disable() 420 { 421 json_validator_debug = 0; 422 } 423 424 //Logs an error message to the given error message location and (optionally) provides debug output. 425 void log_validator_error(char *error_message, const char *format, ...) 426 { 427 va_list args; 428 429 //Log error to error out. 430 va_start(args, format); 431 vsnprintf(error_message, JSON_ERROR_MSG_MAX_LEN, format, args); 432 va_end(args); 433 434 //Debug message if necessary. 435 va_start(args, format); 436 log_validator_msg(format, args); 437 va_end(args); 438 } 439 440 //Logs a debug message to stdout, if validator debug is enabled. 441 void log_validator_debug(const char *format, ...) 442 { 443 va_list args; 444 va_start(args, format); 445 log_validator_msg(format, args); 446 va_end(args); 447 } 448 449 //Logs a single validator debug/error message. 450 void log_validator_msg(const char *format, va_list args) 451 { 452 //Print debug output if debug is on. 453 if (json_validator_debug) { 454 //Make new format string for error. 455 const char *header = "json_validator: "; 456 char *new_format = malloc(strlen(header) + strlen(format) + 2); 457 strcpy(new_format, header); 458 strcat(new_format, format); 459 strcat(new_format, "\n"); 460 461 //Print & free format. 462 vfprintf(stdout, new_format, args); 463 free(new_format); 464 } 465 } 466