Coverage Report

Created: 2026-12-01 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mod_auth_openidc/src/json.c
Line
Count
Source
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
20
/***************************************************************************
21
 * Copyright (C) 2017-2026 ZmartZone Holding BV
22
 * All rights reserved.
23
 *
24
 * DISCLAIMER OF WARRANTIES:
25
 *
26
 * THE SOFTWARE PROVIDED HEREUNDER IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
27
 * ANY WARRANTIES OR REPRESENTATIONS EXPRESS, IMPLIED OR STATUTORY; INCLUDING,
28
 * WITHOUT LIMITATION, WARRANTIES OF QUALITY, PERFORMANCE, NONINFRINGEMENT,
29
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  NOR ARE THERE ANY
30
 * WARRANTIES CREATED BY A COURSE OR DEALING, COURSE OF PERFORMANCE OR TRADE
31
 * USAGE.  FURTHERMORE, THERE ARE NO WARRANTIES THAT THE SOFTWARE WILL MEET
32
 * YOUR NEEDS OR BE FREE FROM ERRORS, OR THAT THE OPERATION OF THE SOFTWARE
33
 * WILL BE UNINTERRUPTED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
34
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF
36
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
 *
40
 * @Author: Hans Zandbelt - hans.zandbelt@openidc.com
41
 */
42
43
/*
44
 * This is the single translation unit that talks to the JSON backend library (libjansson).
45
 * The opaque oidc_json_t type, the oidc_json_* functions and the OIDC_JSON_* constants declared
46
 * in json.h shield the rest of the module from the backend; oidc_json_t is identical to the
47
 * backend value type (struct json_t), so the thin wrappers below pass pointers through without
48
 * casting. Backend-specific concepts (encode/decode flags, value types) are translated here.
49
 */
50
51
#include <limits.h>
52
53
#include <jansson.h>
54
55
#include "const.h"
56
57
#include "json.h"
58
#include "proto/proto.h"
59
#include "util/util.h"
60
61
/*
62
 * translate the backend-independent OIDC_JSON_* flags into libjansson encode/decode flags
63
 */
64
458k
static size_t oidc_json_flags2backend(int flags) {
65
458k
  size_t f = 0;
66
458k
  int indent = 0;
67
458k
  if (flags & OIDC_JSON_COMPACT)
68
385k
    f |= JSON_COMPACT;
69
458k
  if (flags & OIDC_JSON_PRESERVE_ORDER)
70
374k
    f |= JSON_PRESERVE_ORDER;
71
458k
  if (flags & OIDC_JSON_ENCODE_ANY)
72
15.9k
    f |= JSON_ENCODE_ANY;
73
458k
  if (flags & OIDC_JSON_DECODE_ANY)
74
0
    f |= JSON_DECODE_ANY;
75
458k
  indent = (flags >> 8) & 0x1F;
76
458k
  if (indent > 0)
77
9
    f |= JSON_INDENT(indent);
78
458k
  return f;
79
458k
}
80
81
/*
82
 * construction
83
 */
84
54.9k
oidc_json_t *oidc_json_object(void) {
85
54.9k
  return json_object();
86
54.9k
}
87
88
0
oidc_json_t *oidc_json_array(void) {
89
0
  return json_array();
90
0
}
91
92
87.8k
oidc_json_t *oidc_json_string(const char *value) {
93
87.8k
  return json_string(value);
94
87.8k
}
95
96
82.2k
oidc_json_t *oidc_json_integer(oidc_json_int_t value) {
97
82.2k
  return json_integer((json_int_t)value);
98
82.2k
}
99
100
0
oidc_json_t *oidc_json_boolean(int value) {
101
0
  return json_boolean(value);
102
0
}
103
104
/*
105
 * reference counting and copying
106
 */
107
95.5k
void oidc_json_decref(oidc_json_t *json) {
108
95.5k
  json_decref(json);
109
95.5k
}
110
111
1.34k
oidc_json_t *oidc_json_copy(const oidc_json_t *json) {
112
1.34k
  return json_copy((oidc_json_t *)json);
113
1.34k
}
114
115
7.95k
oidc_json_t *oidc_json_deep_copy(const oidc_json_t *json) {
116
7.95k
  return json_deep_copy(json);
117
7.95k
}
118
119
/*
120
 * type inspection
121
 */
