asherah 1.3.13 → 1.3.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asherah",
3
- "version": "1.3.13",
3
+ "version": "1.3.15",
4
4
  "description": "Asherah envelope encryption and key rotation library",
5
5
  "exports": {
6
6
  "node-addons": "./dist/asherah.node"
@@ -30,6 +30,7 @@
30
30
  "src/cobhan_napi_interop.cc",
31
31
  "src/asherah.d.ts",
32
32
  "scripts/download-libraries.sh",
33
+ "scripts/build.sh",
33
34
  "SHA256SUMS",
34
35
  "SHA256SUMS-darwin",
35
36
  ".asherah-version"
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+
3
+ node-gyp configure && node-gyp build && mkdir -p dist/ && cp build/Release/asherah.node dist/asherah.node && cp src/asherah.d.ts dist/asherah.d.ts
package/src/asherah.cc CHANGED
@@ -1,9 +1,10 @@
1
1
  #include "../lib/libasherah.h"
2
- #include <iostream>
3
- #define NODE_ADDON_API_DISABLE_DEPRECATED
4
2
  #include "cobhan_napi_interop.h"
5
3
  #include "hints.h"
6
4
  #include "logging.h"
5
+ #include <iostream>
6
+ #define NODE_ADDON_API_DISABLE_DEPRECATED
7
+ #define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
7
8
  #include <napi.h>
8
9
 
9
10
  size_t max_stack_alloc_size = 2048;
@@ -45,7 +46,8 @@ void setup(const Napi::CallbackInfo &info) {
45
46
  Napi::String product_id = config_json.Get("ProductID").As<Napi::String>();
46
47
  Napi::String service_name = config_json.Get("ServiceName").As<Napi::String>();
47
48
 
48
- est_intermediate_key_overhead = product_id.Utf8Value().length() + service_name.Utf8Value().length();
49
+ est_intermediate_key_overhead =
50
+ product_id.Utf8Value().length() + service_name.Utf8Value().length();
49
51
 
50
52
  Napi::Value verbose = config_json.Get("Verbose");
51
53
  if (likely(verbose.IsBoolean())) {
@@ -82,7 +84,8 @@ void setup(const Napi::CallbackInfo &info) {
82
84
  config_cobhan_buffer = config_cobhan_buffer_unique_ptr.get();
83
85
  }
84
86
  if (unlikely(config_cobhan_buffer == nullptr)) {
85
- log_error_and_throw(env, "setup", "Failed to allocate config cobhan buffer");
87
+ log_error_and_throw(env, "setup",
88
+ "Failed to allocate config cobhan buffer");
86
89
  return;
87
90
  }
88
91
 
@@ -145,7 +148,7 @@ Napi::Value encrypt_to_json(Napi::Env &env, size_t partition_bytes,
145
148
  }
146
149
  if (unlikely(output_cobhan_buffer == nullptr)) {
147
150
  return log_error_and_throw(env, "encrypt_to_json",
148
- "Failed to allocate cobhan output buffer");
151
+ "Failed to allocate cobhan output buffer");
149
152
  }
150
153
 
151
154
  if (unlikely(verbose_flag)) {
@@ -195,7 +198,7 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
195
198
  partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
196
199
  if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
197
200
  return log_error_and_throw(env, "encrypt",
198
- "Failed to get partition_id utf8 length");
201
+ "Failed to get partition_id utf8 length");
199
202
  }
200
203
  if (unlikely(partition_utf8_byte_length == 0)) {
201
204
  return log_error_and_throw(env, "encrypt", "partition_id is empty");
@@ -221,7 +224,7 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
221
224
  }
222
225
  if (unlikely(partition_id_cobhan_buffer == nullptr)) {
223
226
  return log_error_and_throw(env, "encrypt",
224
- "Failed to allocate partitionId cobhan buffer");
227
+ "Failed to allocate partitionId cobhan buffer");
225
228
  }
226
229
 
227
230
  // Copy
@@ -231,7 +234,7 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
231
234
  &partition_copied_bytes);
232
235
  if (unlikely(partition_id_cobhan_buffer == nullptr)) {
233
236
  return log_error_and_throw(env, "encrypt",
234
- "Failed to copy partitionId to cobhan buffer");
237
+ "Failed to copy partitionId to cobhan buffer");
235
238
  }
236
239
 
237
240
  // Determine size
@@ -269,11 +272,18 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
269
272
  copy_nbuffer_to_cbuffer(env, input_napi_buffer, input_cobhan_buffer);
