-
Notifications
You must be signed in to change notification settings - Fork 619
Add IE 11 detection for crypto components #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
*.js | ||
*.js.map | ||
*.d.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {isMsWindow, MsWindow} from "../lib/MsWindow"; | ||
|
||
const fakeMsWindow: MsWindow = { | ||
MSInputMethodContext: {}, | ||
msCrypto: { | ||
getRandomValues: () => {}, | ||
subtle: { | ||
decrypt(alg: any, key: any) { return {} as any; }, | ||
digest(alg: any) { return {} as any; }, | ||
encrypt(alg: any, key: any) { return {} as any; }, | ||
exportKey(format: any, key: any) { return {} as any; }, | ||
generateKey(alg: any) { return {} as any; }, | ||
importKey(format: any, keyData: any, alg: any) { return {} as any; }, | ||
sign(alg: any, key: any) { return {} as any; }, | ||
verify(alg: any, key: any, signature: any) { return {} as any; }, | ||
}, | ||
}, | ||
} as any; | ||
|
||
describe('isMsWindow', () => { | ||
it( | ||
'should return false if an object does not fulfill the MsWindow interface', | ||
() => { | ||
expect(isMsWindow({} as any)).toBe(false); | ||
} | ||
); | ||
|
||
it('should return true if an object fulfills the MsWindow interface', () => { | ||
expect(isMsWindow(fakeMsWindow)).toBe(true); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './lib/CryptoOperation'; | ||
export * from './lib/Key'; | ||
export * from './lib/KeyOperation'; | ||
export * from './lib/MsSubtleCrypto'; | ||
export * from './lib/MsWindow'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {Key} from "./Key"; | ||
|
||
/** | ||
* Represents a cryptographic operation that has been instantiated but not | ||
* necessarily fed all data or finalized. | ||
* | ||
* @see https://msdn.microsoft.com/en-us/library/dn280996(v=vs.85).aspx | ||
*/ | ||
export interface CryptoOperation { | ||
readonly algorithm: string; | ||
readonly key: Key; | ||
onabort: (event: Event) => void; | ||
oncomplete: (event: Event) => void; | ||
onerror: (event: Event) => void; | ||
onprogress: (event: Event) => void; | ||
readonly result: ArrayBuffer|undefined; | ||
|
||
abort(): void; | ||
finish(): void; | ||
process(buffer: ArrayBufferView): void; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* The result of a successful KeyOperation. | ||
* | ||
* @see {KeyOperation} | ||
* @see https://msdn.microsoft.com/en-us/library/dn302313(v=vs.85).aspx | ||
*/ | ||
export interface Key { | ||
readonly algorithm: string; | ||
readonly extractable: boolean; | ||
readonly keyUsage: Array<string>; | ||
readonly type: string; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Key} from "./Key"; | ||
|
||
/** | ||
* Represents the return of a key-related operation that may or may not have | ||
* been completed. | ||
* | ||
* @see https://msdn.microsoft.com/en-us/library/dn302314(v=vs.85).aspx | ||
*/ | ||
export interface KeyOperation { | ||
oncomplete: (event: Event) => void; | ||
onerror: (event: Event) => void; | ||
readonly result: Key|undefined; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {CryptoOperation} from './CryptoOperation'; | ||
import {Key} from './Key'; | ||
import {KeyOperation} from './KeyOperation'; | ||
|
||
export type KeyUsage = 'encrypt'|'decrypt'|'sign'|'verify'|'derive'|'wrap'|'unwrap'; | ||
|
||
export type EncryptionOrVerificationAlgorithm = 'RSAES-PKCS1-v1_5'; | ||
export type Ie11EncryptionAlgorithm = 'AES-CBC'|'AES-GCM'|'RSA-OAEP'|EncryptionOrVerificationAlgorithm; | ||
export type Ie11DigestAlgorithm = 'SHA-1'|'SHA-256'|'SHA-384'; | ||
|
||
export interface HashAlgorithm { | ||
name: Ie11DigestAlgorithm; | ||
} | ||
|
||
export interface HmacAlgorithm { | ||
name: 'HMAC'; | ||
hash: HashAlgorithm; | ||
} | ||
|
||
export type SigningAlgorithm = HmacAlgorithm; | ||
|
||
/** | ||
* Represent ths SubtleCrypto interface as implemented in Internet Explorer 11. | ||
* This implementation was based on an earlier version of the WebCrypto API and | ||
* differs from the `window.crypto.subtle` object exposed in Chrome, Safari, | ||
* Firefox, and MS Edge. | ||
* | ||
* @see https://msdn.microsoft.com/en-us/library/dn302325(v=vs.85).aspx | ||
*/ | ||
export interface MsSubtleCrypto { | ||
decrypt( | ||
algorithm: Ie11EncryptionAlgorithm, | ||
key: Key, | ||
buffer?: ArrayBufferView | ||
): CryptoOperation; | ||
|
||
digest( | ||
algorithm: Ie11DigestAlgorithm, | ||
buffer?: ArrayBufferView | ||
): CryptoOperation; | ||
|
||
encrypt( | ||
algorithm: Ie11EncryptionAlgorithm, | ||
key: Key, | ||
buffer?: ArrayBufferView | ||
): CryptoOperation; | ||
|
||
exportKey( | ||
format: string, | ||
key: Key | ||
): KeyOperation; | ||
|
||
generateKey( | ||
algorithm: SigningAlgorithm|Ie11EncryptionAlgorithm, | ||
extractable?: boolean, | ||
keyUsages?: Array<KeyUsage> | ||
): KeyOperation; | ||
|
||
importKey( | ||
format: string, | ||
keyData: ArrayBufferView, | ||
algorithm: any, | ||
extractable?: boolean, | ||
keyUsages?: Array<KeyUsage> | ||
): KeyOperation; | ||
|
||
sign( | ||
algorithm: SigningAlgorithm, | ||
key: Key, | ||
buffer?: ArrayBufferView, | ||
): CryptoOperation; | ||
|
||
verify( | ||
algorithm: SigningAlgorithm|EncryptionOrVerificationAlgorithm, | ||
key: Key, | ||
signature: ArrayBufferView, | ||
buffer?: ArrayBufferView | ||
): CryptoOperation; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {MsSubtleCrypto} from "./MsSubtleCrypto"; | ||
|
||
type SubtleCryptoMethod = 'decrypt'|'digest'|'encrypt'|'exportKey'|'generateKey'|'importKey'|'sign'|'verify'; | ||
|
||
const msSubtleCryptoMethods: Array<SubtleCryptoMethod> = [ | ||
'decrypt', | ||
'digest', | ||
'encrypt', | ||
'exportKey', | ||
'generateKey', | ||
'importKey', | ||
'sign', | ||
'verify', | ||
]; | ||
|
||
/** | ||
* The value accessible as `window.msCrypto` in Internet Explorer 11. | ||
*/ | ||
export interface MsCrypto { | ||
getRandomValues: (toFill: Uint8Array) => void; | ||
subtle: MsSubtleCrypto; | ||
} | ||
|
||
/** | ||
* The `window` object in Internet Explorer 11. This interface does not | ||
* exhaustively document the prefixed features of `window` in IE11. | ||
*/ | ||
export interface MsWindow extends Window { | ||
MSInputMethodContext: any; | ||
msCrypto: MsCrypto; | ||
} | ||
|
||
function quacksLikeAnMsWindow(window: Window): window is MsWindow { | ||
return 'MSInputMethodContext' in window && | ||
'msCrypto' in window; | ||
} | ||
|
||
/** | ||
* Determines if the provided window is (or is like) the window object one would | ||
* expect to encounter in Internet Explorer 11. | ||
*/ | ||
export function isMsWindow(window: Window): window is MsWindow { | ||
if (quacksLikeAnMsWindow(window) && window.msCrypto.subtle !== undefined) { | ||
const {getRandomValues, subtle} = window.msCrypto; | ||
const methodsToTest = msSubtleCryptoMethods | ||
.map<Function>(methodName => subtle[methodName]) | ||
.concat(getRandomValues); | ||
|
||
for (let method of methodsToTest) { | ||
if (typeof method !== 'function') { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "@aws/crypto-ie11-detection", | ||
"private": true, | ||
"version": "0.0.1", | ||
"description": "Provides functions and types for detecting if the host environment is IE11", | ||
"scripts": { | ||
"pretest": "tsc", | ||
"test": "jest" | ||
}, | ||
"author": "[email protected]", | ||
"license": "UNLICENSED", | ||
"main": "index.js", | ||
"devDependencies": { | ||
"@types/jest": "^19.2.2", | ||
"jest": "^19.0.2", | ||
"typescript": "^2.3" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"strict": true, | ||
"sourceMap": true, | ||
"declaration": true, | ||
"stripInternal": true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on function name