fix(utils/crypto): make Binary and JSON object crypto correct (#454)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 5838f9d · Browse files · 6 files·+89 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+89 added−10 removed
- tests:+57 added−2 removed
- other:+32 added−8 removed
Provenance
- Commit
8a9ebc1- Tree
ace106e- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
5838f9d
6 files changed+89 added−10 removed
modified:deno_dist/utils/body.ts+3 added-2 removed
11234515 unmodified lines2122232425252627export type Body = string | object | Record<string, string | File>export type Body = string | object | Record<string, string | File> | ArrayBuffer
export const parseBody = async (r: Request | Response): Promise<Body> => { const contentType = r.headers.get('Content-Type') || ''
15 unmodified lines }, form) return data }
return r.arrayBuffer() const arrayBuffer = await r.arrayBuffer() return arrayBuffer}modified:deno_dist/utils/crypto.ts+13 added-2 removed
12345667891012 unmodified lines23242526272829303132333435363738394041423243444546474 unmodified linestype Algorithm = { name: string alias: string}
type Data = string | object | booleantype Data = string | boolean | number | object | ArrayBufferView | ArrayBuffer
export const sha256 = async (data: Data) => { const algorithm: Algorithm = { name: 'SHA-256', alias: 'sha256' } const hash = await createHash(data, algorithm)12 unmodified lines return hash}
export const createHash = async (data: Data, algorithm: Algorithm): Promise<string | null> => { let sourceBuffer: ArrayBufferView | ArrayBuffer
if (ArrayBuffer.isView(data) || data instanceof ArrayBuffer) { sourceBuffer = data } else { if (typeof data === 'object') { data = JSON.stringify(data) } sourceBuffer = new TextEncoder().encode(String(data)) }
if (crypto && crypto.subtle) { const buffer = await crypto.subtle.digest( { name: algorithm.name, }, new TextEncoder().encode(String(data)) sourceBuffer ) const hash = Array.prototype.map .call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)) .join('')4 unmodified linesmodified:src/middleware/etag/index.test.ts+41 added-1 removed
15 unmodified lines161718192021222324252627282930313233343536373839404142434445274647484950515253545556575859606162636465666768697071727318 unmodified lines15 unmodified lines app.get('/etag-weak/abc', (c) => { return c.text('Hono is cool') })
app.use('/etag-binary/*', etag()) app.get('/etag-binary', async (c) => { return c.body(new Uint8Array(1)) })
app.get('/etag/ab1', (c) => { return c.body(new ArrayBuffer(1)) }) app.get('/etag/ab2', (c) => { return c.body(new ArrayBuffer(2)) })
app.get('/etag/ui1', (c) => { return c.body(new Uint8Array([1, 2, 3])) }) app.get('/etag/ui2', (c) => { return c.body(new Uint8Array([1, 2, 3, 4])) })
it('Should return etag header', async () => { let res = await app.request('http://localhost/etag/abc') expect(res.headers.get('ETag')).not.toBeFalsy() expect(res.headers.get('ETag')).toBe('"4e32298b1cb4edc595237405e5b696e105c2399a"')
res = await app.request('http://localhost/etag/def') expect(res.headers.get('ETag')).not.toBeFalsy() expect(res.headers.get('ETag')).toBe('"c1d44ff03aff1372856c281854f454e2e1d15b7c"') expect(res.headers.get('ETag')).toBe('"4515561204e8269cb4468d5b39288d8f2482dcfe"') })
it('Should return etag header - binary', async () => { const res = await app.request('http://localhost/etag-binary') expect(res.headers.get('ETag')).not.toBeFalsy() const etag = res.headers.get('ETag') expect(etag).toBe('"5ba93c9db0cff93f52b521d7420e43f6eda2784f"') })
it('Should not be the same etag - arrayBuffer', async () => { let res = await app.request('http://localhost/etag/ab1') const hash = res.headers.get('Etag') res = await app.request('http://localhost/etag/ab2') expect(res.headers.get('ETag')).not.toBe(hash) })
it('Should not be the same etag - Uint8Array', async () => { let res = await app.request('http://localhost/etag/ui1') const hash = res.headers.get('Etag') res = await app.request('http://localhost/etag/ui2') expect(res.headers.get('ETag')).not.toBe(hash) })
it('Should return etag header - weak', async () => { const res = await app.request('http://localhost/etag-weak/abc') expect(res.headers.get('ETag')).not.toBeFalsy() expect(res.headers.get('ETag')).toBe('W/"4e32298b1cb4edc595237405e5b696e105c2399a"')18 unmodified linesmodified:src/utils/body.ts+3 added-2 removed
11234515 unmodified lines2122232425252627export type Body = string | object | Record<string, string | File>export type Body = string | object | Record<string, string | File> | ArrayBuffer
export const parseBody = async (r: Request | Response): Promise<Body> => { const contentType = r.headers.get('Content-Type') || ''
15 unmodified lines }, form) return data }
return r.arrayBuffer() const arrayBuffer = await r.arrayBuffer() return arrayBuffer}modified:src/utils/crypto.test.ts+16 added-1 removed
1+import { createHash } from 'crypto'2 import { sha256, sha1, md5 } from './crypto'3 4 describe('crypto', () => {5 it('sha256', async () => {10 unmodified lines16 expect(await sha1('hono')).toBe('28c7e86f5732391917876b45c06c626c04d77f39')17 expect(await sha1('炎')).toBe('d56e09ae2421b2b8a0b5ee5fdceaed663c8c9472')18 expect(await sha1('abcdedf')).not.toBe('abcdef')19 })19- 20+21 it('md5', async () => {22 expect(await md5('hono')).toBe('cf22a160789a91dd5f737cd3b2640cc2')23 expect(await md5('炎')).toBe('f620d89a5a782c22b4420acb39121be3')24 expect(await md5('abcdedf')).not.toBe('abcdef')25 })26+27+ it('Should not be the same values - compare difference objects', async () => {28+ expect(await sha256({ foo: 'bar' })).not.toEqual(29+ await sha256({30+ bar: 'foo',31+ })32+ )33+ })34+35+ it('Should create hash for Buffer', async () => {36+ const hash = createHash('sha256').update(new Uint8Array(1)).digest('hex')37+ expect(await sha256(new Uint8Array(1))).toBe(hash)38+ expect(await sha256(new Uint8Array(1))).not.toEqual(await sha256(new Uint8Array(2)))39+ })40 })modified:src/utils/crypto.ts+13 added-2 removed
1 unmodified line2 name: string3 alias: string4 }5 6-type Data = string | object | boolean6+type Data = string | boolean | number | object | ArrayBufferView | ArrayBuffer7 8 export const sha256 = async (data: Data) => {9 const algorithm: Algorithm = { name: 'SHA-256', alias: 'sha256' }10 const hash = await createHash(data, algorithm)12 unmodified lines23 return hash24 }25 26 export const createHash = async (data: Data, algorithm: Algorithm): Promise<string | null> => {27+ let sourceBuffer: ArrayBufferView | ArrayBuffer28+29+ if (ArrayBuffer.isView(data) || data instanceof ArrayBuffer) {30+ sourceBuffer = data31+ } else {32+ if (typeof data === 'object') {33+ data = JSON.stringify(data)34+ }35+ sourceBuffer = new TextEncoder().encode(String(data))36+ }37+38 if (crypto && crypto.subtle) {39 const buffer = await crypto.subtle.digest(40 {41 name: algorithm.name,42 },32- new TextEncoder().encode(String(data))43+ sourceBuffer44 )45 const hash = Array.prototype.map46 .call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2))47 .join('')4 unmodified lines