270
273
  if (unlikely(input_cobhan_buffer == nullptr)) {
271
274
  return log_error_and_throw(env, "encrypt",
272
- "Failed to copy input buffer to cobhan buffer");
275
+ "Failed to copy input buffer to cobhan buffer");
273
276
  }
274
277
 
275
- return encrypt_to_json(env, partition_copied_bytes, input_byte_length,
276
- partition_id_cobhan_buffer, input_cobhan_buffer);
278
+ Napi::Value output =
279
+ encrypt_to_json(env, partition_copied_bytes, input_byte_length,
280
+ partition_id_cobhan_buffer, input_cobhan_buffer);
281
+
282
+ if (unlikely(verbose_flag)) {
283
+ debug_log("encrypt", "finished");
284
+ }
285
+
286
+ return output;
277
287
  }
278
288
 
279
289
  Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
@@ -288,7 +298,8 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
288
298
  }
289
299
 
290
300
  if (unlikely(info.Length() < 2)) {
291
- return log_error_and_throw(env, "encrypt_string", "Wrong number of arguments");
301
+ return log_error_and_throw(env, "encrypt_string",
302
+ "Wrong number of arguments");
292
303
  }
293
304
 
294
305
  if (unlikely(!info[0].IsString() || !info[1].IsString())) {
@@ -301,7 +312,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
301
312
  partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
302
313
  if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
303
314
  return log_error_and_throw(env, "encrypt_string",
304
- "Failed to get partition_id utf8 length");
315
+ "Failed to get partition_id utf8 length");
305
316
  }
306
317
  if (unlikely(partition_utf8_byte_length == 0)) {
307
318
  return log_error_and_throw(env, "encrypt_string", "partition_id is empty");
@@ -327,7 +338,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
327
338
  }
328
339
  if (unlikely(partition_id_cobhan_buffer == nullptr)) {
329
340
  return log_error_and_throw(env, "encrypt_string",
330
- "Failed to allocate partitionId cobhan buffer");
341
+ "Failed to allocate partitionId cobhan buffer");
331
342
  }
332
343
 
333
344
  // Copy
@@ -337,7 +348,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
337
348
  &partition_copied_bytes);
338
349
  if (unlikely(partition_id_cobhan_buffer == nullptr)) {
339
350
  return log_error_and_throw(env, "encrypt_string",
340
- "Failed to copy partitionId to cobhan buffer");
351
+ "Failed to copy partitionId to cobhan buffer");
341
352
  }
342
353
 
343
354
  // Determine size
@@ -346,7 +357,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
346
357
  input_utf8_byte_length = nstring_utf8_byte_length(env, input);
347
358
  if (unlikely(input_utf8_byte_length == (size_t)(-1))) {
348
359
  return log_error_and_throw(env, "encrypt_string",
349
- "Failed to get input utf8 length");
360
+ "Failed to get input utf8 length");
350
361
  }
351
362
  if (unlikely(input_utf8_byte_length == 0)) {
352
363
  return log_error_and_throw(env, "encrypt_string", "input is empty");
@@ -371,7 +382,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
371
382
  }
372
383
  if (unlikely(input_cobhan_buffer == nullptr)) {
373
384
  return log_error_and_throw(env, "encrypt_string",
374
- "Failed to allocate input cobhan buffer");
385
+ "Failed to allocate input cobhan buffer");
375
386
  }
376
387
 
377
388
  // Copy
@@ -381,11 +392,18 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
381
392
  input_cobhan_buffer, &input_copied_bytes);
382
393
  if (unlikely(input_cobhan_buffer == nullptr)) {
383
394
  return log_error_and_throw(env, "encrypt_string",
384
- "Failed to copy input to cobhan buffer");
395
+ "Failed to copy input to cobhan buffer");
396
+ }
397
+
398
+ Napi::Value output =
399
+ encrypt_to_json(env, partition_copied_bytes, input_utf8_byte_length,
400
+ partition_id_cobhan_buffer, input_cobhan_buffer);
401
+
402
+ if (unlikely(verbose_flag)) {
403
+ debug_log("encrypt_string", "finished");
385
404
  }
386
405
 
387
- return encrypt_to_json(env, partition_copied_bytes, input_utf8_byte_length,
388
- partition_id_cobhan_buffer, input_cobhan_buffer);
406
+ return output;
389
407
  }
390
408
 
