hpkp.tsx 4.7 KB

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