-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(server): Add Reporting API server helpers #14044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
timfish
wants to merge
7
commits into
develop
Choose a base branch
from
timfish/feat/reporting-api
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6861574
feat(server): Add Reporting API server helpers
timfish ca35199
Lint
timfish e214d08
Merge branch 'develop' into timfish/feat/reporting-api
timfish 003e611
Add test
timfish 379063f
Rename exported function and remove stack parsing
timfish 70e9ad8
simplify usage
timfish 14fdfa0
Fix linting
timfish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/reporting-api/index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head> | ||
<title>Reporting API</title> | ||
</head> | ||
<body> | ||
<!-- <script src="//example.com/script.js"></script> --> | ||
<h1>Reporting API</h1> | ||
</body> | ||
</html> |
27 changes: 27 additions & 0 deletions
27
dev-packages/node-integration-tests/suites/reporting-api/localhost-key.pem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
M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tJ+ii+A | ||
TEX4LxOEgEaV38FV+NqKYsOUSOFtryV9XN8eaCA9AoGATRq2EPUG52+z+S2UM6h4 | ||
xYK21yXkabyBnEbrSri38NPh7be5QKoBkbbolgcOAyw/V4/KOYHGqkd0IC/0Kgnp | ||
rkvqcLbqoA/HrwNfKy0DLWdeV7/VhzziCSPRAW8F2aQAuZjS8lAndM6J20cok1LP | ||
f7qU34l1kn46gU713Lawt7ECgYEAr7Bu+UY15IxxrHAiXMUdms6CDSouOwWwED/7 | ||
vSq1bTBGTm6H6h9yXc/HHbvorASbsW3mfsEhZhOe8jJ6LA8Fjzz0XswSkx5RddU1 | ||
0+OFr7AwXOvRvGhfkEGuWY3vwu+QA7lGnBSwo0h54d+XVWAQkuWpTrhS9/xU+OOV | ||
Cggpsv0CgYBxLAfSj5ZS50dn+J8FaQypPbCMDKb2gYk7RZ1SpTLBeYmWHYbD/oht | ||
8js8hzj2hfj5Eu2LJWxamjS0dbDlizzVWkbVfk/e/hCPnIurXvUaFwVbkcu20psn | ||
V+wc23onQvNUZUM8cZQ+ApS38NiaX66HvgOVtaOCcCiv2PF7EuOxrA== | ||
-----END RSA PRIVATE KEY----- |
16 changes: 16 additions & 0 deletions
16
dev-packages/node-integration-tests/suites/reporting-api/localhost.pem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIICljCCAX4CCQChkPJBaaS+ojANBgkqhkiG9w0BAQsFADANMQswCQYDVQQGEwJ1 | ||
czAeFw0yNDEwMjExMjI4MTBaFw0yNTEwMjExMjI4MTBaMA0xCzAJBgNVBAYTAnVz | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuyGGuR1WxHZI6NzLKBto | ||
n8MQKwrOgruuH3OX3xgFc6U1ZCecB59n7zgnNkSHBAv+x+9I1pDnwSFGKtuqSOU1 | ||
sHC4usRkKNTXN+mU172AdWW7N8mznp8yFe08OELmRPXgJmo1q/5LXfO/I6MNuKAV | ||
Hm8MKWn1e9uxlkcPMW1nOXvuPwk8BcPvXh7w5CSjG7z2wY+D9YkH3KWyAt1oJaET | ||
S7iSfdQy+pfZy/Nq6//n/+Nu7yuQNIWR73+ejWtQAzi58RHYjlBEOwhtBdxgApJX | ||
5OpQMiy+ncFNpAU2YQa/RLRtarNymrWqAStr3gBeeL8R61nX57hFoUM6S65cKnKI | ||
RQIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQBnCF6XsEDFWIS4Uknvhj6Ris8iWI4F | ||
JmXVQ3EMBkk7nk9od1OMXcYdSmrJs5eQLiCl+xZOIeA1Gvtrl+NEjWLbKwcgtkTW | ||
0aIkOMxutAVUK+KKqjzkxJ4gvcdKpCtRH+6YllLpYwisdSOk6bby3g0TAfViY2wN | ||
J4iqtjLRq5UWsIPuFPB8y3l4nhavtAHV6uOQNvl6mXEUj9QDE1BsNC8Fs24Eu/jv | ||
DB7da1V8whpm1KDyp1dc7+ioXYTISZ0r2IcK/XRjzfiH1s7Xvf4juYLVYhDSpwdJ | ||
wQgwssHT71WhtvAtn16bike+ZHZbqkiENZRcStDMJiyeHEiSpT/7Kibl | ||
-----END CERTIFICATE----- |
58 changes: 58 additions & 0 deletions
58
dev-packages/node-integration-tests/suites/reporting-api/server.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as https from 'https'; | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import { defaultStackParser as browserStackParser } from '@sentry/browser'; | ||
import { handleReportingApi } from '@sentry/core'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
const __dirname = new URL('.', import.meta.url).pathname; | ||
|
||
Sentry.init({ | ||
debug: true, | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
transport: loggingTransport, | ||
}); | ||
|
||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import express from 'express'; | ||
|
||
const app = express(); | ||
|
||
app.use(express.json({ type: 'application/reports+json' })); | ||
|
||
app.post('/reporting-api', async (req, res) => { | ||
await handleReportingApi(req.body, browserStackParser); | ||
res.sendStatus(200); | ||
}); | ||
|
||
const port = 9000; | ||
|
||
app.get('/', (req, res) => { | ||
const file = readFileSync(join(__dirname, 'index.html'), { encoding: 'utf-8' }); | ||
|
||
res.setHeader('Content-Type', 'text/html'); | ||
res.setHeader( | ||
'Reporting-Endpoints', | ||
`csp-endpoint="https://localhost:${port}/reporting-api", default="https://localhost:${port}/reporting-api"`, | ||
); | ||
res.setHeader('Content-Security-Policy', "default-src 'self'; report-to csp-endpoint"); | ||
res.setHeader( | ||
'Origin-Trial', | ||
'ApD+E2izWNtaaRBeZ5BXu46aV0l1MSUzJTPERkU3yf+53pAOHj3rARpjb08itVJklPYx7iNEv5//s2dtXUFIvgMAAABzeyJvcmlnaW4iOiJodHRwczovL2xvY2FsaG9zdDo5MDAwIiwiZmVhdHVyZSI6IkRvY3VtZW50UG9saWN5SW5jbHVkZUpTQ2FsbFN0YWNrc0luQ3Jhc2hSZXBvcnRzIiwiZXhwaXJ5IjoxNzQyMzQyMzk5fQ==', | ||
); | ||
res.setHeader('Document-Policy', 'include-js-call-stacks-in-crash-reports'); | ||
res.send(file); | ||
}); | ||
|
||
const options = { | ||
key: readFileSync(join(__dirname, 'localhost-key.pem')), | ||
cert: readFileSync(join(__dirname, 'localhost.pem')), | ||
}; | ||
|
||
const server = https.createServer(options, app); | ||
|
||
server.listen(port, () => { | ||
// eslint-disable-next-line no-console | ||
console.log(`{"port":${port}}`); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { Event, Report, StackParser, Stacktrace } from '@sentry/types'; | ||
import { uuid4 } from '@sentry/utils'; | ||
import { getClient } from './currentScopes'; | ||
import { createRawSecurityEnvelope } from './envelope'; | ||
|
||
/** Handles Requests from the Reporting API */ | ||
export async function handlerReportingApiRequest( | ||
request: Request, | ||
browserStackParser?: StackParser, | ||
client = getClient(), | ||
): Promise<Response> { | ||
if (request.method !== 'POST') { | ||
return new Response('Expected POST', { status: 405 }); | ||
} | ||
|
||
if (request.headers.get('Content-Type') !== 'application/reports+json') { | ||
return new Response('Expected "application/reports+json" Content-Type', { status: 415 }); | ||
} | ||
const reports = await request.json(); | ||
await handleReportingApi(reports, browserStackParser, client); | ||
return new Response(undefined, { status: 200 }); | ||
} | ||
|
||
/** Handles Reports from the Reporting API */ | ||
export async function handleReportingApi( | ||
reports: Report[], | ||
browserStackParser?: StackParser, | ||
client = getClient(), | ||
): Promise<void> { | ||
if (!client) { | ||
// eslint-disable-next-line no-console | ||
console.warn('[Reporting API] No client available'); | ||
return; | ||
} | ||
|
||
const dsn = client.getDsn(); | ||
if (!dsn) { | ||
// eslint-disable-next-line no-console | ||
console.warn('[Reporting API] No DSN set'); | ||
return; | ||
} | ||
|
||
for (const report of reports) { | ||
if (report.type === 'crash') { | ||
const event: Event = { | ||
level: 'fatal', | ||
message: 'Crashed', | ||
request: { | ||
url: report.url, | ||
...(report.user_agent && { headers: { 'User-Agent': report.user_agent } }), | ||
}, | ||
}; | ||
|
||
if (report.body.reason === 'oom') { | ||
event.message = 'Crashed: Out of memory'; | ||
} else if (report.body.reason === 'unresponsive') { | ||
event.message = 'Crashed: Unresponsive'; | ||
} | ||
|
||
if (report.body.stack) { | ||
event.exception = { | ||
values: [ | ||
{ | ||
type: 'Crashed', | ||
value: event.message, | ||
stacktrace: { | ||
...(browserStackParser && report.body.stack && { frames: browserStackParser(report.body.stack) }), | ||
}, | ||
}, | ||
], | ||
}; | ||
delete event.message; | ||
} | ||
|
||
client.captureEvent(event); | ||
} else if (report.type === 'csp-violation') { | ||
const options = client.getOptions(); | ||
const envelope = createRawSecurityEnvelope(report, dsn, options.tunnel, options.release, options.environment); | ||
|
||
await client.sendEnvelope(envelope); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.