391
409
  Napi::Value decrypt(const Napi::CallbackInfo &info) {
@@ -413,7 +431,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
413
431
  partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
414
432
  if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
415
433
  return log_error_and_throw(env, "decrypt",
416
- "Failed to get partition_id utf8 length");
434
+ "Failed to get partition_id utf8 length");
417
435
  }
418
436
  if (unlikely(partition_utf8_byte_length == 0)) {
419
437
  return log_error_and_throw(env, "decrypt", "partition_id is empty");
@@ -439,7 +457,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
439
457
  }
440
458
  if (unlikely(partition_id_cobhan_buffer == nullptr)) {
441
459
  return log_error_and_throw(env, "decrypt",
442
- "Failed to allocate partition_id cobhan buffer");
460
+ "Failed to allocate partition_id cobhan buffer");
443
461
  }
444
462
 
445
463
  // Copy
@@ -448,7 +466,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
448
466
  &partition_copied_bytes);
449
467
  if (unlikely(partition_id_cobhan_buffer == nullptr)) {
450
468
  return log_error_and_throw(env, "decrypt",
451
- "Failed to copy partition_id to cobhan buffer");
469
+ "Failed to copy partition_id to cobhan buffer");
452
470
  }
453
471
 
454
472
  // Determine size
@@ -456,7 +474,8 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
456
474
  Napi::String input = info[1].As<Napi::String>();
457
475
  input_utf8_byte_length = nstring_utf8_byte_length(env, input);
458
476
  if (unlikely(input_utf8_byte_length == (size_t)(-1))) {
459
- return log_error_and_throw(env, "decrypt", "Failed to get input utf8 length");
477
+ return log_error_and_throw(env, "decrypt",
478
+ "Failed to get input utf8 length");
460
479
  }
461
480
  if (unlikely(input_utf8_byte_length == 0)) {
462
481
  return log_error_and_throw(env, "decrypt", "input is empty");
@@ -486,7 +505,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
486
505
  }
487
506
  if (unlikely(input_cobhan_buffer == nullptr)) {
488
507
  return log_error_and_throw(env, "decrypt",
489
- "Failed to allocate input cobhan buffer");
508
+ "Failed to allocate input cobhan buffer");
490
509
  }
491
510
 
492
511
  // Copy
@@ -496,7 +515,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
496
515
  input_cobhan_buffer, &input_copied_bytes);
497
516
  if (unlikely(input_cobhan_buffer == nullptr)) {
498
517
  return log_error_and_throw(env, "decrypt",
499
- "Failed to copy input to cobhan buffer");
518
+ "Failed to copy input to cobhan buffer");
500
519
  }
501
520
 
502
521
  char *output_cobhan_buffer;
@@ -517,7 +536,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
517
536
  }
518
537
  if (unlikely(output_cobhan_buffer == nullptr)) {
519
538
  return log_error_and_throw(env, "decrypt",
520
- "Failed to allocate cobhan output buffer");
539
+ "Failed to allocate cobhan output buffer");
521
540
  }
522
541
 
523
542
  if (unlikely(verbose_flag)) {
@@ -538,7 +557,13 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
538
557
  return log_error_and_throw(env, "decrypt", std::to_string(result));
539
558
  }
540
559
 
541
- return cbuffer_to_nbuffer(env, output_cobhan_buffer);
560
+ Napi::Value output = cbuffer_to_nbuffer(env, output_cobhan_buffer);
561
+
562
+ if (unlikely(verbose_flag)) {
563
+ debug_log("decrypt", "finished");
564
+ }
565
+
566
+ return output;
542
567
  }
543
568
 
544
569
  Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
@@ -553,7 +578,8 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
553
578
  }
554
579
 
555
580
  if (unlikely(info.Length() < 2)) {
556
- return log_error_and_throw(env, "decrypt_string", "Wrong number of arguments");
581
+ return log_error_and_throw(env, "decrypt_string",
582
+ "Wrong number of arguments");
557
583
  }
558
584
 
559
585
  if (unlikely(!info[0].IsString() || !info[1].IsString())) {
@@ -566,7 +592,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
566
592
  partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
567
593
  if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
568
594
  return log_error_and_throw(env, "decrypt_string",
569
- "Failed to get partition_id utf8 length");
595
+ "Failed to get partition_id utf8 length");
570
596
  }
571
597
  if (unlikely(partition_utf8_byte_length == 0)) {
572
598
  return log_error_and_throw(env, "decrypt_string", "partition_id is empty");
@@ -592,7 +618,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
592
618
  }
