Skip to content

Commit 26710e6

Browse files
authored
Use pure js crypto provider instead of web crypto provider (#256)
Also minor refactoring ot move json validation after rules
1 parent 82d5a08 commit 26710e6

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

schemaregistry/rules/encryption/tink/aes_siv.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import {Aead} from './aead';
77

88
// @ts-expect-error miscreant does not have types
9-
import {SIV, WebCryptoProvider} from "@hackbg/miscreant-esm";
10-
import * as crypto from 'crypto';
9+
import {SIV, SoftCryptoProvider} from "@hackbg/miscreant-esm";
1110

1211
/**
1312
* Implementation of AES-SIV.
@@ -22,16 +21,16 @@ export class AesSiv extends Aead {
2221
*/
2322
async encrypt(plaintext: Uint8Array, associatedData?: Uint8Array):
2423
Promise<Uint8Array> {
25-
let key = await SIV.importKey(this.key, "AES-CMAC-SIV", new WebCryptoProvider(crypto));
26-
return key.seal(plaintext, [associatedData]);
24+
let key = await SIV.importKey(this.key, "AES-CMAC-SIV", new SoftCryptoProvider());
25+
return key.seal(plaintext, associatedData != null ? [associatedData] : []);
2726
}
2827

2928
/**
3029
*/
3130
async decrypt(ciphertext: Uint8Array, associatedData?: Uint8Array):
3231
Promise<Uint8Array> {
33-
let key = await SIV.importKey(this.key, "AES-CMAC-SIV", new WebCryptoProvider(crypto));
34-
return key.open(ciphertext, [associatedData]);
32+
let key = await SIV.importKey(this.key, "AES-CMAC-SIV", new SoftCryptoProvider());
33+
return key.open(ciphertext, associatedData != null? [associatedData] : []);
3534
}
3635
}
3736

schemaregistry/serde/json.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,6 @@ export class JsonDeserializer extends Deserializer implements JsonSerde {
190190
}
191191

192192
const info = await this.getSchema(topic, payload)
193-
if ((this.conf as JsonSerdeConfig).validate) {
194-
const validate = await this.toValidateFunction(info)
195-
if (validate != null && !validate(JSON.parse(payload.subarray(5).toString()))) {
196-
throw new SerializationError('Invalid message')
197-
}
198-
199-
}
200193
const subject = this.subjectName(topic, info)
201194
const readerMeta = await this.getReaderSchema(subject)
202195
let migrations: Migration[] = []
@@ -215,6 +208,12 @@ export class JsonDeserializer extends Deserializer implements JsonSerde {
215208
target = info
216209
}
217210
msg = this.executeRules(subject, topic, RuleMode.READ, null, target, msg, null)
211+
if ((this.conf as JsonSerdeConfig).validate) {
212+
const validate = await this.toValidateFunction(info)
213+
if (validate != null && !validate(JSON.parse(msg))) {
214+
throw new SerializationError('Invalid message')
215+
}
216+
}
218217
return msg
219218
}
220219

schemaregistry/serde/serde.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export abstract class Serializer extends Serde {
272272
async getId(topic: string, msg: any, info?: SchemaInfo, format?: string): Promise<[number, SchemaInfo]> {
273273
let autoRegister = this.config().autoRegisterSchemas
274274
let useSchemaId = this.config().useSchemaId
275-
let useLatestWithMetadata = this.conf.useLatestWithMetadata
275+
let useLatestWithMetadata = this.config().useLatestWithMetadata
276276
let useLatest = this.config().useLatestVersion
277277
let normalizeSchema = this.config().normalizeSchemas
278278

0 commit comments

Comments
 (0)