1
0

permissionpolicy.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package permissionpolicy
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. )
  7. /*
  8. Permisson Policy
  9. This is a permission policy header modifier that changes
  10. the request permission related policy fields
  11. author: tobychui
  12. */
  13. type PermissionsPolicy struct {
  14. Accelerometer []string `json:"accelerometer"`
  15. AmbientLightSensor []string `json:"ambient_light_sensor"`
  16. Autoplay []string `json:"autoplay"`
  17. Battery []string `json:"battery"`
  18. Camera []string `json:"camera"`
  19. CrossOriginIsolated []string `json:"cross_origin_isolated"`
  20. DisplayCapture []string `json:"display_capture"`
  21. DocumentDomain []string `json:"document_domain"`
  22. EncryptedMedia []string `json:"encrypted_media"`
  23. ExecutionWhileNotRendered []string `json:"execution_while_not_rendered"`
  24. ExecutionWhileOutOfView []string `json:"execution_while_out_of_viewport"`
  25. Fullscreen []string `json:"fullscreen"`
  26. Geolocation []string `json:"geolocation"`
  27. Gyroscope []string `json:"gyroscope"`
  28. KeyboardMap []string `json:"keyboard_map"`
  29. Magnetometer []string `json:"magnetometer"`
  30. Microphone []string `json:"microphone"`
  31. Midi []string `json:"midi"`
  32. NavigationOverride []string `json:"navigation_override"`
  33. Payment []string `json:"payment"`
  34. PictureInPicture []string `json:"picture_in_picture"`
  35. PublicKeyCredentialsGet []string `json:"publickey_credentials_get"`
  36. ScreenWakeLock []string `json:"screen_wake_lock"`
  37. SyncXHR []string `json:"sync_xhr"`
  38. USB []string `json:"usb"`
  39. WebShare []string `json:"web_share"`
  40. XRSpatialTracking []string `json:"xr_spatial_tracking"`
  41. ClipboardRead []string `json:"clipboard_read"`
  42. ClipboardWrite []string `json:"clipboard_write"`
  43. Gamepad []string `json:"gamepad"`
  44. SpeakerSelection []string `json:"speaker_selection"`
  45. ConversionMeasurement []string `json:"conversion_measurement"`
  46. FocusWithoutUserActivation []string `json:"focus_without_user_activation"`
  47. HID []string `json:"hid"`
  48. IdleDetection []string `json:"idle_detection"`
  49. InterestCohort []string `json:"interest_cohort"`
  50. Serial []string `json:"serial"`
  51. SyncScript []string `json:"sync_script"`
  52. TrustTokenRedemption []string `json:"trust_token_redemption"`
  53. Unload []string `json:"unload"`
  54. WindowPlacement []string `json:"window_placement"`
  55. VerticalScroll []string `json:"vertical_scroll"`
  56. }
  57. // GetDefaultPermissionPolicy returns a PermissionsPolicy struct with all policies set to *
  58. func GetDefaultPermissionPolicy() *PermissionsPolicy {
  59. return &PermissionsPolicy{
  60. Accelerometer: []string{"*"},
  61. AmbientLightSensor: []string{"*"},
  62. Autoplay: []string{"*"},
  63. Battery: []string{"*"},
  64. Camera: []string{"*"},
  65. CrossOriginIsolated: []string{"*"},
  66. DisplayCapture: []string{"*"},
  67. DocumentDomain: []string{"*"},
  68. EncryptedMedia: []string{"*"},
  69. ExecutionWhileNotRendered: []string{"*"},
  70. ExecutionWhileOutOfView: []string{"*"},
  71. Fullscreen: []string{"*"},
  72. Geolocation: []string{"*"},
  73. Gyroscope: []string{"*"},
  74. KeyboardMap: []string{"*"},
  75. Magnetometer: []string{"*"},
  76. Microphone: []string{"*"},
  77. Midi: []string{"*"},
  78. NavigationOverride: []string{"*"},
  79. Payment: []string{"*"},
  80. PictureInPicture: []string{"*"},
  81. PublicKeyCredentialsGet: []string{"*"},
  82. ScreenWakeLock: []string{"*"},
  83. SyncXHR: []string{"*"},
  84. USB: []string{"*"},
  85. WebShare: []string{"*"},
  86. XRSpatialTracking: []string{"*"},
  87. ClipboardRead: []string{"*"},
  88. ClipboardWrite: []string{"*"},
  89. Gamepad: []string{"*"},
  90. SpeakerSelection: []string{"*"},
  91. ConversionMeasurement: []string{"*"},
  92. FocusWithoutUserActivation: []string{"*"},
  93. HID: []string{"*"},
  94. IdleDetection: []string{"*"},
  95. InterestCohort: []string{"*"},
  96. Serial: []string{"*"},
  97. SyncScript: []string{"*"},
  98. TrustTokenRedemption: []string{"*"},
  99. Unload: []string{"*"},
  100. WindowPlacement: []string{"*"},
  101. VerticalScroll: []string{"*"},
  102. }
  103. }
  104. // InjectPermissionPolicyHeader inject the permission policy into headers
  105. func InjectPermissionPolicyHeader(w http.ResponseWriter, policy *PermissionsPolicy) {
  106. //Keep the original Permission Policy if exists, or there are no policy given
  107. if policy == nil || w.Header().Get("Permissions-Policy") != "" {
  108. return
  109. }
  110. policyHeader := []string{}
  111. // Helper function to add policy directives
  112. addDirective := func(name string, sources []string) {
  113. if len(sources) > 0 {
  114. if sources[0] == "*" {
  115. //Allow all
  116. policyHeader = append(policyHeader, fmt.Sprintf("%s=%s", name, "*"))
  117. } else {
  118. //Other than "self" which do not need double quote, others domain need double quote in place
  119. formatedSources := []string{}
  120. for _, source := range sources {
  121. if source == "self" {
  122. formatedSources = append(formatedSources, "self")
  123. } else {
  124. formatedSources = append(formatedSources, "\""+source+"\"")
  125. }
  126. }
  127. policyHeader = append(policyHeader, fmt.Sprintf("%s=(%s)", name, strings.Join(formatedSources, " ")))
  128. }
  129. } else {
  130. //There are no setting for this field. Assume no permission
  131. policyHeader = append(policyHeader, fmt.Sprintf("%s=()", name))
  132. }
  133. }
  134. // Add each policy directive to the header
  135. addDirective("accelerometer", policy.Accelerometer)
  136. addDirective("ambient-light-sensor", policy.AmbientLightSensor)
  137. addDirective("autoplay", policy.Autoplay)
  138. addDirective("battery", policy.Battery)
  139. addDirective("camera", policy.Camera)
  140. addDirective("cross-origin-isolated", policy.CrossOriginIsolated)
  141. addDirective("display-capture", policy.DisplayCapture)
  142. addDirective("document-domain", policy.DocumentDomain)
  143. addDirective("encrypted-media", policy.EncryptedMedia)
  144. addDirective("execution-while-not-rendered", policy.ExecutionWhileNotRendered)
  145. addDirective("execution-while-out-of-viewport", policy.ExecutionWhileOutOfView)
  146. addDirective("fullscreen", policy.Fullscreen)
  147. addDirective("geolocation", policy.Geolocation)
  148. addDirective("gyroscope", policy.Gyroscope)
  149. addDirective("keyboard-map", policy.KeyboardMap)
  150. addDirective("magnetometer", policy.Magnetometer)
  151. addDirective("microphone", policy.Microphone)
  152. addDirective("midi", policy.Midi)
  153. addDirective("navigation-override", policy.NavigationOverride)
  154. addDirective("payment", policy.Payment)
  155. addDirective("picture-in-picture", policy.PictureInPicture)
  156. addDirective("publickey-credentials-get", policy.PublicKeyCredentialsGet)
  157. addDirective("screen-wake-lock", policy.ScreenWakeLock)
  158. addDirective("sync-xhr", policy.SyncXHR)
  159. addDirective("usb", policy.USB)
  160. addDirective("web-share", policy.WebShare)
  161. addDirective("xr-spatial-tracking", policy.XRSpatialTracking)
  162. addDirective("clipboard-read", policy.ClipboardRead)
  163. addDirective("clipboard-write", policy.ClipboardWrite)
  164. addDirective("gamepad", policy.Gamepad)
  165. addDirective("speaker-selection", policy.SpeakerSelection)
  166. addDirective("conversion-measurement", policy.ConversionMeasurement)
  167. addDirective("focus-without-user-activation", policy.FocusWithoutUserActivation)
  168. addDirective("hid", policy.HID)
  169. addDirective("idle-detection", policy.IdleDetection)
  170. addDirective("interest-cohort", policy.InterestCohort)
  171. addDirective("serial", policy.Serial)
  172. addDirective("sync-script", policy.SyncScript)
  173. addDirective("trust-token-redemption", policy.TrustTokenRedemption)
  174. addDirective("unload", policy.Unload)
  175. addDirective("window-placement", policy.WindowPlacement)
  176. addDirective("vertical-scroll", policy.VerticalScroll)
  177. // Join the directives and set the header
  178. policyHeaderValue := strings.Join(policyHeader, ", ")
  179. //Inject the new policy into the header
  180. w.Header().Set("Permissions-Policy", policyHeaderValue)
  181. }