csp.tsx 5.5 KB

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