hpkp.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {RouteComponentProps} from 'react-router';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import Panel from 'sentry/components/panels/panel';
  4. import PanelBody from 'sentry/components/panels/panelBody';
  5. import PanelHeader from 'sentry/components/panels/panelHeader';
  6. import PreviewFeature from 'sentry/components/previewFeature';
  7. import {t, tct} from 'sentry/locale';
  8. import {Organization, ProjectKey} from 'sentry/types';
  9. import routeTitleGen from 'sentry/utils/routeTitle';
  10. import withOrganization from 'sentry/utils/withOrganization';
  11. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  12. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  13. import ReportUri, {
  14. getSecurityDsn,
  15. } from 'sentry/views/settings/projectSecurityHeaders/reportUri';
  16. type Props = RouteComponentProps<{projectId: string}, {}> & {
  17. organization: Organization;
  18. };
  19. type State = {
  20. keyList: null | ProjectKey[];
  21. } & DeprecatedAsyncView['state'];
  22. class ProjectHpkpReports extends DeprecatedAsyncView<Props, State> {
  23. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  24. const {organization} = this.props;
  25. const {projectId} = this.props.params;
  26. return [['keyList', `/projects/${organization.slug}/${projectId}/keys/`]];
  27. }
  28. getTitle() {
  29. const {projectId} = this.props.params;
  30. return routeTitleGen(t('HTTP Public Key Pinning (HPKP)'), projectId, false);
  31. }
  32. getInstructions(keyList: ProjectKey[]) {
  33. return (
  34. 'def middleware(request, response):\n' +
  35. " response['Public-Key-Pins'] = \\\n" +
  36. ' \'pin-sha256="cUPcTAZWKaASuYWhhneDttWpY3oBAkE3h2+soZS7sWs="; \' \\\n' +
  37. ' \'pin-sha256="M8HztCzM3elUxkcjR2S5P4hhyBNf6lHkmjAHKhpGPWE="; \' \\\n' +
  38. " 'max-age=5184000; includeSubDomains; ' \\\n" +
  39. ` \'report-uri="${getSecurityDsn(keyList)}"\' \n` +
  40. ' return response\n'
  41. );
  42. }
  43. getReportOnlyInstructions(keyList: ProjectKey[]) {
  44. return (
  45. 'def middleware(request, response):\n' +
  46. " response['Public-Key-Pins-Report-Only'] = \\\n" +
  47. ' \'pin-sha256="cUPcTAZWKaASuYWhhneDttWpY3oBAkE3h2+soZS7sWs="; \' \\\n' +
  48. ' \'pin-sha256="M8HztCzM3elUxkcjR2S5P4hhyBNf6lHkmjAHKhpGPWE="; \' \\\n' +
  49. " 'max-age=5184000; includeSubDomains; ' \\\n" +
  50. ` \'report-uri="${getSecurityDsn(keyList)}"\' \n` +
  51. ' return response\n'
  52. );
  53. }
  54. renderBody() {
  55. const {organization, params} = this.props;
  56. const {keyList} = this.state;
  57. if (!keyList) {
  58. return null;
  59. }
  60. return (
  61. <div>
  62. <SettingsPageHeader title={t('HTTP Public Key Pinning')} />
  63. <PreviewFeature />
  64. <ReportUri
  65. keyList={keyList}
  66. orgId={organization.slug}
  67. projectId={params.projectId}
  68. />
  69. <Panel>
  70. <PanelHeader>{t('About')}</PanelHeader>
  71. <PanelBody withPadding>
  72. <p>
  73. {tct(
  74. `[link:HTTP Public Key Pinning]
  75. (HPKP) is a security feature that tells a web client to associate a specific
  76. cryptographic public key with a certain web server to decrease the risk of MITM
  77. attacks with forged certificates. It's enforced by browser vendors, and Sentry
  78. supports capturing violations using the standard reporting hooks.`,
  79. {
  80. link: (
  81. <ExternalLink href="https://en.wikipedia.org/wiki/HTTP_Public_Key_Pinning" />
  82. ),
  83. }
  84. )}
  85. </p>
  86. <p>
  87. {t(
  88. `To configure HPKP reports
  89. in Sentry, you'll need to send a header from your server describing your
  90. policy, as well specifying the authenticated Sentry endpoint.`
  91. )}
  92. </p>
  93. <p>
  94. {t(
  95. 'For example, in Python you might achieve this via a simple web middleware'
  96. )}
  97. </p>
  98. <pre>{this.getInstructions(keyList)}</pre>
  99. <p>
  100. {t(`Alternatively you can setup HPKP reports to simply send reports rather than
  101. actually enforcing the policy`)}
  102. </p>
  103. <pre>{this.getReportOnlyInstructions(keyList)}</pre>
  104. <p>
  105. {tct(
  106. `We recommend setting this up to only run on a percentage of requests, as
  107. otherwise you may find that you've quickly exhausted your quota. For more
  108. information, take a look at [link:the documentation on MDN].`,
  109. {
  110. link: (
  111. <ExternalLink href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning" />
  112. ),
  113. }
  114. )}
  115. </p>
  116. </PanelBody>
  117. </Panel>
  118. </div>
  119. );
  120. }
  121. }
  122. export default withOrganization(ProjectHpkpReports);