prism-csp.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Original by Scott Helme.
  3. *
  4. * Reference: https://scotthelme.co.uk/csp-cheat-sheet/
  5. *
  6. * Supports the following:
  7. * - https://www.w3.org/TR/CSP1/
  8. * - https://www.w3.org/TR/CSP2/
  9. * - https://www.w3.org/TR/CSP3/
  10. */
  11. (function (Prism) {
  12. /**
  13. * @param {string} source
  14. * @returns {RegExp}
  15. */
  16. function value(source) {
  17. return RegExp(/([ \t])/.source + '(?:' + source + ')' + /(?=[\s;]|$)/.source, 'i');
  18. }
  19. Prism.languages.csp = {
  20. 'directive': {
  21. pattern: /(^|[\s;])(?:base-uri|block-all-mixed-content|(?:child|connect|default|font|frame|img|manifest|media|object|prefetch|script|style|worker)-src|disown-opener|form-action|frame-(?:ancestors|options)|input-protection(?:-(?:clip|selectors))?|navigate-to|plugin-types|policy-uri|referrer|reflected-xss|report-(?:to|uri)|require-sri-for|sandbox|(?:script|style)-src-(?:attr|elem)|upgrade-insecure-requests)(?=[\s;]|$)/i,
  22. lookbehind: true,
  23. alias: 'property'
  24. },
  25. 'scheme': {
  26. pattern: value(/[a-z][a-z0-9.+-]*:/.source),
  27. lookbehind: true
  28. },
  29. 'none': {
  30. pattern: value(/'none'/.source),
  31. lookbehind: true,
  32. alias: 'keyword'
  33. },
  34. 'nonce': {
  35. pattern: value(/'nonce-[-+/\w=]+'/.source),
  36. lookbehind: true,
  37. alias: 'number'
  38. },
  39. 'hash': {
  40. pattern: value(/'sha(?:256|384|512)-[-+/\w=]+'/.source),
  41. lookbehind: true,
  42. alias: 'number'
  43. },
  44. 'host': {
  45. pattern: value(
  46. /[a-z][a-z0-9.+-]*:\/\/[^\s;,']*/.source +
  47. '|' +
  48. /\*[^\s;,']*/.source +
  49. '|' +
  50. /[a-z0-9-]+(?:\.[a-z0-9-]+)+(?::[\d*]+)?(?:\/[^\s;,']*)?/.source
  51. ),
  52. lookbehind: true,
  53. alias: 'url',
  54. inside: {
  55. 'important': /\*/
  56. }
  57. },
  58. 'keyword': [
  59. {
  60. pattern: value(/'unsafe-[a-z-]+'/.source),
  61. lookbehind: true,
  62. alias: 'unsafe'
  63. },
  64. {
  65. pattern: value(/'[a-z-]+'/.source),
  66. lookbehind: true,
  67. alias: 'safe'
  68. },
  69. ],
  70. 'punctuation': /;/
  71. };
  72. }(Prism));