hpkp.tsx 4.5 KB

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