xref: /openbmc/libcper/json-schema.c (revision 8f793ac3)
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     //If the boolean field "additionalProperties" exists and is set to false, ensure there are no
262     //extra properties apart from those required in the object.
263     //... todo
264 
265     //Run through the "properties" object and validate each of those in turn.
266     json_object* properties = json_object_object_get(schema, "properties");
267     if (properties != NULL && json_object_get_type(properties) == json_type_object)
268     {
269         json_object_object_foreach(properties, key, value) {
270 
271             //If the given property name does not exist on the target object, ignore and continue next.
272             json_object* object_prop = json_object_object_get(object, key);
273             if (object_prop == NULL)
274                 continue;
275 
276             //Validate against the schema.
277             if (!validate_field(key, value, object_prop, error_message))
278                 return 0;
279         }
280     }
281 
282     return 1;
283 }
284 
285 //Validates a single array value according to the given specification.
286 int validate_array(const char* field_name, json_object* schema, json_object* object, char* error_message)
287 {
288     //Iterate all items in the array, and validate according to the "items" schema.
289     json_object* items_schema = json_object_object_get(schema, "items");
290     if (items_schema != NULL && json_object_get_type(items_schema) == json_type_object)
291     {
292         int array_len = json_object_array_length(object);
293         for (int i=0; i<array_len; i++)
294         {
295             if (!validate_field(field_name, items_schema, json_object_array_get_idx(object, i), error_message))
296                 return 0;
297         }
298     }
299 
300     return 1;
301 }
302 
303 //Enables/disables debugging globally for the JSON validator.
304 void validate_schema_debug_enable() { json_validator_debug = 1; }
305 void validate_schema_debug_disable() { json_validator_debug = 0; }
306 
307 //Logs an error message to the given character and (optionally) provides debug output.
308 void log_validator_error(char* error_message, const char* format, ...)
309 {
310     va_list args;
311 
312     //Log error to error out.
313     va_start(args, format);
314     vsnprintf(error_message, JSON_ERROR_MSG_MAX_LEN, format, args);
315     va_end(args);
316 
317     //Debug message if necessary.
318     va_start(args, format);
319     log_validator_msg(format, args);
320     va_end(args);
321 }
322 
323 //Logs a debug message to the given character and (optionally) provides debug output.
324 void log_validator_debug(const char* format, ...)
325 {
326     va_list args;
327     va_start(args, format);
328     log_validator_msg(format, args);
329     va_end(args);
330 }
331 
332 //Logs a single validator debug/error message.
333 void log_validator_msg(const char* format, va_list args)
334 {
335     //Print debug output if debug is on.
336     if (json_validator_debug)
337     {
338         //Make new format string for error.
339         const char* header = "json_validator: ";
340         char* new_format = malloc(strlen(header) + strlen(format) + 2);
341         strcpy(new_format, header);
342         strcat(new_format, format);
343         strcat(new_format, "\n");
344 
345         //Print & free format.
346         vfprintf(stdout, new_format, args);
347         free(new_format);
348     }
349 }