csp.tsx 5.5 KB

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