122
1.16M
oidc_json_type_t oidc_json_typeof(const oidc_json_t *json) {
123
1.16M
  switch (json_typeof(json)) {
124
4.75k
  case JSON_OBJECT:
125
4.75k
    return OIDC_JSON_TYPE_OBJECT;
126
3.67k
  case JSON_ARRAY:
127
3.67k
    return OIDC_JSON_TYPE_ARRAY;
128
2.26k
  case JSON_STRING:
129
2.26k
    return OIDC_JSON_TYPE_STRING;
130
934k
  case JSON_INTEGER:
131
934k
    return OIDC_JSON_TYPE_INTEGER;
132
178k
  case JSON_REAL:
133
178k
    return OIDC_JSON_TYPE_REAL;
134
1.41k
  case JSON_TRUE:
135
1.41k
    return OIDC_JSON_TYPE_TRUE;
136
1.82k
  case JSON_FALSE:
137
1.82k
    return OIDC_JSON_TYPE_FALSE;
138
39.1k
  case JSON_NULL:
139
39.1k
  default:
140
39.1k
    return OIDC_JSON_TYPE_NULL;
141
1.16M
  }
142
1.16M
}
143
144
54.5k
int oidc_json_is_object(const oidc_json_t *json) {
145
54.5k
  return json_is_object(json);
146
54.5k
}
147
148
8.08k
int oidc_json_is_array(const oidc_json_t *json) {
149
8.08k
  return json_is_array(json);
150
8.08k
}
151
152
3.48M
int oidc_json_is_string(const oidc_json_t *json) {
153
3.48M
  return json_is_string(json);
154
3.48M
}
155
156
42.3k
int oidc_json_is_integer(const oidc_json_t *json) {
157
42.3k
  return json_is_integer(json);
158
42.3k
}
159
160
4.76k
int oidc_json_is_real(const oidc_json_t *json) {
161
4.76k
  return json_is_real(json);
162
4.76k
}
163
164
645
int oidc_json_is_number(const oidc_json_t *json) {
165
645
  return json_is_number(json);
166
645
}
167
168
438k
int oidc_json_is_boolean(const oidc_json_t *json) {
169
438k
  return json_is_boolean(json);
170
438k
}
171
172
959
int oidc_json_is_true(const oidc_json_t *json) {
173
959
  return json_is_true(json);
174
959
}
175
176
0
int oidc_json_is_null(const oidc_json_t *json) {
177
0
  return json_is_null(json);
178
0
}
179
180
/*
181
 * scalar value access
182
 */
183
1.69M
const char *oidc_json_string_value(const oidc_json_t *json) {
184
1.69M
  return json_string_value(json);
185
1.69M
}
186
187
101k
oidc_json_int_t oidc_json_integer_value(const oidc_json_t *json) {
188
101k
  return (oidc_json_int_t)json_integer_value(json);
189
101k
}
190
191
1.52k
double oidc_json_real_value(const oidc_json_t *json) {
192
1.52k
  return json_real_value(json);
193
1.52k
}
194
195
645
double oidc_json_number_value(const oidc_json_t *json) {
196
645
  return json_number_value(json);
197
645
}
198
199
44.9k
void oidc_json_integer_set(oidc_json_t *json, oidc_json_int_t value) {
200
44.9k
  json_integer_set(json, (json_int_t)value);
201
44.9k
}
202
203
/*
204
 * object access/mutation
205
 */
206
432k
oidc_json_t *oidc_json_object_get(const oidc_json_t *json, const char *key) {
207
432k
  return json_object_get(json, key);
208
432k
}
209
210
20.0k
int oidc_json_object_set(oidc_json_t *json, const char *key, oidc_json_t *value) {
211
20.0k
  return json_object_set(json, key, value);
212
20.0k
}
213
214
205k
int oidc_json_object_set_new(oidc_json_t *json, const char *key, oidc_json_t *value) {
215
205k
  return json_object_set_new(json, key, value);
216
205k
}
217
218
1.01k
int oidc_json_object_del(oidc_json_t *json, const char *key) {
219
1.01k
  return json_object_del(json, key);
220
1.01k
}
221
222
/*
223
 * object iteration
224
 */
225
52.9k
void *oidc_json_object_iter(oidc_json_t *json) {
226
52.9k
  return json_object_iter(json);
227
52.9k
}
228
229
104k
void *oidc_json_object_iter_next(oidc_json_t *json, void *iter) {
230
104k
  return json_object_iter_next(json, iter);
231
104k
}
232
233
102k
const char *oidc_json_object_iter_key(void *iter) {
234
102k
  return json_object_iter_key(iter);
235
102k
}
236
237
96.4k
oidc_json_t *oidc_json_object_iter_value(void *iter) {
238
96.4k
  return json_object_iter_value(iter);
239
96.4k
}
240
241
/*
242
 * array access/mutation
243
 */
