csp.tsx 5.3 KB

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