Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const { test, describe } = require('node:test')
const assert = require('node:assert')

const { applyAlgorithmToBytes } = require('../../lib/web/subresource-integrity/subresource-integrity')
const { runtimeFeatures } = require('../../lib/util/runtime-features')
Expand All @@ -14,16 +13,16 @@ describe('applyAlgorithmToBytes', () => {
const hash384 = 'hiVfosNuSzCWnq4X3DTHcsvr38WLWEA5AL6HYU6xo0uHgCY/JV615lypu7hkHMz+'
const hash512 = '9s3ioPgZMUzd5V/CJ9jX2uPSjMVWIioKitZtkcytSq1glPUXohgjYMmqz2o9wyMWLLb9jN/+2w/gOPVehf+1tg=='

test('valid sha256', { skip }, () => {
test('valid sha256', { skip }, (t) => {
const result = applyAlgorithmToBytes('sha256', Buffer.from('Hello world!'))
assert.strictEqual(result, hash256)
t.assert.strictEqual(result, hash256)
})
test('valid sha384', { skip }, () => {
test('valid sha384', { skip }, (t) => {
const result = applyAlgorithmToBytes('sha384', Buffer.from('Hello world!'))
assert.strictEqual(result, hash384)
t.assert.strictEqual(result, hash384)
})
test('valid sha512', { skip }, () => {
test('valid sha512', { skip }, (t) => {
const result = applyAlgorithmToBytes('sha512', Buffer.from('Hello world!'))
assert.strictEqual(result, hash512)
t.assert.strictEqual(result, hash512)
})
})
5 changes: 2 additions & 3 deletions test/subresource-integrity/bytes-match.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const assert = require('node:assert')
const { test, describe } = require('node:test')

const { bytesMatch } = require('../../lib/web/subresource-integrity/subresource-integrity')
Expand All @@ -9,9 +8,9 @@ const { runtimeFeatures } = require('../../lib/util/runtime-features')
const skip = runtimeFeatures.has('crypto') === false

describe('bytesMatch', () => {
test('valid sha256 and base64', { skip }, () => {
test('valid sha256 and base64', { skip }, (t) => {
const data = Buffer.from('Hello world!')
const hash256 = 'sha256-wFNeS+K3n/2TKRMFQ2v4iTFOSj+uwF7P/Lt98xrZ5Ro='
assert.ok(bytesMatch(data, hash256))
t.assert.ok(bytesMatch(data, hash256))
})
})
45 changes: 22 additions & 23 deletions test/subresource-integrity/case-sensitive-match.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,73 @@
'use strict'

const { test, describe } = require('node:test')
const assert = require('node:assert')

const { caseSensitiveMatch } = require('../../lib/web/subresource-integrity/subresource-integrity')

describe('caseSensitiveMatch', () => {
test('identical strings', () => {
test('identical strings', (t) => {
const actualValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs'
const expectedValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs'
assert.ok(caseSensitiveMatch(actualValue, expectedValue))
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue))
})

test('identical strings, actualValue has one padding char', () => {
test('identical strings, actualValue has one padding char', (t) => {
const actualValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs='
const expectedValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs'
assert.ok(caseSensitiveMatch(actualValue, expectedValue))
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue))
})

test('identical strings, expectedValue has one padding char', () => {
test('identical strings, expectedValue has one padding char', (t) => {
const actualValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs'
const expectedValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs='
assert.ok(caseSensitiveMatch(actualValue, expectedValue))
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue))
})

test('identical strings, expectedValue has two padding chars', () => {
test('identical strings, expectedValue has two padding chars', (t) => {
const actualValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs'
const expectedValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=='
assert.ok(caseSensitiveMatch(actualValue, expectedValue))
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue))
})

test('identical strings, both have one padding char', () => {
test('identical strings, both have one padding char', (t) => {
const actualValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs='
const expectedValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs='
assert.ok(caseSensitiveMatch(actualValue, expectedValue))
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue))
})

test('identical strings, both have two padding chars', () => {
test('identical strings, both have two padding chars', (t) => {
const actualValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=='
const expectedValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=='
assert.ok(caseSensitiveMatch(actualValue, expectedValue))
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue))
})

test('identical strings, expectedValue has invalid third padding char', () => {
test('identical strings, expectedValue has invalid third padding char', (t) => {
const actualValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=='
const expectedValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs==='
assert.ok(caseSensitiveMatch(actualValue, expectedValue) === false)
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue) === false)
})

test('expectedValue can be base64Url - match `_`', () => {
test('expectedValue can be base64Url - match `_`', (t) => {
const actualValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs'
const expectedValue = 'ypeBEsobvcr6wjGzmiPcTaeG7_gUfE5yuYB3ha/uSLs'
assert.ok(caseSensitiveMatch(actualValue, expectedValue))
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue))
})

test('expectedValue can be base64Url - match `+`', () => {
test('expectedValue can be base64Url - match `+`', (t) => {
const actualValue = 'ypeBEsobvcr6wjGzmiPcTaeG7+gUfE5yuYB3ha/uSLs'
const expectedValue = 'ypeBEsobvcr6wjGzmiPcTaeG7-gUfE5yuYB3ha/uSLs'
assert.ok(caseSensitiveMatch(actualValue, expectedValue))
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue))
})

test('should be case sensitive', () => {
test('should be case sensitive', (t) => {
const actualValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs'
const expectedValue = 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLS'
assert.ok(caseSensitiveMatch(actualValue, expectedValue) === false)
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue) === false)
})

test('empty string should return true', () => {
test('empty string should return true', (t) => {
const actualValue = ''
const expectedValue = ''
assert.ok(caseSensitiveMatch(actualValue, expectedValue))
t.assert.ok(caseSensitiveMatch(actualValue, expectedValue))
})
})
29 changes: 14 additions & 15 deletions test/subresource-integrity/get-strongest-metadata.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,70 @@
'use strict'

const assert = require('node:assert')
const { test, describe } = require('node:test')

const { getStrongestMetadata } = require('../../lib/web/subresource-integrity/subresource-integrity')

describe('getStrongestMetadata', () => {
test('should return strongest sha512 /1', () => {
test('should return strongest sha512 /1', (t) => {
const result = getStrongestMetadata([
{ alg: 'sha256', val: 'sha256-abc' },
{ alg: 'sha384', val: 'sha384-def' },
{ alg: 'sha512', val: 'sha512-ghi' }
])
assert.deepEqual(result, [
t.assert.deepEqual(result, [
{ alg: 'sha512', val: 'sha512-ghi' }
])
})

test('should return strongest sha512 /2', () => {
test('should return strongest sha512 /2', (t) => {
const result = getStrongestMetadata([
{ alg: 'sha512', val: 'sha512-ghi' },
{ alg: 'sha256', val: 'sha256-abc' },
{ alg: 'sha384', val: 'sha384-def' }
])
assert.deepEqual(result, [
t.assert.deepEqual(result, [
{ alg: 'sha512', val: 'sha512-ghi' }
])
})

test('should return strongest sha384', () => {
test('should return strongest sha384', (t) => {
const result = getStrongestMetadata([
{ alg: 'sha256', val: 'sha256-abc' },
{ alg: 'sha384', val: 'sha384-def' }
])
assert.deepEqual(result, [
t.assert.deepEqual(result, [
{ alg: 'sha384', val: 'sha384-def' }
])
})

test('should return both strongest sha384', () => {
test('should return both strongest sha384', (t) => {
const result = getStrongestMetadata([
{ alg: 'sha384', val: 'sha384-abc' },
{ alg: 'sha256', val: 'sha256-def' },
{ alg: 'sha384', val: 'sha384-ghi' }
])
assert.deepEqual(result, [
t.assert.deepEqual(result, [
{ alg: 'sha384', val: 'sha384-abc' },
{ alg: 'sha384', val: 'sha384-ghi' }
])
})

test('should return multiple metadata with the same strength', () => {
test('should return multiple metadata with the same strength', (t) => {
const result = getStrongestMetadata([
{ alg: 'sha256', val: 'sha256-abc' }
])
assert.deepEqual(result, [
t.assert.deepEqual(result, [
{ alg: 'sha256', val: 'sha256-abc' }
])
})

test('should return empty array when no metadata is provided', () => {
test('should return empty array when no metadata is provided', (t) => {
const result = getStrongestMetadata([])
assert.deepEqual(result, [])
t.assert.deepEqual(result, [])
})

test('should throw when invalid hash algorithm is provided', () => {
assert.throws(() => getStrongestMetadata([
test('should throw when invalid hash algorithm is provided', (t) => {
t.assert.throws(() => getStrongestMetadata([
{ alg: 'sha1024', val: 'sha1024-xyz' }
]), {
name: 'AssertionError',
Expand Down
17 changes: 8 additions & 9 deletions test/subresource-integrity/is-valid-sri-hash-algorithm.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
'use strict'

const { test, describe } = require('node:test')
const assert = require('node:assert')
const { runtimeFeatures } = require('../../lib/util/runtime-features.js')

const { isValidSRIHashAlgorithm } = require('../../lib/web/subresource-integrity/subresource-integrity')

const skip = runtimeFeatures.has('crypto') === false

describe('isValidSRIHashAlgorithm', () => {
test('valid sha256', { skip }, () => {
assert.ok(isValidSRIHashAlgorithm('sha256'))
test('valid sha256', { skip }, (t) => {
t.assert.ok(isValidSRIHashAlgorithm('sha256'))
})
test('valid sha384', { skip }, () => {
assert.ok(isValidSRIHashAlgorithm('sha384'))
test('valid sha384', { skip }, (t) => {
t.assert.ok(isValidSRIHashAlgorithm('sha384'))
})
test('valid sha512', { skip }, () => {
assert.ok(isValidSRIHashAlgorithm('sha512'))
test('valid sha512', { skip }, (t) => {
t.assert.ok(isValidSRIHashAlgorithm('sha512'))
})
test('invalid sha1024', () => {
assert.ok(isValidSRIHashAlgorithm('sha1024') === false)
test('invalid sha1024', (t) => {
t.assert.ok(isValidSRIHashAlgorithm('sha1024') === false)
})
})
17 changes: 8 additions & 9 deletions test/subresource-integrity/parse-metadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const assert = require('node:assert')
const { test, describe } = require('node:test')

const { parseMetadata } = require('../../lib/web/subresource-integrity/subresource-integrity')
Expand All @@ -14,46 +13,46 @@ describe('parseMetadata', () => {
const hash384 = 'hiVfosNuSzCWnq4X3DTHcsvr38WLWEA5AL6HYU6xo0uHgCY/JV615lypu7hkHMz+'
const hash512 = '9s3ioPgZMUzd5V/CJ9jX2uPSjMVWIioKitZtkcytSq1glPUXohgjYMmqz2o9wyMWLLb9jN/+2w/gOPVehf+1tg=='

test('should parse valid metadata with option', { skip }, () => {
test('should parse valid metadata with option', { skip }, (t) => {
const validMetadata = `sha256-${hash256} !@ sha384-${hash384} !@ sha512-${hash512} !@`
const result = parseMetadata(validMetadata)

assert.deepEqual(result, [
t.assert.deepEqual(result, [
{ alg: 'sha256', val: hash256 },
{ alg: 'sha384', val: hash384 },
{ alg: 'sha512', val: hash512 }
])
})

test('should parse valid metadata with non ASCII chars option', { skip }, () => {
test('should parse valid metadata with non ASCII chars option', { skip }, (t) => {
const validMetadata = `sha256-${hash256} !© sha384-${hash384} !€ sha512-${hash512} !µ`
const result = parseMetadata(validMetadata)

assert.deepEqual(result, [
t.assert.deepEqual(result, [
{ alg: 'sha256', val: hash256 },
{ alg: 'sha384', val: hash384 },
{ alg: 'sha512', val: hash512 }
])
})

test('should parse valid metadata without option', { skip }, () => {
test('should parse valid metadata without option', { skip }, (t) => {
const validMetadata = `sha256-${hash256} sha384-${hash384} sha512-${hash512}`
const result = parseMetadata(validMetadata)

assert.deepEqual(result, [
t.assert.deepEqual(result, [
{ alg: 'sha256', val: hash256 },
{ alg: 'sha384', val: hash384 },
{ alg: 'sha512', val: hash512 }
])
})

test('should not set hash as undefined when invalid base64 chars are provided', { skip }, () => {
test('should not set hash as undefined when invalid base64 chars are provided', { skip }, (t) => {
const invalidHash384 = 'zifp5hE1Xl5LQQqQz[]Bq/iaq9Wb6jVb//T7EfTmbXD2aEP5c2ZdJr9YTDfcTE1ZH+'

const validMetadata = `sha256-${hash256} sha384-${invalidHash384} sha512-${hash512}`
const result = parseMetadata(validMetadata)

assert.deepEqual(result, [
t.assert.deepEqual(result, [
{ alg: 'sha256', val: hash256 },
{ alg: 'sha384', val: invalidHash384 },
{ alg: 'sha512', val: hash512 }
Expand Down
Loading