244
2.86M
size_t oidc_json_array_size(const oidc_json_t *json) {
245
2.86M
  return json_array_size(json);
246
2.86M
}
247
248
3.77M
oidc_json_t *oidc_json_array_get(const oidc_json_t *json, size_t index) {
249
3.77M
  return json_array_get(json, index);
250
3.77M
}
251
252
0
int oidc_json_array_append_new(oidc_json_t *json, oidc_json_t *value) {
253
0
  return json_array_append_new(json, value);
254
0
}
255
256
/*
257
 * encode a JSON object into a pool-allocated string
258
 */
259
385k
char *oidc_json_encode(apr_pool_t *pool, const oidc_json_t *json, int flags) {
260
385k
  if (json == NULL)
261
0
    return NULL;
262
385k
  char *s = json_dumps(json, oidc_json_flags2backend(flags));
263
385k
  char *s_value = apr_pstrdup(pool, s);
264
385k
  free(s);
265
385k
  return s_value;
266
385k
}
267
268
6.16k
#define OIDC_JSON_MAX_ERROR_STR 4096
269
270
/*
271
 * parse a string into a JSON value; pool-based and silent, optionally returning an error message
272
 */
273
73.0k
apr_byte_t oidc_json_parse(apr_pool_t *pool, const char *str, int flags, oidc_json_t **json, char **s_err) {
274
73.0k
  json_error_t json_error;
275
276
73.0k
  *json = NULL;
277
73.0k
  if (s_err != NULL)
278
73.0k
    *s_err = NULL;
279
280
73.0k
  if (str == NULL) {
281
0
    if (s_err != NULL)
282
0
      *s_err = apr_pstrdup(pool, "input string is NULL");
283
0
    return FALSE;
284
0
  }
285
286
73.0k
  *json = json_loads(str, oidc_json_flags2backend(flags), &json_error);
287
73.0k
  if (*json == NULL) {
288
6.56k
    if (s_err != NULL) {
289
6.56k
#if JANSSON_VERSION_HEX >= 0x020B00
290
6.56k
      if (json_error_code(&json_error) == json_error_null_character)
291
403
        *s_err = apr_pstrdup(pool, json_error.text);
292
6.16k
      else
293
6.16k
#endif
294
6.16k
        *s_err = apr_psprintf(pool, "%s (%s)", json_error.text,
295
6.16k
                  apr_pstrndup(pool, str, OIDC_JSON_MAX_ERROR_STR));
296
6.56k
    }
297
6.56k
    return FALSE;
298
6.56k
  }
299
300
66.4k
  return TRUE;
301
73.0k
}
302
303
/*
304
 * parse a JSON object
305
 */
306
82.0k
apr_byte_t oidc_json_decode_object_err(request_rec *r, const char *str, oidc_json_t **json, apr_byte_t log_err) {
307
82.0k
  char *s_err = NULL;
308
309
82.0k
  if (str == NULL)
310
28.1k
    return FALSE;
311
312
  /* decode the JSON contents of the buffer */
313
53.8k
  if (oidc_json_parse(r->pool, str, 0, json, &s_err) == FALSE) {
314
    /* something went wrong */
315
4.54k
    if (log_err)
316
4.54k
      oidc_error(r, "JSON parsing returned an error: %s", s_err);
317
4.54k
    return FALSE;
318
4.54k
  }
319
320
49.3k
  if (!oidc_json_is_object(*json)) {
321
    /* a successfully parsed non-object (e.g. a top-level array) is still not a valid result here;
322
     * log_err controls only whether we log the rejection, not whether we reject */
323
401
    if (log_err)
324
401
      oidc_error(r, "parsed JSON did not contain a JSON object");
325
401
    oidc_json_decref(*json);
326
401
    *json = NULL;
327
401
    return FALSE;
328
401
  }
329
330
48.9k
  return TRUE;
331
49.3k
}
332
333
82.0k
apr_byte_t oidc_json_decode_object(request_rec *r, const char *str, oidc_json_t **json) {
334
82.0k
  return oidc_json_decode_object_err(r, str, json, TRUE);
335
82.0k
}
336
337
/*
338
 * printout a JSON string value
339
 */
