@@ -74,6 +74,9 @@ type Policy struct {
7474 // When true, add crossorigin="anonymous" to HTML audio, img, link, script, and video tags
7575 requireCrossOriginAnonymous bool
7676
77+ // When true, add and filter sandbox attribute on iframe tags
78+ requireSandboxOnIFrame map [string ]bool
79+
7780 // When true add target="_blank" to fully qualified links
7881 // Will add for href="http://foo"
7982 // Will skip for href="/foo" or href="foo"
@@ -189,6 +192,25 @@ type stylePolicyBuilder struct {
189192
190193type urlPolicy func (url * url.URL ) (allowUrl bool )
191194
195+ type SandboxValue int64
196+
197+ const (
198+ SandboxAllowDownloads SandboxValue = iota
199+ SandboxAllowDownloadsWithoutUserActivation
200+ SandboxAllowForms
201+ SandboxAllowModals
202+ SandboxAllowOrientationLock
203+ SandboxAllowPointerLock
204+ SandboxAllowPopups
205+ SandboxAllowPopupsToEscapeSandbox
206+ SandboxAllowPresentation
207+ SandboxAllowSameOrigin
208+ SandboxAllowScripts
209+ SandboxAllowStorageAccessByUserActivation
210+ SandboxAllowTopNavigation
211+ SandboxAllowTopNavigationByUserActivation
212+ )
213+
192214// init initializes the maps if this has not been done already
193215func (p * Policy ) init () {
194216 if ! p .initialized {
@@ -680,6 +702,58 @@ func (p *Policy) AllowURLSchemeWithCustomPolicy(
680702 return p
681703}
682704
705+ // RequireSandboxOnIFrame will result in all iframe tags having a sandbox="" tag
706+ // Any sandbox values not specified here will be filtered from the generated HTML
707+ func (p * Policy ) RequireSandboxOnIFrame (vals ... SandboxValue ) {
708+ p .requireSandboxOnIFrame = make (map [string ]bool )
709+
710+ for val := range vals {
711+ switch SandboxValue (val ) {
712+ case SandboxAllowDownloads :
713+ p .requireSandboxOnIFrame ["allow-downloads" ] = true
714+
715+ case SandboxAllowDownloadsWithoutUserActivation :
716+ p .requireSandboxOnIFrame ["allow-downloads-without-user-activation" ] = true
717+
718+ case SandboxAllowForms :
719+ p .requireSandboxOnIFrame ["allow-forms" ] = true
720+
721+ case SandboxAllowModals :
722+ p .requireSandboxOnIFrame ["allow-modals" ] = true
723+
724+ case SandboxAllowOrientationLock :
725+ p .requireSandboxOnIFrame ["allow-orientation-lock" ] = true
726+
727+ case SandboxAllowPointerLock :
728+ p .requireSandboxOnIFrame ["allow-pointer-lock" ] = true
729+
730+ case SandboxAllowPopups :
731+ p .requireSandboxOnIFrame ["allow-popups" ] = true
732+
733+ case SandboxAllowPopupsToEscapeSandbox :
734+ p .requireSandboxOnIFrame ["allow-popups-to-escape-sandbox" ] = true
735+
736+ case SandboxAllowPresentation :
737+ p .requireSandboxOnIFrame ["allow-presentation" ] = true
738+
739+ case SandboxAllowSameOrigin :
740+ p .requireSandboxOnIFrame ["allow-same-origin" ] = true
741+
742+ case SandboxAllowScripts :
743+ p .requireSandboxOnIFrame ["allow-scripts" ] = true
744+
745+ case SandboxAllowStorageAccessByUserActivation :
746+ p .requireSandboxOnIFrame ["allow-storage-access-by-user-activation" ] = true
747+
748+ case SandboxAllowTopNavigation :
749+ p .requireSandboxOnIFrame ["allow-top-navigation" ] = true
750+
751+ case SandboxAllowTopNavigationByUserActivation :
752+ p .requireSandboxOnIFrame ["allow-top-navigation-by-user-activation" ] = true
753+ }
754+ }
755+ }
756+
683757// AddSpaceWhenStrippingTag states whether to add a single space " " when
684758// removing tags that are not allowed by the policy.
685759//
0 commit comments