csp.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {RouteComponentProps} from 'react-router';
  2. import Access from 'sentry/components/acl/access';
  3. import Form from 'sentry/components/forms/form';
  4. import JsonForm from 'sentry/components/forms/jsonForm';
  5. import ExternalLink from 'sentry/components/links/externalLink';
  6. import {Panel, PanelBody, PanelHeader} from 'sentry/components/panels';
  7. import PreviewFeature from 'sentry/components/previewFeature';
  8. import formGroups from 'sentry/data/forms/cspReports';
  9. import {t, tct} from 'sentry/locale';
  10. import {Project, ProjectKey} from 'sentry/types';
  11. import routeTitleGen from 'sentry/utils/routeTitle';
  12. import AsyncView from 'sentry/views/asyncView';
  13. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  14. import ReportUri, {
  15. getSecurityDsn,
  16. } from 'sentry/views/settings/projectSecurityHeaders/reportUri';
  17. type Props = RouteComponentProps<{orgId: string; projectId: string}, {}>;
  18. type State = {
  19. keyList: null | ProjectKey[];
  20. project: null | Project;
  21. } & AsyncView['state'];
  22. export default class ProjectCspReports extends AsyncView<Props, State> {
  23. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  24. const {orgId, projectId} = this.props.params;
  25. return [
  26. ['keyList', `/projects/${orgId}/${projectId}/keys/`],
  27. ['project', `/projects/${orgId}/${projectId}/`],
  28. ];
  29. }
  30. getTitle() {
  31. const {projectId} = this.props.params;
  32. return routeTitleGen(t('Content Security Policy (CSP)'), projectId, false);
  33. }
  34. getInstructions(keyList: ProjectKey[]) {
  35. return (
  36. 'def middleware(request, response):\n' +
  37. " response['Content-Security-Policy'] = \\\n" +
  38. ' "default-src *; " \\\n' +
  39. " \"script-src 'self' 'unsafe-eval' 'unsafe-inline' cdn.example.com cdn.ravenjs.com; \" \\\n" +
  40. " \"style-src 'self' 'unsafe-inline' cdn.example.com; \" \\\n" +
  41. ' "img-src * data:; " \\\n' +
  42. ' "report-uri ' +
  43. getSecurityDsn(keyList) +
  44. '"\n' +
  45. ' return response\n'
  46. );
  47. }
  48. getReportOnlyInstructions(keyList: ProjectKey[]) {
  49. return (
  50. 'def middleware(request, response):\n' +
  51. " response['Content-Security-Policy-Report-Only'] = \\\n" +
  52. ' "default-src \'self\'; " \\\n' +
  53. ' "report-uri ' +
  54. getSecurityDsn(keyList) +
  55. '"\n' +
  56. ' return response\n'
  57. );
  58. }
  59. renderBody() {
  60. const {orgId, projectId} = this.props.params;
  61. const {project, keyList} = this.state;
  62. if (!keyList || !project) {
  63. return null;
  64. }
  65. return (
  66. <div>
  67. <SettingsPageHeader title={t('Content Security Policy')} />
  68. <PreviewFeature />
  69. <ReportUri keyList={keyList} orgId={orgId} projectId={projectId} />
  70. <Form
  71. saveOnBlur
  72. apiMethod="PUT"
  73. initialData={project.options}
  74. apiEndpoint={`/projects/${orgId}/${projectId}/`}
  75. >
  76. <Access access={['project:write']}>
  77. {({hasAccess}) => <JsonForm disabled={!hasAccess} forms={formGroups} />}
  78. </Access>
  79. </Form>
  80. <Panel>
  81. <PanelHeader>{t('About')}</PanelHeader>
  82. <PanelBody withPadding>
  83. <p>
  84. {tct(
  85. `[link:Content Security Policy]
  86. (CSP) is a security standard which helps prevent cross-site scripting (XSS),
  87. clickjacking and other code injection attacks resulting from execution of
  88. malicious content in the trusted web page context. It's enforced by browser
  89. vendors, and Sentry supports capturing CSP violations using the standard
  90. reporting hooks.`,
  91. {
  92. link: (
  93. <ExternalLink href="https://en.wikipedia.org/wiki/Content_Security_Policy" />
  94. ),
  95. }
  96. )}
  97. </p>
  98. <p>
  99. {tct(
  100. `To configure [csp:CSP] reports
  101. in Sentry, you'll need to send a header from your server describing your
  102. policy, as well specifying the authenticated Sentry endpoint.`,
  103. {
  104. csp: <abbr title="Content Security Policy" />,
  105. }
  106. )}
  107. </p>
  108. <p>
  109. {t(
  110. 'For example, in Python you might achieve this via a simple web middleware'
  111. )}
  112. </p>
  113. <pre>{this.getInstructions(keyList)}</pre>
  114. <p>
  115. {t(`Alternatively you can setup CSP reports to simply send reports rather than
  116. actually enforcing the policy`)}
  117. </p>
  118. <pre>{this.getReportOnlyInstructions(keyList)}</pre>
  119. <p>
  120. {tct(
  121. `We recommend setting this up to only run on a percentage of requests, as
  122. otherwise you may find that you've quickly exhausted your quota. For more
  123. information, take a look at [link:the article on html5rocks.com].`,
  124. {
  125. link: (
  126. <a href="http://www.html5rocks.com/en/tutorials/security/content-security-policy/" />
  127. ),
  128. }
  129. )}
  130. </p>
  131. </PanelBody>
  132. </Panel>
  133. </div>
  134. );
  135. }
  136. }