340
0
static apr_byte_t oidc_json_string_print(request_rec *r, const oidc_json_t *result, const char *key, const char *log) {
341
0
  const oidc_json_t *value = oidc_json_object_get(result, key);
342
0
  if (value != NULL && !oidc_json_is_null(value)) {
343
0
    oidc_error(r, "%s: response contained an \"%s\" entry with value: \"%s\"", log, key,
344
0
         oidc_json_encode(r->pool, value,
345
0
              OIDC_JSON_PRESERVE_ORDER | OIDC_JSON_COMPACT | OIDC_JSON_ENCODE_ANY));
346
0
    return TRUE;
347
0
  }
348
0
  return FALSE;
349
0
}
350
351
/*
352
 * check a JSON object for "error" results and printout
353
 */
354
0
apr_byte_t oidc_json_check_error(request_rec *r, const oidc_json_t *json) {
355
0
  if (oidc_json_string_print(r, json, OIDC_PROTO_ERROR, "oidc_util_check_json_error") == TRUE) {
356
0
    oidc_json_string_print(r, json, OIDC_PROTO_ERROR_DESCRIPTION, "oidc_util_check_json_error");
357
0
    return TRUE;
358
0
  }
359
0
  return FALSE;
360
0
}
361
362
/*
363
 * decode a JSON string, check for "error" results and printout
364
 */
365
0
apr_byte_t oidc_json_decode_and_check_error(request_rec *r, const char *str, oidc_json_t **json) {
366
367
0
  if (oidc_json_decode_object(r, str, json) == FALSE)
368
0
    return FALSE;
369
370
  // see if it is an error response
371
0
  if (oidc_json_check_error(r, *json) == TRUE) {
372
0
    oidc_json_decref(*json);
373
0
    *json = NULL;
374
0
    return FALSE;
375
0
  }
376
377
0
  return TRUE;
378
0
}
379
380
/*
381
 * see if a certain string value is part of a JSON array with string elements
382
 */
383
907
apr_byte_t oidc_json_array_has_value(request_rec *r, const oidc_json_t *haystack, const char *needle) {
384
385
907
  if ((haystack == NULL) || (!oidc_json_is_array(haystack)))
386
0
    return FALSE;
387
388
907
  size_t i;
389
1.43M
  for (i = 0; i < oidc_json_array_size(haystack); i++) {
390
1.43M
    const oidc_json_t *elem = oidc_json_array_get(haystack, i);
391
1.43M
    if (!oidc_json_is_string(elem)) {
392
711k
      oidc_error(r, "unhandled in-array JSON non-string object type [%d]", oidc_json_typeof(elem));
393
711k
      continue;
394
711k
    }
395
724k
    if (_oidc_strcmp(oidc_json_string_value(elem), needle) == 0) {
396
403
      break;
397
403
    }
398
724k
  }
399
400
907
  return (i == oidc_json_array_size(haystack)) ? FALSE : TRUE;
401
907
}
402
403
/*
404
 * get (optional) string from a JSON object
405
 */
406
apr_byte_t oidc_json_object_get_string(apr_pool_t *pool, const oidc_json_t *json, const char *name, char **value,
407
134k
               const char *default_value) {
408
134k
  *value = default_value ? apr_pstrdup(pool, default_value) : NULL;
409
134k
  if (json != NULL) {
410
134k
    const oidc_json_t *v = oidc_json_object_get(json, name);
411
134k
    if ((v != NULL) && (oidc_json_is_string(v))) {
412
6.55k
      *value = apr_pstrdup(pool, oidc_json_string_value(v));
413
6.55k
    }
414
134k
  }
415
134k
  return TRUE;
416
134k
}
417
418
/*
419
 * get (optional) string array from a JSON object
420
 */
421
41
static void oidc_json_string_array_append(apr_pool_t *pool, const oidc_json_t *arr, apr_array_header_t *value) {
422
643k
  for (size_t i = 0; i < oidc_json_array_size(arr); i++) {
423
643k
    const oidc_json_t *v = oidc_json_array_get(arr, i);
424
    /* skip non-string elements rather than pushing a NULL (oidc_json_string_value returns NULL
425
     * for them), matching the single-string getter above */
426
643k
    if (oidc_json_is_string(v))
427
135k
      APR_ARRAY_PUSH(value, const char *) = apr_pstrdup(pool, oidc_json_string_value(v));
428
643k
  }
429
41
}
430
431
apr_byte_t oidc_json_object_get_string_array(apr_pool_t *pool, const oidc_json_t *json, const char *name,
432
2.66k
               apr_array_header_t **value, const apr_array_header_t *default_value) {
433
2.66k
  const oidc_json_t *arr = NULL;
434
2.66k
  *value = (default_value != NULL) ? apr_array_copy(pool, default_value) : NULL;
435
2.66k
  if (json != NULL) {
436
2.66k
    arr = oidc_json_object_get(json, name);
437
2.66k
    if ((arr != NULL) && (oidc_json_is_array(arr))) {
438
41
      *value = apr_array_make(pool, (int)oidc_json_array_size(arr), sizeof(const char *));
439
41
      oidc_json_string_array_append(pool, arr, *value);
440
41
    }
441
2.66k
  }
442
2.66k
  return TRUE;
443
2.66k
}
444
445
/*
446
 * get (optional) int from a JSON object
447
 */
