Skip to content

Commit

Permalink
fix(sdk): Remove stray call to node:Buffer
Browse files Browse the repository at this point in the history
Update assertion.ts

fix(lib): Removes all refs to node:buffer I could find

- removes the polyfill from the web-test-runner, to make sure we aren't accidentally including it (at least in things that covers)
- updates all .spec and .test files, in turn
- renames a bunch of things called `buffer` to something else so I can search for this easier

Update cli.ts
  • Loading branch information
dmihalcik-virtru committed Oct 25, 2024
1 parent 406e6fb commit 00a2a10
Show file tree
Hide file tree
Showing 23 changed files with 125 additions and 234 deletions.
53 changes: 4 additions & 49 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { CLIError, Level, log } from './logger.js';
import { webcrypto } from 'crypto';
import { attributeFQNsAsValues } from '@opentdf/client/nano';
import { base64 } from '@opentdf/client/encodings';

type AuthToProcess = {
auth?: string;
Expand All @@ -37,8 +38,9 @@ const bindingTypes = ['ecdsa', 'gmac'];
const containerTypes = ['tdf3', 'nano', 'dataset', 'ztdf'];

const parseJwt = (jwt: string, field = 1) => {
return JSON.parse(Buffer.from(jwt.split('.')[field], 'base64').toString());
return JSON.parse(base64.decode(jwt.split('.')[field]));
};

const parseJwtComplete = (jwt: string) => {
return { header: parseJwt(jwt, 0), payload: parseJwt(jwt) };
};
Expand Down Expand Up @@ -413,9 +415,9 @@ export const handleArgs = (args: string[]) => {

log('DEBUG', 'Handle output.');
if (argv.output) {
await writeFile(argv.output, Buffer.from(plaintext));
await writeFile(argv.output, new Uint8Array(plaintext));
} else {
console.log(Buffer.from(plaintext).toString('utf8'));
console.log(new TextDecoder().decode(plaintext));
}
}
const lastRequest = authProvider.requestLog[authProvider.requestLog.length - 1];
Expand Down Expand Up @@ -503,9 +505,9 @@ export const handleArgs = (args: string[]) => {

log('DEBUG', `Handle cyphertext output ${JSON.stringify(cyphertext)}`);
if (argv.output) {
await writeFile(argv.output, Buffer.from(cyphertext));
await writeFile(argv.output, new Uint8Array(cyphertext));
} else {
console.log(Buffer.from(cyphertext).toString('base64'));
console.log(base64.encodeArrayBuffer(cyphertext));
}
}
}
Expand Down
24 changes: 1 addition & 23 deletions lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"axios-retry": "^3.9.0",
"base64-js": "^1.5.1",
"browser-fs-access": "^0.34.1",
"buffer": "^6.0.3",
"buffer-crc32": "^0.2.13",
"dpop": "^1.2.0",
"eventemitter3": "^5.0.1",
Expand Down
20 changes: 10 additions & 10 deletions lib/src/nanotdf/models/Header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,28 +232,28 @@ export default class Header {
/**
* Copy the contents of the header to buffer
*/
copyToBuffer(buffer: Uint8Array): void {
if (this.length > buffer.length) {
copyToBuffer(target: Uint8Array): void {
if (this.length > target.length) {
throw new InvalidFileError('invalid buffer size to copy tdf header');
}

let offset = 0;

// Write Magic number and version
buffer.set(this.magicNumberVersion, 0);
target.set(this.magicNumberVersion, 0);
offset += this.magicNumberVersion.length;

// Write kas resource locator
const kasResourceLocatorBuf = this.kas.toBuffer();
buffer.set(kasResourceLocatorBuf, offset);
target.set(kasResourceLocatorBuf, offset);
offset += kasResourceLocatorBuf.length;

// Write ECC & Binding Mode
const ecdsaBinding = this.useECDSABinding ? 1 : 0;
const eccBingingMode = (ecdsaBinding << 7) | this.ephemeralCurveName;
const eccBingingModeAsByte = new Uint8Array(1);
eccBingingModeAsByte[0] = eccBingingMode;
buffer.set(eccBingingModeAsByte, offset);
target.set(eccBingingModeAsByte, offset);
offset += eccBingingModeAsByte.length;

// Write symmetric & payload config
Expand All @@ -262,16 +262,16 @@ export default class Header {
(isSignatureEnable << 7) | this.signatureCurveName | this.symmetricCipher;
const symmetricPayloadConfigAsByte = new Uint8Array(1);
symmetricPayloadConfigAsByte[0] = symmetricPayloadConfig;
buffer.set(symmetricPayloadConfigAsByte, offset);
target.set(symmetricPayloadConfigAsByte, offset);
offset += symmetricPayloadConfigAsByte.length;

// Write the policy
const policyBuffer = this.policy.toBuffer();
buffer.set(policyBuffer, offset);
target.set(policyBuffer, offset);
offset += policyBuffer.length;

// Write the ephemeral public key
buffer.set(this.ephemeralPublicKey, offset);
target.set(this.ephemeralPublicKey, offset);
}

/**
Expand Down Expand Up @@ -304,8 +304,8 @@ export default class Header {
*/
toBuffer(): ArrayBuffer {
const arrayBuffer = new ArrayBuffer(this.length);
const buffer = new Uint8Array(arrayBuffer);
this.copyToBuffer(buffer);
const target = new Uint8Array(arrayBuffer);
this.copyToBuffer(target);
return arrayBuffer;
}

Expand Down
12 changes: 6 additions & 6 deletions lib/src/nanotdf/models/Payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ export default class Payload {
/**
* Copy the contents of the signature to buffer
*/
copyToBuffer(buffer: Uint8Array): void {
if (this.length > buffer.length) {
copyToBuffer(target: Uint8Array): void {
if (this.length > target.length) {
throw new Error('internal: invalid buffer size to copy payload');
}

Expand All @@ -188,9 +188,9 @@ export default class Payload {
payloadSizeAsBg[1] = lengthAsUint24[1];
payloadSizeAsBg[2] = lengthAsUint24[0];

buffer.set(payloadSizeAsBg, 0);
buffer.set(this.iv, payloadSizeAsBg.length);
buffer.set(this.ciphertext, payloadSizeAsBg.length + this.iv.length);
buffer.set(this.authTag, payloadSizeAsBg.length + this.iv.length + this.ciphertext.length);
target.set(payloadSizeAsBg, 0);
target.set(this.iv, payloadSizeAsBg.length);
target.set(this.ciphertext, payloadSizeAsBg.length + this.iv.length);
target.set(this.authTag, payloadSizeAsBg.length + this.iv.length + this.ciphertext.length);
}
}
12 changes: 6 additions & 6 deletions lib/src/nanotdf/models/Policy/EmbeddedPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ class EmbeddedPolicy extends AbstractPolicy implements EmbeddedPolicyInterface {
* Return the content of the policy
*/
override toBuffer(): Uint8Array {
const buffer = new Uint8Array(this.getLength());
const target = new Uint8Array(this.getLength());

if (this.content.length > EmbeddedPolicy.MAX_POLICY_SIZE) {
throw new ConfigurationError("TDF Policy can't be more that 2^16");
}

buffer.set([this.type], 0);
target.set([this.type], 0);

// Write the policy length, assuming the host system is little endian
// TODO: There should be better way to convert to big endian
Expand All @@ -86,15 +86,15 @@ class EmbeddedPolicy extends AbstractPolicy implements EmbeddedPolicyInterface {
const policyContentSizeAsBg = new Uint8Array(2);
policyContentSizeAsBg[0] = temp[1];
policyContentSizeAsBg[1] = temp[0];
buffer.set(policyContentSizeAsBg, 1);
target.set(policyContentSizeAsBg, 1);

// Write the policy content
buffer.set(this.content, policyContentSizeAsBg.length + 1);
target.set(this.content, policyContentSizeAsBg.length + 1);

// Write the binding.
buffer.set(this.binding, this.content.length + policyContentSizeAsBg.length + 1);
target.set(this.binding, this.content.length + policyContentSizeAsBg.length + 1);

return buffer;
return target;
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/src/nanotdf/models/Policy/RemotePolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ class RemotePolicy extends AbstractPolicy implements RemotePolicyInterface {
* Return the content of the policy
*/
override toBuffer(): Uint8Array {
const buffer = new Uint8Array(this.getLength());
const target = new Uint8Array(this.getLength());

buffer.set([PolicyTypeEnum.Remote], 0);
target.set([PolicyTypeEnum.Remote], 0);

// Write the remote policy location
const resourceLocatorAsBuf = this.remotePolicy.toBuffer();
buffer.set(resourceLocatorAsBuf, 1);
target.set(resourceLocatorAsBuf, 1);

// Write the binding.
buffer.set(this.binding, resourceLocatorAsBuf.length + 1);
target.set(this.binding, resourceLocatorAsBuf.length + 1);

return buffer;
return target;
}
}

Expand Down
12 changes: 6 additions & 6 deletions lib/src/nanotdf/models/ResourceLocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default class ResourceLocator {
* Return the contents of the Resource Locator in buffer
*/
toBuffer(): Uint8Array {
const buffer = new Uint8Array(ResourceLocator.BODY_OFFSET + this.body.length + this.idType);
const target = new Uint8Array(ResourceLocator.BODY_OFFSET + this.body.length + this.idType);
let idTypeNibble = 0;
switch (this.idType) {
case ResourceLocatorIdentifierEnum.TwoBytes:
Expand All @@ -191,13 +191,13 @@ export default class ResourceLocator {
idTypeNibble = ResourceLocator.IDENTIFIER_32_BYTE;
break;
}
buffer.set([this.protocol | idTypeNibble], ResourceLocator.PROTOCOL_OFFSET);
buffer.set([this.lengthOfBody], ResourceLocator.LENGTH_OFFSET);
buffer.set(new TextEncoder().encode(this.body), ResourceLocator.BODY_OFFSET);
target.set([this.protocol | idTypeNibble], ResourceLocator.PROTOCOL_OFFSET);
target.set([this.lengthOfBody], ResourceLocator.LENGTH_OFFSET);
target.set(new TextEncoder().encode(this.body), ResourceLocator.BODY_OFFSET);
if (this.id) {
buffer.set(new TextEncoder().encode(this.id), ResourceLocator.BODY_OFFSET + this.body.length);
target.set(new TextEncoder().encode(this.id), ResourceLocator.BODY_OFFSET + this.body.length);
}
return buffer;
return target;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions lib/src/nanotdf/models/Signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ export default class Signature {
/**
* Copy the contents of the signature to buffer
*/
copyToBuffer(buffer: Uint8Array): void {
if (this.length > buffer.length) {
copyToBuffer(target: Uint8Array): void {
if (this.length > target.length) {
throw new ConfigurationError('Invalid buffer size to copy signature');
}

buffer.set(this.publicKey, 0);
buffer.set(this.signature, this.publicKey.length);
target.set(this.publicKey, 0);
target.set(this.signature, this.publicKey.length);
}
}
8 changes: 4 additions & 4 deletions lib/tdf3/src/ciphers/aes-gcm-cipher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ type ProcessGcmPayload = {
payloadAuthTag: Binary;
};
// Should this be a Binary, Buffer, or... both?
function processGcmPayload(buffer: ArrayBuffer): ProcessGcmPayload {
function processGcmPayload(source: ArrayBuffer): ProcessGcmPayload {
// Read the 12 byte IV from the beginning of the stream
const payloadIv = Binary.fromArrayBuffer(buffer.slice(0, 12));
const payloadIv = Binary.fromArrayBuffer(source.slice(0, 12));

// Slice the final 16 bytes of the buffer for the authentication tag
const payloadAuthTag = Binary.fromArrayBuffer(buffer.slice(-16));
const payloadAuthTag = Binary.fromArrayBuffer(source.slice(-16));

return {
payload: Binary.fromArrayBuffer(buffer.slice(12, -16)),
payload: Binary.fromArrayBuffer(source.slice(12, -16)),
payloadIv,
payloadAuthTag,
};
Expand Down
Loading

0 comments on commit 00a2a10

Please sign in to comment.