Skip to content

Commit 1beb02c

Browse files
chore!(sdk): Remove html wrapper creation (#406)
- still allow reading html wrapped files, for transitional support
1 parent a5c1167 commit 1beb02c

File tree

14 files changed

+67
-354
lines changed

14 files changed

+67
-354
lines changed

lib/tdf3/src/client/builders.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,22 @@ export type EncryptParams = {
4040
scope?: Scope;
4141
metadata?: Metadata;
4242
keypair?: CryptoKeyPair;
43-
// Deprecated: Only offline more is currently supported
44-
offline?: boolean;
4543
windowSize?: number;
46-
asHtml?: boolean;
4744
getPolicyId?: () => Scope['policyId'];
4845
mimeType?: string;
4946
payloadKey?: Binary;
5047
keyMiddleware?: EncryptKeyMiddleware;
5148
splitPlan?: SplitStep[];
5249
streamMiddleware?: EncryptStreamMiddleware;
5350
assertionConfigs?: AssertionConfig[];
51+
52+
// Unsupported
53+
asHtml?: boolean;
54+
// Unsupported
55+
offline?: boolean;
5456
};
5557

5658
// 'Readonly<EncryptParams>': scope, metadata, offline, windowSize, asHtml
57-
5859
// deep copy is expensive, could be faster is Immer used, but to keep SDK work
5960
// stable we can just make this object readonly
6061
function freeze<Type>(obj: Type): Readonly<Type> {
@@ -75,9 +76,7 @@ class EncryptParamsBuilder {
7576
attributes: [],
7677
},
7778
keypair: undefined,
78-
offline: false,
7979
windowSize: DEFAULT_SEGMENT_SIZE,
80-
asHtml: false,
8180
assertionConfigs: [],
8281
}
8382
) {
@@ -382,37 +381,24 @@ class EncryptParamsBuilder {
382381
}
383382

384383
/**
385-
* Whether the encrypted data should be formatted using html. This allows authorized users to
386-
* double click and read using the Virtru Secure Reader, at the cost of reduced space efficiency.
387-
* <br/><br/>
388-
* This is enabled by default.
389-
* @return {boolean} true if the encrypted data will be in html format.
384+
* @deprecated This feature is not supported
390385
*/
391386
hasHtmlFormat(): boolean {
392-
return !!this._params.asHtml;
387+
return false;
393388
}
394389

395390
/**
396-
* Specify that the encrypted data should be formatted using html. This allows authorized users to
397-
* double click and read using the Virtru Secure Reader, at the cost of reduced space efficiency.
398-
* <br/><br/>
399-
* This is enabled by default.
391+
* @deprecated This feature is not supported
400392
*/
401393
setHtmlFormat() {
402-
this._params.asHtml = true;
394+
throw new ConfigurationError('HTML format is not supported');
403395
}
404396

405397
/**
406-
* Specify that the encrypted data should be formatted using html. This allows authorized users to
407-
* double click and read using the Virtru Secure Reader, at the cost of reduced space efficiency.
408-
* Returns this object for method chaining.
409-
* <br/><br/>
410-
* This is enabled by default.
411-
* @return {EncryptParamsBuilder} - this object.
398+
* @deprecated This feature is not supported
412399
*/
413400
withHtmlFormat(): EncryptParamsBuilder {
414-
this.setHtmlFormat();
415-
return this;
401+
throw new ConfigurationError('HTML format is not supported');
416402
}
417403

418404
/**

lib/tdf3/src/client/index.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import {
1010
EncryptConfiguration,
1111
fetchKasPublicKey,
1212
loadTDFStream,
13-
unwrapHtml,
1413
validatePolicyObject,
1514
readStream,
16-
wrapHtml,
1715
writeStream,
1816
} from '../tdf.js';
17+
import { unwrapHtml } from '../utils/unwrap.js';
1918
import { OIDCRefreshTokenProvider } from '../../../src/auth/oidc-refreshtoken-provider.js';
2019
import { OIDCExternalJwtProvider } from '../../../src/auth/oidc-externaljwt-provider.js';
2120
import { CryptoService } from '../crypto/declarations.js';
@@ -52,7 +51,6 @@ import { attributeFQNsAsValues } from '../../../src/policy/api.js';
5251
import { type Value } from '../../../src/policy/attributes.js';
5352

5453
const GLOBAL_BYTE_LIMIT = 64 * 1000 * 1000 * 1000; // 64 GB, see WS-9363.
55-
const HTML_BYTE_LIMIT = 100 * 1000 * 1000; // 100 MB, see WS-9476.
5654

5755
// No default config for now. Delegate to Virtru wrapper for endpoints.
5856
const defaultClientConfig = { oidcOrigin: '', cryptoService: defaultCryptoService };
@@ -350,7 +348,7 @@ export class Client {
350348
scope = { attributes: [], dissem: [] },
351349
autoconfigure,
352350
source,
353-
asHtml = false,
351+
asHtml,
354352
metadata,
355353
mimeType,
356354
offline = true,
@@ -363,6 +361,9 @@ export class Client {
363361
if (!offline) {
364362
throw new ConfigurationError('online mode not supported');
365363
}
364+
if (asHtml) {
365+
throw new ConfigurationError('html mode not supported');
366+
}
366367
const dpopKeys = await this.dpopKeys;
367368

368369
const policyObject = asPolicy(scope);
@@ -426,7 +427,7 @@ export class Client {
426427

427428
// TODO: Refactor underlying builder to remove some of this unnecessary config.
428429

429-
const byteLimit = asHtml ? HTML_BYTE_LIMIT : GLOBAL_BYTE_LIMIT;
430+
const byteLimit = GLOBAL_BYTE_LIMIT;
430431
const encryptionInformation = new SplitKey(new AesGcmCipher(this.cryptoService));
431432
const splits: SplitStep[] = splitPlan?.length ? splitPlan : [{ kas: this.kasEndpoint }];
432433
encryptionInformation.keyAccess = await Promise.all(
@@ -465,24 +466,7 @@ export class Client {
465466
assertionConfigs,
466467
};
467468

468-
const stream = await (streamMiddleware as EncryptStreamMiddleware)(await writeStream(ecfg));
469-
470-
if (!asHtml) {
471-
return stream;
472-
}
473-
474-
// Wrap if it's html.
475-
if (!stream.manifest) {
476-
throw new Error('internal: missing manifest in encrypt function');
477-
}
478-
const htmlBuf = wrapHtml(await stream.toBuffer(), stream.manifest, this.readerUrl ?? '');
479-
480-
return new DecoratedReadableStream({
481-
pull(controller: ReadableStreamDefaultController) {
482-
controller.enqueue(htmlBuf);
483-
controller.close();
484-
},
485-
});
469+
return (streamMiddleware as EncryptStreamMiddleware)(await writeStream(ecfg));
486470
}
487471

488472
/**

lib/tdf3/src/tdf.ts

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ import {
1919
SplitType,
2020
} from './models/index.js';
2121
import { base64 } from '../../src/encodings/index.js';
22-
import {
23-
ZipReader,
24-
ZipWriter,
25-
base64ToBuffer,
26-
keyMerge,
27-
buffToString,
28-
concatUint8,
29-
} from './utils/index.js';
22+
import { ZipReader, ZipWriter, keyMerge, buffToString, concatUint8 } from './utils/index.js';
3023
import { Binary } from './binary.js';
3124
import { KasPublicKeyAlgorithm, KasPublicKeyInfo, OriginAllowList } from '../../src/access.js';
3225
import { allPool, anyPool } from '../../src/concurrency.js';
@@ -40,7 +33,6 @@ import {
4033
UnsupportedFeatureError as UnsupportedError,
4134
} from '../../src/errors.js';
4235
import { type Chunker } from '../../src/seekable.js';
43-
import { htmlWrapperTemplate } from './templates/index.js';
4436

4537
// configurable
4638
// TODO: remove dependencies from ciphers so that we can open-source instead of relying on other Virtru libs
@@ -180,51 +172,6 @@ export async function fetchKasPublicKey(
180172
return fetchKasPubKeyV2(kas, algorithm || 'rsa:2048');
181173
}
182174

183-
/**
184-
*
185-
* @param payload The TDF content to encode in HTML
186-
* @param manifest A copy of the manifest
187-
* @param transferUrl reader web-service start page
188-
* @return utf-8 encoded HTML data
189-
*/
190-
export function wrapHtml(
191-
payload: Uint8Array,
192-
manifest: Manifest | string,
193-
transferUrl: string
194-
): Uint8Array {
195-
const { origin } = new URL(transferUrl);
196-
const exportManifest: string = typeof manifest === 'string' ? manifest : JSON.stringify(manifest);
197-
198-
const fullHtmlString = htmlWrapperTemplate({
199-
transferUrl,
200-
transferBaseUrl: origin,
201-
manifest: base64.encode(exportManifest),
202-
payload: buffToString(payload, 'base64'),
203-
});
204-
205-
return new TextEncoder().encode(fullHtmlString);
206-
}
207-
208-
export function unwrapHtml(htmlPayload: ArrayBuffer | Uint8Array | Binary | string) {
209-
let html;
210-
if (htmlPayload instanceof ArrayBuffer || ArrayBuffer.isView(htmlPayload)) {
211-
html = new TextDecoder().decode(htmlPayload);
212-
} else {
213-
html = htmlPayload.toString();
214-
}
215-
const payloadRe = /<input id=['"]?data-input['"]?[^>]*?value=['"]?([a-zA-Z0-9+/=]+)['"]?/;
216-
const reResult = payloadRe.exec(html);
217-
if (reResult === null) {
218-
throw new InvalidFileError('Payload is missing');
219-
}
220-
const base64Payload = reResult[1];
221-
try {
222-
return base64ToBuffer(base64Payload);
223-
} catch (e) {
224-
throw new InvalidFileError('There was a problem extracting the TDF3 payload', e);
225-
}
226-
}
227-
228175
export async function extractPemFromKeyString(keyString: string): Promise<string> {
229176
let pem: string = keyString;
230177

lib/tdf3/src/templates/default.html.ts

Lines changed: 0 additions & 105 deletions
This file was deleted.

lib/tdf3/src/templates/escaper.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

lib/tdf3/src/templates/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

lib/tdf3/src/utils/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ const hexSliceLookupTable = (() => {
2424
return table;
2525
})();
2626

27-
export function base64ToBuffer(b64: string): Uint8Array {
28-
return Uint8Array.from(atob(b64).split(''), (c) => c.charCodeAt(0));
29-
}
30-
3127
export function concatUint8(uint8Arrays: Uint8Array[]): Uint8Array {
3228
const newLength = uint8Arrays.reduce(
3329
(accumulator, currentValue) => accumulator + currentValue.length,

lib/tdf3/src/utils/unwrap.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { decodeArrayBuffer } from '../../../src/encodings/base64.js';
2+
import { InvalidFileError } from '../../../src/errors.js';
3+
4+
export function unwrapHtml(htmlPayload: Uint8Array): Uint8Array {
5+
const html = new TextDecoder().decode(htmlPayload);
6+
const payloadRe = /<input id=['"]?data-input['"]?[^>]*?value=['"]?([a-zA-Z0-9+/=]+)['"]?/;
7+
const reResult = payloadRe.exec(html);
8+
if (!reResult) {
9+
throw new InvalidFileError('Payload is missing');
10+
}
11+
const base64Payload = reResult[1];
12+
try {
13+
return new Uint8Array(decodeArrayBuffer(base64Payload));
14+
} catch (e) {
15+
throw new InvalidFileError('There was a problem extracting the TDF3 payload', e);
16+
}
17+
}

lib/tests/mocha/unit/crypto-di.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ describe('CryptoService DI', () => {
7373
});
7474
try {
7575
await c.encrypt({
76-
asHtml: false,
7776
source: new ReadableStream({
7877
pull(controller) {
7978
controller.enqueue(new TextEncoder().encode('hello world'));

0 commit comments

Comments
 (0)