Add spans for AP signature checkers
This commit is contained in:
parent
ab08ab4e28
commit
2a95b88477
|
@ -1,5 +1,5 @@
|
||||||
import { SequelizeInstrumentation } from 'opentelemetry-instrumentation-sequelize'
|
import { SequelizeInstrumentation } from 'opentelemetry-instrumentation-sequelize'
|
||||||
import { diag, DiagLogLevel, trace } from '@opentelemetry/api'
|
import { context, diag, DiagLogLevel, trace } from '@opentelemetry/api'
|
||||||
import { JaegerExporter } from '@opentelemetry/exporter-jaeger'
|
import { JaegerExporter } from '@opentelemetry/exporter-jaeger'
|
||||||
import { registerInstrumentations } from '@opentelemetry/instrumentation'
|
import { registerInstrumentations } from '@opentelemetry/instrumentation'
|
||||||
import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns'
|
import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns'
|
||||||
|
@ -15,6 +15,8 @@ import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
|
||||||
import { logger } from '@server/helpers/logger'
|
import { logger } from '@server/helpers/logger'
|
||||||
import { CONFIG } from '@server/initializers/config'
|
import { CONFIG } from '@server/initializers/config'
|
||||||
|
|
||||||
|
const tracer = trace.getTracer('peertube')
|
||||||
|
|
||||||
function registerOpentelemetryTracing () {
|
function registerOpentelemetryTracing () {
|
||||||
if (CONFIG.OPEN_TELEMETRY.TRACING.ENABLED !== true) return
|
if (CONFIG.OPEN_TELEMETRY.TRACING.ENABLED !== true) return
|
||||||
|
|
||||||
|
@ -75,9 +77,18 @@ function registerOpentelemetryTracing () {
|
||||||
tracerProvider.register()
|
tracerProvider.register()
|
||||||
}
|
}
|
||||||
|
|
||||||
const tracer = trace.getTracer('peertube')
|
async function wrapWithSpanAndContext <T> (spanName: string, cb: () => Promise<T>) {
|
||||||
|
const span = tracer.startSpan(spanName)
|
||||||
|
const activeContext = trace.setSpan(context.active(), span)
|
||||||
|
|
||||||
|
const result = await context.with(activeContext, () => cb())
|
||||||
|
span.end()
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
registerOpentelemetryTracing,
|
registerOpentelemetryTracing,
|
||||||
tracer
|
tracer,
|
||||||
|
wrapWithSpanAndContext
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { NextFunction, Request, Response } from 'express'
|
import { NextFunction, Request, Response } from 'express'
|
||||||
import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor'
|
import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor'
|
||||||
import { getAPId } from '@server/lib/activitypub/activity'
|
import { getAPId } from '@server/lib/activitypub/activity'
|
||||||
|
import { wrapWithSpanAndContext } from '@server/lib/opentelemetry/tracing'
|
||||||
import { ActivityDelete, ActivityPubSignature, HttpStatusCode } from '@shared/models'
|
import { ActivityDelete, ActivityPubSignature, HttpStatusCode } from '@shared/models'
|
||||||
import { logger } from '../helpers/logger'
|
import { logger } from '../helpers/logger'
|
||||||
import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto'
|
import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto'
|
||||||
|
@ -61,91 +62,95 @@ export {
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
async function checkHttpSignature (req: Request, res: Response) {
|
async function checkHttpSignature (req: Request, res: Response) {
|
||||||
// FIXME: compatibility with http-signature < v1.3
|
return wrapWithSpanAndContext('peertube.activitypub.checkHTTPSignature', async () => {
|
||||||
const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string
|
// FIXME: compatibility with http-signature < v1.3
|
||||||
if (sig && sig.startsWith('Signature ') === true) req.headers[HTTP_SIGNATURE.HEADER_NAME] = sig.replace(/^Signature /, '')
|
const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string
|
||||||
|
if (sig && sig.startsWith('Signature ') === true) req.headers[HTTP_SIGNATURE.HEADER_NAME] = sig.replace(/^Signature /, '')
|
||||||
|
|
||||||
let parsed: any
|
let parsed: any
|
||||||
|
|
||||||
try {
|
try {
|
||||||
parsed = parseHTTPSignature(req, HTTP_SIGNATURE.CLOCK_SKEW_SECONDS)
|
parsed = parseHTTPSignature(req, HTTP_SIGNATURE.CLOCK_SKEW_SECONDS)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn('Invalid signature because of exception in signature parser', { reqBody: req.body, err })
|
logger.warn('Invalid signature because of exception in signature parser', { reqBody: req.body, err })
|
||||||
|
|
||||||
res.fail({
|
res.fail({
|
||||||
status: HttpStatusCode.FORBIDDEN_403,
|
status: HttpStatusCode.FORBIDDEN_403,
|
||||||
message: err.message
|
message: err.message
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const keyId = parsed.keyId
|
const keyId = parsed.keyId
|
||||||
if (!keyId) {
|
if (!keyId) {
|
||||||
res.fail({
|
res.fail({
|
||||||
status: HttpStatusCode.FORBIDDEN_403,
|
status: HttpStatusCode.FORBIDDEN_403,
|
||||||
message: 'Invalid key ID',
|
message: 'Invalid key ID',
|
||||||
data: {
|
data: {
|
||||||
keyId
|
keyId
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug('Checking HTTP signature of actor %s...', keyId)
|
logger.debug('Checking HTTP signature of actor %s...', keyId)
|
||||||
|
|
||||||
let [ actorUrl ] = keyId.split('#')
|
let [ actorUrl ] = keyId.split('#')
|
||||||
if (actorUrl.startsWith('acct:')) {
|
if (actorUrl.startsWith('acct:')) {
|
||||||
actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, ''))
|
actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, ''))
|
||||||
}
|
}
|
||||||
|
|
||||||
const actor = await getOrCreateAPActor(actorUrl)
|
const actor = await getOrCreateAPActor(actorUrl)
|
||||||
|
|
||||||
const verified = isHTTPSignatureVerified(parsed, actor)
|
const verified = isHTTPSignatureVerified(parsed, actor)
|
||||||
if (verified !== true) {
|
if (verified !== true) {
|
||||||
logger.warn('Signature from %s is invalid', actorUrl, { parsed })
|
logger.warn('Signature from %s is invalid', actorUrl, { parsed })
|
||||||
|
|
||||||
res.fail({
|
res.fail({
|
||||||
status: HttpStatusCode.FORBIDDEN_403,
|
status: HttpStatusCode.FORBIDDEN_403,
|
||||||
message: 'Invalid signature',
|
message: 'Invalid signature',
|
||||||
data: {
|
data: {
|
||||||
actorUrl
|
actorUrl
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
res.locals.signature = { actor }
|
res.locals.signature = { actor }
|
||||||
return true
|
return true
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkJsonLDSignature (req: Request, res: Response) {
|
async function checkJsonLDSignature (req: Request, res: Response) {
|
||||||
const signatureObject: ActivityPubSignature = req.body.signature
|
return wrapWithSpanAndContext('peertube.activitypub.JSONLDSignature', async () => {
|
||||||
|
const signatureObject: ActivityPubSignature = req.body.signature
|
||||||
|
|
||||||
if (!signatureObject || !signatureObject.creator) {
|
if (!signatureObject || !signatureObject.creator) {
|
||||||
res.fail({
|
res.fail({
|
||||||
status: HttpStatusCode.FORBIDDEN_403,
|
status: HttpStatusCode.FORBIDDEN_403,
|
||||||
message: 'Object and creator signature do not match'
|
message: 'Object and creator signature do not match'
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const [ creator ] = signatureObject.creator.split('#')
|
const [ creator ] = signatureObject.creator.split('#')
|
||||||
|
|
||||||
logger.debug('Checking JsonLD signature of actor %s...', creator)
|
logger.debug('Checking JsonLD signature of actor %s...', creator)
|
||||||
|
|
||||||
const actor = await getOrCreateAPActor(creator)
|
const actor = await getOrCreateAPActor(creator)
|
||||||
const verified = await isJsonLDSignatureVerified(actor, req.body)
|
const verified = await isJsonLDSignatureVerified(actor, req.body)
|
||||||
|
|
||||||
if (verified !== true) {
|
if (verified !== true) {
|
||||||
logger.warn('Signature not verified.', req.body)
|
logger.warn('Signature not verified.', req.body)
|
||||||
|
|
||||||
res.fail({
|
res.fail({
|
||||||
status: HttpStatusCode.FORBIDDEN_403,
|
status: HttpStatusCode.FORBIDDEN_403,
|
||||||
message: 'Signature could not be verified'
|
message: 'Signature could not be verified'
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
res.locals.signature = { actor }
|
res.locals.signature = { actor }
|
||||||
return true
|
return true
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user