Skip to content

Commit

Permalink
feat(compressedbinarydata): validation on compressedBinary data
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe authored May 7, 2021
1 parent 3d3426a commit c1a6991
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/documentBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class DocumentBuilder {
data: string,
compressionType: CompressionType
) {
//TODO: Validate data (length ? base64 ?)
this.validateCompressedBinaryData(data);
this.doc.compressedBinaryData = {
data,
compressionType,
Expand Down Expand Up @@ -194,6 +194,7 @@ export class DocumentBuilder {
const out = {
...this.doc,
...this.marshalMetadata(),
...this.marshalCompressedBinaryData(),
...this.marshalPermissions(),
};
delete out.metadata;
Expand All @@ -211,6 +212,16 @@ export class DocumentBuilder {
return out;
}

private marshalCompressedBinaryData() {
if (!this.doc.compressedBinaryData) {
return {};
}
return {
compressedBinaryData: this.doc.compressedBinaryData.data,
compressionType: this.doc.compressedBinaryData.compressionType,
};
}

private marshalPermissions() {
if (!this.doc.permissions) {
return '';
Expand Down Expand Up @@ -245,6 +256,13 @@ export class DocumentBuilder {
return validatedDate.toISOString();
}

private validateCompressedBinaryData(data: string) {
const isBase64 = Buffer.from(data, 'base64').toString('base64') === data;
if (!isBase64) {
throw 'Invalid compressedBinaryData: When using compressedBinaryData, the data must be base64 encoded.';
}
}

private setPermission(
securityIdentityBuilder: SecurityIdentityBuilder,
permissionSection: 'allowedPermissions' | 'deniedPermissions'
Expand Down

0 comments on commit c1a6991

Please sign in to comment.