593
619
  if (unlikely(partition_id_cobhan_buffer == nullptr)) {
594
620
  return log_error_and_throw(env, "decrypt_string",
595
- "Failed to allocate partitionId cobhan buffer");
621
+ "Failed to allocate partitionId cobhan buffer");
596
622
  }
597
623
 
598
624
  // Copy
@@ -602,7 +628,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
602
628
  &partition_copied_bytes);
603
629
  if (unlikely(partition_id_cobhan_buffer == nullptr)) {
604
630
  return log_error_and_throw(env, "decrypt_string",
605
- "Failed to copy partitionId to cobhan buffer");
631
+ "Failed to copy partitionId to cobhan buffer");
606
632
  }
607
633
 
608
634
  // Determine size
@@ -611,7 +637,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
611
637
  input_utf8_byte_length = nstring_utf8_byte_length(env, input);
612
638
  if (unlikely(input_utf8_byte_length == (size_t)(-1))) {
613
639
  return log_error_and_throw(env, "decrypt_string",
614
- "Failed to get input utf8 length");
640
+ "Failed to get input utf8 length");
615
641
  }
616
642
  if (unlikely(input_utf8_byte_length == 0)) {
617
643
  return log_error_and_throw(env, "decrypt_string", "input is empty");
@@ -636,7 +662,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
636
662
  }
637
663
  if (unlikely(input_cobhan_buffer == nullptr)) {
638
664
  return log_error_and_throw(env, "decrypt_string",
639
- "Failed to allocate input cobhan buffer");
665
+ "Failed to allocate input cobhan buffer");
640
666
  }
641
667
 
642
668
  // Copy
@@ -646,7 +672,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
646
672
  input_cobhan_buffer, &input_copied_bytes);
647
673
  if (unlikely(input_cobhan_buffer == nullptr)) {
648
674
  return log_error_and_throw(env, "decrypt_string",
649
- "Failed to copy input to cobhan buffer");
675
+ "Failed to copy input to cobhan buffer");
650
676
  }
651
677
 
652
678
  char *output_cobhan_buffer;
@@ -667,7 +693,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
667
693
  }
668
694
  if (unlikely(output_cobhan_buffer == nullptr)) {
669
695
  return log_error_and_throw(env, "decrypt_string",
670
- "Failed to allocate cobhan output buffer");
696
+ "Failed to allocate cobhan output buffer");
671
697
  }
672
698
 
673
699
  if (unlikely(verbose_flag)) {
@@ -689,6 +715,11 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
689
715
  }
690
716
 
691
717
  Napi::Value output = cbuffer_to_nstring(env, output_cobhan_buffer);
718
+
719
+ if (unlikely(verbose_flag)) {
720
+ debug_log("decrypt_string", "finished");
721
+ }
722
+
692
723
  return output;
693
724
  }
694
725
 
@@ -720,7 +751,7 @@ void set_max_stack_alloc_item_size(const Napi::CallbackInfo &info) {
720
751
 
721
752
  if (unlikely(info.Length() < 1)) {
722
753
  log_error_and_throw(env, "set_max_stack_alloc_item_size",
723
- "Wrong number of arguments");
754
+ "Wrong number of arguments");
724
755
  return;
725
756
  }
726
757
 
@@ -738,7 +769,7 @@ void set_safety_padding_overhead(const Napi::CallbackInfo &info) {
738
769
 
739
770
  if (unlikely(info.Length() < 1)) {
740
771
  log_error_and_throw(env, "set_safety_padding_overhead",
741
- "Wrong number of arguments");
772
+ "Wrong number of arguments");
742
773
  return;
743
774
  }
744
775
 
@@ -1,10 +1,11 @@
1
1
  #ifndef COBHAN_NAPI_INTEROP_H
2
2
  #define COBHAN_NAPI_INTEROP_H
3
+ #include "hints.h"
4
+ #include "logging.h"
3
5
  #include <string>
4
6
  #define NODE_ADDON_API_DISABLE_DEPRECATED
7
+ #define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
5
8
  #include <napi.h>
6
- #include "logging.h"
7
- #include "hints.h"
8
9
 
9
10
  extern size_t est_intermediate_key_overhead;
10
11
  extern size_t safety_padding_bytes;
@@ -66,14 +67,14 @@ std::string napi_status_to_string(napi_status status) {
66
67
  }
67
68
  }
68
69
 
