Skip to content

Commit 748be9e

Browse files
committed
feat: Updates to the AWS Encryption SDK.
This change includes fixes for issues that were reported by Thai Duong from Google's Security team, and for issues that were identified by AWS Cryptography. See: https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/migration.html
1 parent f0b58dc commit 748be9e

File tree

116 files changed

+10350
-9784
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+10350
-9784
lines changed

.eslintrc.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ module.exports = {
4242
// it is good for understanding
4343
// for the source files to get more detailed
4444
// as you read down from the top.
45-
'no-use-before-define': ['error', { functions: false }],
45+
// Note: eslint has gotten better
46+
// at parsing typescript
47+
// and now errors for interfaces as well.
48+
'no-use-before-define': 'off',
4649
'@typescript-eslint/no-use-before-define': ['error', { functions: false }],
4750
// This is used in a few specific ways.
4851
// It may be that adding this to overrides for the tests
@@ -74,7 +77,9 @@ module.exports = {
7477
'@typescript-eslint/no-empty-interface': 'off',
7578
// To minimize the source change,
7679
// this is turned of.
77-
'@typescript-eslint/ban-ts-ignore': 'off',
80+
'@typescript-eslint/ban-ts-comment': ['error', { 'ts-ignore': false }],
81+
// This rule fights with Prettier and no-semi
82+
'@typescript-eslint/no-extra-semi': 'off',
7883
},
7984
// This is a good rule,
8085
// but in many tests,

buildspec.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
version: 0.2
22

3+
env:
4+
variables:
5+
NODE_OPTIONS: "--max-old-space-size=4096"
6+
37
phases:
48
install:
59
runtime-versions:
610
nodejs: 10
711
commands:
8-
- npm ci
12+
- npm ci --unsafe-perm
913
- npm run build
1014
build:
1115
commands:

karma.conf.js

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
// Karma configuration
55

6-
module.exports = function (config) {
6+
const credentialsPromise = require('@aws-sdk/credential-provider-node').defaultProvider()()
77

8+
module.exports = function (config) {
89
process.on('infrastructure_error', (error) => {
910
/* @aws-sdk/karma-credential-loader get credential
1011
* as configured by the AWS SDK.
@@ -14,7 +15,7 @@ module.exports = function (config) {
1415
* The following will log errors link this,
1516
* but still let the karma-server shut down.
1617
*/
17-
console.error('infrastructure_error', error);
18+
console.error('infrastructure_error', error)
1819
})
1920

2021
config.set({
@@ -30,7 +31,7 @@ module.exports = function (config) {
3031
},
3132
webpack: {
3233
resolve: {
33-
extensions: [ '.ts', '.js' ]
34+
extensions: ['.ts', '.js'],
3435
},
3536
mode: 'development',
3637
module: {
@@ -43,47 +44,49 @@ module.exports = function (config) {
4344
options: {
4445
logInfoToStdOut: true,
4546
projectReferences: true,
46-
configFile: `${__dirname}/tsconfig.module.json`
47-
}
48-
}
47+
configFile: `${__dirname}/tsconfig.module.json`,
48+
},
49+
},
4950
],
5051
exclude: /node_modules/,
5152
},
5253
{
5354
test: /\.ts$/,
54-
exclude: [ /\/test\// ],
55+
exclude: [/\/test\//],
5556
enforce: 'post',
5657
use: {
5758
loader: 'istanbul-instrumenter-loader',
58-
options: { esModules: true }
59-
}
60-
}
61-
]
59+
options: { esModules: true },
60+
},
61+
},
62+
],
6263
},
6364
stats: {
6465
colors: true,
6566
modules: true,
6667
reasons: true,
67-
errorDetails: true
68+
errorDetails: true,
6869
},
6970
devtool: 'inline-source-map',
7071
node: {
71-
fs: 'empty'
72-
}
72+
fs: 'empty',
73+
},
7374
},
7475
coverageIstanbulReporter: {
75-
reports: [ 'json' ],
76+
reports: ['json'],
7677
dir: '.karma_output',
77-
fixWebpackSourcePaths: true
78+
fixWebpackSourcePaths: true,
7879
},
7980
plugins: [
80-
'@aws-sdk/karma-credential-loader',
81+
{
82+
'preprocessor:credentials': ['factory', createCredentialPreprocessor],
83+
},
8184
'karma-chrome-launcher',
8285
'karma-mocha',
8386
'karma-chai',
8487
'karma-webpack',
8588
'karma-coverage-istanbul-reporter',
86-
'karma-json-fixtures-preprocessor'
89+
'karma-json-fixtures-preprocessor',
8790
],
8891
reporters: ['progress', 'coverage-istanbul'],
8992
port: 9876,
@@ -94,11 +97,36 @@ module.exports = function (config) {
9497
customLaunchers: {
9598
ChromeHeadlessDisableCors: {
9699
base: 'ChromeHeadless',
97-
flags: ['--disable-web-security', '--no-sandbox']
98-
}
100+
flags: ['--disable-web-security', '--no-sandbox'],
101+
},
99102
},
100103
singleRun: true,
101104
concurrency: Infinity,
102-
exclude: ['**/*.d.ts']
105+
exclude: ['**/*.d.ts'],
103106
})
104107
}
108+
109+
function createCredentialPreprocessor() {
110+
return async function (content, file, done) {
111+
// strip the extension from the file since it won't match the preprocessor pattern
112+
const fileName = file.originalPath
113+
// add region and credentials to each file
114+
const region = process.env.AWS_SMOKE_TEST_REGION || ''
115+
const credentials = await credentialsPromise
116+
// This will affect the generated (ES5) JS
117+
const regionCode = `var defaultRegion = '${region}';`
118+
const credentialsCode = `var credentials = ${JSON.stringify(credentials)};`
119+
const isBrowser = `var isBrowser = true;`
120+
const contents = content.split('\n')
121+
let idx = -1
122+
for (let i = 0; i < contents.length; i++) {
123+
const line = contents[i]
124+
if (line.indexOf(fileName) !== -1) {
125+
idx = i
126+
break
127+
}
128+
}
129+
contents.splice(idx + 1, 0, regionCode, credentialsCode, isBrowser)
130+
done(contents.join('\n'))
131+
}
132+
}

