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