448
22.3k
apr_byte_t oidc_json_object_get_int(const oidc_json_t *json, const char *name, int *value, const int default_value) {
449
22.3k
  const oidc_json_t *v = NULL;
450
22.3k
  *value = default_value;
451
22.3k
  if (json != NULL) {
452
22.3k
    v = oidc_json_object_get(json, name);
453
22.3k
    if ((v != NULL) && (oidc_json_is_integer(v))) {
454
      /* oidc_json_int_t is at least int64; clamp into int range to avoid silent truncation */
455
493
      oidc_json_int_t n = oidc_json_integer_value(v);
456
493
      if (n > INT_MAX)
457
12
        *value = INT_MAX;
458
481
      else if (n < INT_MIN)
459
69
        *value = INT_MIN;
460
412
      else
461
412
        *value = (int)n;
462
493
      return TRUE;
463
493
    }
464
22.3k
  }
465
21.8k
  return FALSE;
466
22.3k
}
467
468
/*
469
 * get (optional) 64-bit integer from a JSON object, preserving the full value - unlike
470
 * oidc_json_object_get_int, which clamps into int range; used for timestamps that must survive 2038
471
 */
472
apr_byte_t oidc_json_object_get_int64(const oidc_json_t *json, const char *name, oidc_json_int_t *value,
473
1.49k
              const oidc_json_int_t default_value) {
474
1.49k
  const oidc_json_t *v = NULL;
475
1.49k
  *value = default_value;
476
1.49k
  if (json != NULL) {
477
1.49k
    v = oidc_json_object_get(json, name);
478
1.49k
    if ((v != NULL) && (oidc_json_is_integer(v))) {
479
1.35k
      *value = oidc_json_integer_value(v);
480
1.35k
      return TRUE;
481
1.35k
    }
482
1.49k
  }
483
132
  return FALSE;
484
1.49k
}
485
486
/*
487
 * get (optional) boolean from a JSON object
488
 */
489
10.5k
apr_byte_t oidc_json_object_get_bool(const oidc_json_t *json, const char *name, int *value, const int default_value) {
490
10.5k
  const oidc_json_t *v = NULL;
491
10.5k
  *value = default_value;
492
10.5k
  if (json != NULL) {
493
10.5k
    v = oidc_json_object_get(json, name);
494
10.5k
    if ((v != NULL) && (oidc_json_is_boolean(v))) {
495
4
      *value = oidc_json_is_true(v);
496
4
      return TRUE;
497
4
    }
498
10.5k
  }
499
10.5k
  return FALSE;
500
10.5k
}
501
502
/*
503
 * merge two JSON objects
504
 */
505
759
apr_byte_t oidc_json_merge(request_rec *r, oidc_json_t *src, oidc_json_t *dst) {
506
507
759
  const char *key;
508
759
  oidc_json_t *value = NULL;
509
759
  void *iter = NULL;
510
511
759
  if ((src == NULL) || (dst == NULL))
512
0
    return FALSE;
513
514
759
  oidc_debug(r, "src=%s, dst=%s", oidc_json_encode(r->pool, src, OIDC_JSON_PRESERVE_ORDER | OIDC_JSON_COMPACT),
515
759
       oidc_json_encode(r->pool, dst, OIDC_JSON_PRESERVE_ORDER | OIDC_JSON_COMPACT));
516
517
759
  iter = oidc_json_object_iter(src);
518
1.65k
  while (iter) {
519
895
    key = oidc_json_object_iter_key(iter);
520
895
    value = oidc_json_object_iter_value(iter);
521
895
    oidc_json_object_set(dst, key, value);
522
895
    iter = oidc_json_object_iter_next(src, iter);
523
895
  }
524
525
759
  oidc_debug(r, "result dst=%s", oidc_json_encode(r->pool, dst, OIDC_JSON_PRESERVE_ORDER | OIDC_JSON_COMPACT));
526
527
759
  return TRUE;
528
759
}