modules/cache-material/src/build_cryptographic_materials_cache_key_helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export interface CryptographicMaterialsCacheKeyHelpersInterface<
9292
> {
9393
buildEncryptionMaterialCacheKey(
9494
partition: string,
95-
{ suite, encryptionContext }: EncryptionRequest<S>
95+
{ suite, encryptionContext }: Omit<EncryptionRequest<S>, 'commitmentPolicy'>
9696
): Promise<string>
9797
buildDecryptionMaterialCacheKey(
9898
partition: string,

modules/cache-material/src/caching_cryptographic_materials_decorators.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,23 @@ export function getEncryptionMaterials<S extends SupportedAlgorithmSuites>({
8686
this: CachingMaterialsManager<S>,
8787
request: EncryptionRequest<S>
8888
): Promise<EncryptionMaterial<S>> {
89-
const { suite, encryptionContext, plaintextLength } = request
89+
const {
90+
suite,
91+
encryptionContext,
92+
plaintextLength,
93+
commitmentPolicy,
94+
} = request
95+
9096
/* Check for early return (Postcondition): If I can not cache the EncryptionMaterial, do not even look. */
9197
if (
9298
(suite && !suite.cacheSafe) ||
9399
typeof plaintextLength !== 'number' ||
94100
plaintextLength < 0
95101
) {
96-
return this._backingMaterialsManager.getEncryptionMaterials(request)
102+
const material = await this._backingMaterialsManager.getEncryptionMaterials(
103+
request
104+
)
105+
return material
97106
}
98107

99108
const cacheKey = await buildEncryptionMaterialCacheKey(this._partition, {
@@ -112,7 +121,7 @@ export function getEncryptionMaterials<S extends SupportedAlgorithmSuites>({
112121
/* Strip any information about the plaintext from the backing request,
113122
* because the resulting response may be used to encrypt multiple plaintexts.
114123
*/
115-
.getEncryptionMaterials({ suite, encryptionContext, plaintextLength })
124+
.getEncryptionMaterials({ suite, encryptionContext, commitmentPolicy })
116125

117126
/* Check for early return (Postcondition): If I can not cache the EncryptionMaterial, just return it. */
118127
if (!material.suite.cacheSafe) return material
@@ -157,7 +166,10 @@ export function decryptMaterials<S extends SupportedAlgorithmSuites>({
157166
const { suite } = request
158167
/* Check for early return (Postcondition): If I can not cache the DecryptionMaterial, do not even look. */
159168
if (!suite.cacheSafe) {
160-
return this._backingMaterialsManager.decryptMaterials(request)
169+
const material = await this._backingMaterialsManager.decryptMaterials(
170+
request
171+
)
172+
return material
161173
}
162174

163175
const cacheKey = await buildDecryptionMaterialCacheKey(

modules/cache-material/test/caching_cryptographic_materials_decorators.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
EncryptedDataKey,
2222
NodeEncryptionMaterial,
2323
NodeDecryptionMaterial,
24+
CommitmentPolicy,
2425
} from '@aws-crypto/material-management'
2526

2627
describe('decorateProperties', () => {
@@ -33,6 +34,7 @@ describe('decorateProperties', () => {
3334
partition: 'something',
3435
maxBytesEncrypted: 100,
3536
maxMessagesEncrypted: 200,
37+
commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
3638
} as any)
3739

3840
expect(test._cache).to.equal('cache')
@@ -121,6 +123,7 @@ describe('cacheEntryHasExceededLimits', () => {
121123
partition: 'something',
122124
maxBytesEncrypted,
123125
maxMessagesEncrypted,
126+
commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
124127
} as any)
125128

126129
test.cacheEntryHasExceededLimits = cacheEntryHasExceededLimits()
@@ -275,6 +278,7 @@ describe('Cryptographic Material Functions', () => {
275278
_cacheEntryHasExceededLimits: cacheEntryHasExceededLimits(),
276279
getEncryptionMaterials: getEncryptionMaterials(cacheKeyHelpers),
277280
decryptMaterials: decryptMaterials(cacheKeyHelpers),
281+
_commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
278282
} as any
279283

280284
describe('getEncryptionMaterials', () => {
@@ -310,6 +314,7 @@ describe('Cryptographic Material Functions', () => {
310314
_cacheEntryHasExceededLimits: cacheEntryHasExceededLimits(),
311315
getEncryptionMaterials: getEncryptionMaterials(cacheKeyHelpers),
312316
decryptMaterials: decryptMaterials(cacheKeyHelpers),
317+
_commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
313318
} as any
314319

315320
const testSuiteCacheSafe = await testCMM.getEncryptionMaterials({
@@ -356,6 +361,7 @@ describe('Cryptographic Material Functions', () => {
356361
decryptMaterials: () => {
357362
throw new Error('this should never happen')
358363
},
364+
_commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
359365
} as any
360366

361367
await testCMM.getEncryptionMaterials({
@@ -444,6 +450,7 @@ describe('Cryptographic Material Functions', () => {
444450
decryptMaterials: () => {
445451
throw new Error('this should never happen')
446452
},
453+
_commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
447454
} as any
448455

449456
const test = await testCMM.getEncryptionMaterials({
@@ -539,6 +546,7 @@ describe('Cryptographic Material Functions', () => {
539546
decryptMaterials: () => {
540547
throw new Error('this should never happen')
541548
},
549+
_commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
542550
} as any
543551

544552
const test = await testCMM.getEncryptionMaterials({
@@ -582,6 +590,7 @@ describe('Cryptographic Material Functions', () => {
582590
},
583591
},
584592
_backingMaterialsManager,
593+
_commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
585594
_cacheEntryHasExceededLimits: cacheEntryHasExceededLimits(),
586595
getEncryptionMaterials: getEncryptionMaterials(cacheKeyHelpers),
587596
decryptMaterials: decryptMaterials(cacheKeyHelpers),
@@ -618,6 +627,7 @@ describe('Cryptographic Material Functions', () => {
618627
assertCount += 1
619628
return false
620629
},
630+
_commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
621631
getEncryptionMaterials: getEncryptionMaterials(cacheKeyHelpers),
622632
decryptMaterials: decryptMaterials(cacheKeyHelpers),
623633
} as any

modules/caching-materials-manager-browser/src/caching_materials_manager_browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
decryptMaterials,
99
cacheEntryHasExceededLimits,
1010
buildCryptographicMaterialsCacheKeyHelpers,
11-
CachingMaterialsManagerInput,
1211
CryptographicMaterialsCache,
12+
CachingMaterialsManagerInput,
1313
} from '@aws-crypto/cache-material'
1414
import {
1515
WebCryptoMaterialsManager,

modules/caching-materials-manager-node/src/caching_materials_manager_node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
decryptMaterials,
99
cacheEntryHasExceededLimits,
1010
buildCryptographicMaterialsCacheKeyHelpers,
11-
CachingMaterialsManagerInput,
1211
CryptographicMaterialsCache,
12+
CachingMaterialsManagerInput,
1313
} from '@aws-crypto/cache-material'
1414
import {
1515
NodeMaterialsManager,

modules/caching-materials-manager-node/test/caching_materials_manager_node.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { NodeCachingMaterialsManager } from '../src/index'
88
import {} from '@aws-crypto/cache-material'
99
import {
1010
KeyringNode,
11-
NodeDefaultCryptographicMaterialsManager,
1211
NodeEncryptionMaterial,
1312
NodeDecryptionMaterial,
13+
NodeDefaultCryptographicMaterialsManager,
1414
} from '@aws-crypto/material-management-node'
1515

1616
describe('NodeCachingMaterialsManager', () => {

modules/client-browser/Readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ npm install @aws-crypto/client-browser
2626

2727
## use
2828

29+
For detailed code examples
30+
that show you how to these modules
31+
to create keyrings
32+
and encrypt and decrypt data,
33+
install the [example-browser](https://github.com/aws/aws-encryption-sdk-javascript/tree/master/modules/example-browser) module.
34+
2935
```javascript
3036

3137
/* Start by constructing a keyring. We'll create a KMS keyring.

modules/client-browser/src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,17 @@ export * from '@aws-crypto/kms-keyring-browser'
99
export * from '@aws-crypto/raw-aes-keyring-browser'
1010
export * from '@aws-crypto/raw-rsa-keyring-browser'
1111
export * from '@aws-crypto/web-crypto-backend'
12+
13+
import { CommitmentPolicy } from '@aws-crypto/material-management-browser'
14+
15+
import { buildEncrypt } from '@aws-crypto/encrypt-browser'
16+
import { buildDecrypt } from '@aws-crypto/decrypt-browser'
17+
18+
export function buildClient(
19+
commitmentPolicy: CommitmentPolicy
20+
): ReturnType<typeof buildEncrypt> & ReturnType<typeof buildDecrypt> {
21+
return {
22+
...buildEncrypt(commitmentPolicy),
23+
...buildDecrypt(commitmentPolicy),
24+
}
25+
}

modules/client-node/Readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ npm install @aws-crypto/client-node
2525

2626
## use
2727

28+
For detailed code examples
29+
that show you how to these modules
30+
to create keyrings
31+
and encrypt and decrypt data,
32+
install the [example-node](https://github.com/aws/aws-encryption-sdk-javascript/tree/master/modules/example-node) module.
33+
2834
```javascript
2935

30-
const { KmsKeyringNode, encrypt, decrypt } = require('@aws-crypto/client-node')
3136
/* Start by constructing a keyring. We'll create a KMS keyring.
3237
* Specify an AWS Key Management Service (AWS KMS) customer master key (CMK) to be the
3338
* generator key in the keyring. This CMK generates a data key and encrypts it.

0 commit comments

Comments
 (0)