69
- __attribute__((always_inline)) inline size_t
70
+ __attribute__((always_inline)) inline int32_t
70
71
  cbuffer_byte_length(char *cobhan_buffer) {
71
- return *((int *)cobhan_buffer);
72
+ return *((int32_t *)cobhan_buffer);
72
73
  }
73
74
 
74
75
  __attribute__((always_inline)) inline Napi::Value
75
76
  log_error_and_throw(Napi::Env &env, const char *function_name,
76
- std::string error_msg) {
77
+ std::string error_msg) {
77
78
  error_log(function_name, error_msg);
78
79
  Napi::Error::New(env, function_name + (": " + error_msg))
79
80
  .ThrowAsJavaScriptException();
@@ -143,15 +144,21 @@ __attribute__((always_inline)) inline Napi::Value
143
144
  cbuffer_to_nstring(Napi::Env &env, char *cobhan_buffer) {
144
145
  napi_value output;
145
146
 
147
+ int32_t cobhan_buffer_size_bytes = cbuffer_byte_length(cobhan_buffer);
148
+ if (cobhan_buffer_size_bytes <= 0) {
149
+ return log_error_and_throw(env, "cbuffer_to_nstring",
150
+ "Invalid cobhan buffer byte length");
151
+ }
152
+
146
153
  // Using C function because it allows length delimited input
147
154
  napi_status status = napi_create_string_utf8(
148
155
  env, ((const char *)cobhan_buffer) + cobhan_header_size_bytes,
149
- cbuffer_byte_length(cobhan_buffer), &output);
156
+ cobhan_buffer_size_bytes, &output);
150
157
 
151
158
  if (unlikely(status != napi_ok)) {
152
159
  return log_error_and_throw(env, "cbuffer_to_nstring",
153
- "napi_create_string_utf8 failed: " +
154
- napi_status_to_string(status));
160
+ "napi_create_string_utf8 failed: " +
161
+ napi_status_to_string(status));
155
162
  }
156
163
 
157
164
  return Napi::String(env, output);
@@ -165,8 +172,8 @@ nstring_utf8_byte_length(Napi::Env &env, Napi::String &str) {
165
172
  status = napi_get_value_string_utf8(env, str, nullptr, 0, &utf8_length);
166
173
  if (unlikely(status != napi_ok)) {
167
174
  log_error_and_throw(env, "nstring_utf8_length",
168
- "napi_get_value_string_utf8 length check failed: " +
169
- napi_status_to_string(status));
175
+ "napi_get_value_string_utf8 length check failed: " +
176
+ napi_status_to_string(status));
170
177
  return (size_t)(-1);
171
178
  }
172
179
 
@@ -179,9 +186,15 @@ copy_nstring_to_cbuffer(Napi::Env &env, Napi::String &str,
179
186
  size_t *byte_length = nullptr) {
180
187
 
181
188
  size_t cobhan_buffer_size_bytes = cbuffer_byte_length(cobhan_buffer);
189
+ if (unlikely(cobhan_buffer_size_bytes <= 0)) {
190
+ log_error_and_throw(env, "copy_nstring_to_cbuffer",
191
+ "Invalid cobhan buffer byte length");
192
+ return nullptr;
193
+ }
194
+
182
195
  if (cobhan_buffer_size_bytes < str_utf8_byte_length) {
183
196
  log_error_and_throw(env, "copy_nstring_to_cbuffer",
184
- "String too large for cobhan buffer");
197
+ "String too large for cobhan buffer");
185
198
  return nullptr;
186
199
  }
187
200
 
@@ -190,21 +203,21 @@ copy_nstring_to_cbuffer(Napi::Env &env, Napi::String &str,
190
203
  // NOTE: This implementation relies on the additional byte that is reserved
191
204
  // upon allocation for a NULL delimiter as methods like
192
205
  // napi_get_value_string_utf8 append a NULL delimiter
193
- status = napi_get_value_string_utf8(
194
- env, str, cobhan_buffer + cobhan_header_size_bytes,
195
- str_utf8_byte_length + 1, &copied_bytes);
206
+ status = napi_get_value_string_utf8(env, str,
207
+ cobhan_buffer + cobhan_header_size_bytes,
208
+ str_utf8_byte_length + 1, &copied_bytes);
196
209
  if (unlikely(status != napi_ok)) {
197
210
  log_error_and_throw(env, "copy_nstring_to_cbuffer",
198
- "Napi utf8 string conversion failure: " +
199
- napi_status_to_string(status));
211
+ "Napi utf8 string conversion failure: " +
212
+ napi_status_to_string(status));
200
213
  return nullptr;
201
214
  }
