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.5 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
72
73
74
  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. // For use during development only! This allows us to use a performant webpack devtool setting (eval)
  38. // https://webpack.js.org/configuration/devtool/#devtool
  39. process.env.NODE_ENV === 'development' && "'unsafe-eval'"
  40. ].filter(Boolean),
  41. frameSrc: [ // exceptions for GraphQL Explorer
  42. 'https://graphql-explorer.githubapp.com', // production env
  43. 'https://graphql.github.com/',
  44. 'http://localhost:3000', // development env
  45. 'https://www.youtube-nocookie.com'
  46. ],
  47. styleSrc: [
  48. "'self'",
  49. "'unsafe-inline'"
  50. ],
  51. childSrc: [
  52. "'self'" // exception for search in deprecated GHE versions
  53. ]
  54. }
  55. }
  56. const { requestedVersion } = isArchivedVersion(req)
  57. // Exception for Algolia instantsearch in deprecated Enterprise docs (Node.js era)
  58. if (versionSatisfiesRange(requestedVersion, '<=2.19') && versionSatisfiesRange(requestedVersion, '>2.12')) {
  59. csp.directives.scriptSrc.push("'unsafe-eval'", "'unsafe-inline'", 'http://www.google-analytics.com', 'https://ssl.google-analytics.com')
  60. csp.directives.connectSrc.push('https://www.google-analytics.com')
  61. csp.directives.imgSrc.push('http://www.google-analytics.com', 'https://ssl.google-analytics.com')
  62. }
  63. // Exception for search in deprecated Enterprise docs <=2.12 (static site era)
  64. if (versionSatisfiesRange(requestedVersion, '<=2.12')) {
  65. csp.directives.scriptSrc.push("'unsafe-inline'")
  66. }
  67. return contentSecurityPolicy(csp)(req, res, next)
  68. }
Tip!

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

Comments

Loading...