mirror of https://github.com/abpframework/abp.git
7 changed files with 178 additions and 15 deletions
@ -0,0 +1,32 @@ |
|||||
|
export abstract class ContentSecurityStrategy { |
||||
|
constructor(public nonce?: string) {} |
||||
|
|
||||
|
abstract applyCSP(element: HTMLScriptElement | HTMLStyleElement): void; |
||||
|
} |
||||
|
|
||||
|
export class StrictContentSecurityStrategy extends ContentSecurityStrategy { |
||||
|
constructor(nonce: string) { |
||||
|
super(nonce); |
||||
|
} |
||||
|
|
||||
|
applyCSP(element: HTMLScriptElement | HTMLStyleElement) { |
||||
|
element.setAttribute('nonce', this.nonce); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export class LooseContentSecurityStrategy extends ContentSecurityStrategy { |
||||
|
constructor() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
applyCSP(_: HTMLScriptElement | HTMLStyleElement) {} |
||||
|
} |
||||
|
|
||||
|
export const CONTENT_SECURITY_STRATEGY = { |
||||
|
Loose() { |
||||
|
return new LooseContentSecurityStrategy(); |
||||
|
}, |
||||
|
Strict(nonce: string) { |
||||
|
return new StrictContentSecurityStrategy(nonce); |
||||
|
}, |
||||
|
}; |
||||
@ -1,3 +1,4 @@ |
|||||
|
export * from './content-security.strategy'; |
||||
export * from './cross-origin.strategy'; |
export * from './cross-origin.strategy'; |
||||
export * from './dom.strategy'; |
export * from './dom.strategy'; |
||||
export * from './loading.strategy'; |
export * from './loading.strategy'; |
||||
|
|||||
@ -0,0 +1,41 @@ |
|||||
|
import { |
||||
|
CONTENT_SECURITY_STRATEGY, |
||||
|
LooseContentSecurityStrategy, |
||||
|
StrictContentSecurityStrategy, |
||||
|
} from '../strategies'; |
||||
|
import { uuid } from '../utils'; |
||||
|
|
||||
|
describe('LooseContentSecurityStrategy', () => { |
||||
|
describe('#applyCSP', () => { |
||||
|
it('should not set nonce attribute', () => { |
||||
|
const strategy = new LooseContentSecurityStrategy(); |
||||
|
const element = document.createElement('link'); |
||||
|
strategy.applyCSP(element); |
||||
|
|
||||
|
expect(element.getAttribute('nonce')).toBeNull(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('StrictContentSecurityStrategy', () => { |
||||
|
describe('#applyCSP', () => { |
||||
|
it('should set nonce attribute', () => { |
||||
|
const nonce = uuid(); |
||||
|
const strategy = new StrictContentSecurityStrategy(nonce); |
||||
|
const element = document.createElement('link'); |
||||
|
strategy.applyCSP(element); |
||||
|
|
||||
|
expect(element.getAttribute('nonce')).toBe(nonce); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('CONTENT_SECURITY_STRATEGY', () => { |
||||
|
test.each` |
||||
|
name | Strategy | nonce |
||||
|
${'Loose'} | ${LooseContentSecurityStrategy} | ${undefined} |
||||
|
${'Strict'} | ${StrictContentSecurityStrategy} | ${uuid()} |
||||
|
`('should successfully map $name to $Strategy.name', ({ name, Strategy, nonce }) => {
|
||||
|
expect(CONTENT_SECURITY_STRATEGY[name](nonce)).toEqual(new Strategy(nonce)); |
||||
|
}); |
||||
|
}); |
||||
Loading…
Reference in new issue