202
215
 
203
216
  if (unlikely(copied_bytes != str_utf8_byte_length)) {
204
217
  log_error_and_throw(env, "copy_nstring_to_cbuffer",
205
- "Did not copy expected number of bytes " +
206
- std::to_string(str_utf8_byte_length) + " copied " +
207
- std::to_string(copied_bytes));
218
+ "Did not copy expected number of bytes " +
219
+ std::to_string(str_utf8_byte_length) + " copied " +
220
+ std::to_string(copied_bytes));
208
221
  return nullptr;
209
222
  }
210
223
 
@@ -218,10 +231,19 @@ copy_nstring_to_cbuffer(Napi::Env &env, Napi::String &str,
218
231
  __attribute__((always_inline)) inline char *
219
232
  copy_nbuffer_to_cbuffer(Napi::Env &env, Napi::Buffer<unsigned char> &nbuffer,
220
233
  char *cobhan_buffer) {
234
+
235
+ int32_t cobhan_buffer_size_bytes = cbuffer_byte_length(cobhan_buffer);
236
+ if (unlikely(cobhan_buffer_size_bytes <= 0)) {
237
+ log_error_and_throw(env, "copy_nbuffer_to_cbuffer",
238
+ "Invalid cobhan buffer byte length");
239
+ return nullptr;
240
+ }
241
+
221
242
  size_t nbuffer_byte_length = nbuffer.ByteLength();
222
- if (cbuffer_byte_length(cobhan_buffer) < nbuffer_byte_length) {
243
+ if (nbuffer_byte_length > INT32_MAX ||
244
+ cobhan_buffer_size_bytes < (int32_t)nbuffer_byte_length) {
223
245
  log_error_and_throw(env, "copy_nbuffer_to_cbuffer",
224
- "Buffer too large for cobhan buffer");
246
+ "Buffer too large for cobhan buffer");
225
247
  return nullptr;
226
248
  }
227
249
  memcpy(cobhan_buffer + cobhan_header_size_bytes, nbuffer.Data(),
@@ -230,11 +252,35 @@ copy_nbuffer_to_cbuffer(Napi::Env &env, Napi::Buffer<unsigned char> &nbuffer,
230
252
  return cobhan_buffer;
231
253
  }
232
254
 
233
- __attribute__((always_inline)) inline Napi::Buffer<unsigned char>
255
+ __attribute__((always_inline)) inline Napi::Value
234
256
  cbuffer_to_nbuffer(Napi::Env &env, char *cobhan_buffer) {
235
- return Napi::Buffer<unsigned char>::Copy(
257
+ int32_t cobhan_buffer_byte_length = cbuffer_byte_length(cobhan_buffer);
258
+ if (unlikely(cobhan_buffer_byte_length <= 0)) {
259
+ return log_error_and_throw(env, "cbuffer_to_nbuffer",
260
+ "Invalid cobhan buffer byte length");
261
+ }
262
+
263
+ if (unlikely(verbose_flag)) {
264
+ debug_log("cbuffer_to_nbuffer",
265
+ "cbuffer_byte_length: " +
266
+ std::to_string(cobhan_buffer_byte_length));
267
+ }
268
+
269
+ if (unlikely(cobhan_buffer_byte_length <= 0)) {
270
+ log_error_and_throw(env, "cbuffer_to_nbuffer",
271
+ "Invalid cobhan buffer byte length");
272
+ }
273
+
274
+ Napi::Buffer nbuffer = Napi::Buffer<unsigned char>::Copy(
236
275
  env, ((unsigned char *)cobhan_buffer) + cobhan_header_size_bytes,
237
- cbuffer_byte_length(cobhan_buffer));
276
+ cobhan_buffer_byte_length);
277
+
278
+ if (unlikely(verbose_flag)) {
279
+ debug_log("cbuffer_to_nbuffer",
280
+ "nbuffer.ByteLength(): " + std::to_string(nbuffer.ByteLength()));
281
+ }
282
+
283
+ return nbuffer;
238
284
  }
239
285
 
240
286
  #endif
package/src/logging.h CHANGED
@@ -1,8 +1,9 @@
1
1
  #ifndef LOGGING_H
2
2
  #define LOGGING_H
3
+ #include "hints.h"
3
4
  #include <cstdint>
4
5
  #include <string>
5
- #include "hints.h"
6
+ #include <iostream>
6
7
 
7
8
  extern int32_t verbose_flag;
8
9