Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should only satisfy caveats that get more restrictive', () => {
const interval = 1000
const condition = 'expiration'
const firstCaveat = new Caveat({
condition,
value: Date.now() + interval,
})
const secondCaveat = new Caveat({
condition,
value: Date.now() + interval / 2, // more restrictive time
})
expect(satisfier).to.have.property('satisfyPrevious')
let isValid = verifyCaveats([firstCaveat, secondCaveat], satisfier)
expect(isValid, 'Expected caveats w/ increasing restrictiveness to pass')
.to.be.true
isValid = verifyCaveats([secondCaveat, firstCaveat], satisfier)
it('should only satisfy caveats that get more restrictive', () => {
const interval = 1000
const condition = 'expiration'
const firstCaveat = new Caveat({
condition,
value: Date.now() + interval,
})
const secondCaveat = new Caveat({
condition,
value: Date.now() + interval / 2, // more restrictive time
})
expect(satisfier).to.have.property('satisfyPrevious')
let isValid = verifyCaveats([firstCaveat, secondCaveat], satisfier)
expect(isValid, 'Expected caveats w/ increasing restrictiveness to pass')
.to.be.true
isValid = verifyCaveats([secondCaveat, firstCaveat], satisfier)
expect(
isValid,
'Expected caveats w/ decreasingly restrictive expirations to fail'
it('should validate expiration caveat', () => {
const validCaveat = new Caveat({
condition: 'expiration',
value: Date.now() + 1000,
})
expect(validCaveat.condition).to.equal(satisfier.condition)
let isValid = satisfier.satisfyFinal(validCaveat, {} as Request)
expect(isValid, 'Valid caveat should have been satisfied').to.be.true
const expired = new Caveat({
condition: 'expiration',
value: Date.now() - 100,
})
expect(expired.condition).to.equal(satisfier.condition)
isValid = satisfier.satisfyFinal(expired, {} as Request)
expect(isValid, 'expired caveat should be invalid').to.be.false
})
it('should validate expiration caveat', () => {
const validCaveat = new Caveat({
condition: 'expiration',
value: Date.now() + 1000,
})
expect(validCaveat.condition).to.equal(satisfier.condition)
let isValid = satisfier.satisfyFinal(validCaveat, {} as Request)
expect(isValid, 'Valid caveat should have been satisfied').to.be.true
const expired = new Caveat({
condition: 'expiration',
value: Date.now() - 100,
})
expect(expired.condition).to.equal(satisfier.condition)
isValid = satisfier.satisfyFinal(expired, {} as Request)
expect(isValid, 'expired caveat should be invalid').to.be.false
})
it('should not support additional caveats from other origin', () => {
const firstCaveat = new Caveat({ condition, value: '84.123.45.2' })
const secondCaveat = new Caveat({ condition, value: '74.321.5.27' })
const request = { ip: firstCaveat.value, boltwallConfig: config }
const isValid = verifyCaveats(
[firstCaveat, secondCaveat],
satisfier,
request
)
expect(isValid).to.be.false
})
})
it('should create a caveat that restricts access by ip and be able to satisfy it', () => {
const origin = '180.1.23.45'
const expected = new Caveat({ condition, value: origin })
const requests = [
{
name: 'request from proxy',
req: { headers: { 'x-forwarded-for': origin } },
},
{
name: 'request from proxy with array of ips',
req: { headers: { 'x-forwarded-for': [origin, '127.0.0.1'] } },
},
{
name: 'request with ip (express)',
req: { ip: origin },
},
{
name: 'request without express',
const secondCaveatGetter = (): string => {
const caveat = new Caveat({
condition: 'middleName',
value: 'danger',
})
return caveat.encode()
}
const firstCaveat = singleCaveatGetter(request)
const getOriginCaveat: CaveatGetter = (req: Request) => {
const origin = getOriginFromRequest(req)
const caveat = new Caveat({ condition: 'ip', value: origin })
return caveat.encode()
}