Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

csp.js 2.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  1. // This module defines a Content Security Policy (CSP) to disallow
  2. // inline scripts and content from untrusted sources.
  3. const { contentSecurityPolicy } = require('helmet')
  4. const isArchivedVersion = require('../lib/is-archived-version')
  5. const versionSatisfiesRange = require('../lib/version-satisfies-range')
  6. const AZURE_STORAGE_URL = 'githubdocs.azureedge.net'
  7. // module.exports = contentSecurityPolicy({
  8. module.exports = function csp (req, res, next) {
  9. const csp = {
  10. directives: {
  11. defaultSrc: ["'none'"],
  12. connectSrc: [
  13. "'self'",
  14. '*.algolia.net',
  15. '*.algolianet.com'
  16. ],
  17. fontSrc: [
  18. "'self'",
  19. 'data:',
  20. AZURE_STORAGE_URL
  21. ],
  22. imgSrc: [
  23. "'self'",
  24. 'data:',
  25. 'github.githubassets.com',
  26. AZURE_STORAGE_URL,
  27. 'placehold.it',
  28. '*.githubusercontent.com',
  29. 'github.com'
  30. ],
  31. objectSrc: [
  32. "'self'"
  33. ],
  34. scriptSrc: [
  35. "'self'",
  36. 'data:'
  37. ],
  38. frameSrc: [ // exceptions for GraphQL Explorer
  39. 'https://graphql-explorer.githubapp.com', // production env
  40. 'https://graphql.github.com/',
  41. 'http://localhost:3000', // development env
  42. 'https://www.youtube-nocookie.com'
  43. ],
  44. styleSrc: [
  45. "'self'",
  46. "'unsafe-inline'"
  47. ],
  48. childSrc: [
  49. "'self'" // exception for search in deprecated GHE versions
  50. ]
  51. }
  52. }
  53. const { requestedVersion } = isArchivedVersion(req)
  54. // Exception for Algolia instantsearch in deprecated Enterprise docs (Node.js era)
  55. if (versionSatisfiesRange(requestedVersion, '<=2.19') && versionSatisfiesRange(requestedVersion, '>2.12')) {
  56. csp.directives.scriptSrc.push("'unsafe-eval'", "'unsafe-inline'", 'http://www.google-analytics.com', 'https://ssl.google-analytics.com')
  57. csp.directives.connectSrc.push('https://www.google-analytics.com')
  58. csp.directives.imgSrc.push('http://www.google-analytics.com', 'https://ssl.google-analytics.com')
  59. }
  60. // Exception for search in deprecated Enterprise docs <=2.12 (static site era)
  61. if (versionSatisfiesRange(requestedVersion, '<=2.12')) {
  62. csp.directives.scriptSrc.push("'unsafe-inline'")
  63. }
  64. return contentSecurityPolicy(csp)(req, res, next)
  65. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...