Skip to content
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

SEC-421: Binary Handler Impersonation Check #195

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions src/network/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ export class NetworkClass extends EventEmitter {
next()
}

handleError(error: any, req: any, res: any, route: string) {
/* prettier-ignore */ if (logFlags.error) this.mainLogger.error(`Error in route ${route}: ${error.message}`)

nestedCountersInstance.countEvent('endpoint-exception', `${route}`)

// Send an error response
res.status(500).json({
error: 'Internal Server Error',
message: isDebugMode() ? error.message : 'An unexpected error occurred',
route: route,
})
}

// TODO: Allow for binding to a specified network interface
_setupExternal() {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -545,29 +558,29 @@ export class NetworkClass extends EventEmitter {
handlers.push(authHandler)
}

if (isDebugMode() && ['GET', 'POST'].includes(method)) {
const wrappedHandler = async (req, res, next) => {
profilerInstance.profileSectionStart('net-externl', false)
profilerInstance.profileSectionStart(`net-externl-${route}`, false)
profilerInstance.scopedProfileSectionStart(`net-externl-${route}`, false)

let result
try {
result = await responseHandler(req, res, next)
} finally {
const wrappedHandler = async (req, res, next) => {
let result
try {
if (isDebugMode() && ['GET', 'POST'].includes(method)) {
profilerInstance.profileSectionStart('net-externl', false)
profilerInstance.profileSectionStart(`net-externl-${route}`, false)
profilerInstance.scopedProfileSectionStart(`net-externl-${route}`, false)
}
result = await responseHandler(req, res, next)
} catch (error) {
this.handleError(error, req, res, route)
} finally {
if (isDebugMode() && ['GET', 'POST'].includes(method)) {
profilerInstance.scopedProfileSectionEnd(`net-externl-${route}`)
profilerInstance.profileSectionEnd(`net-externl-${route}`, false)
profilerInstance.profileSectionEnd('net-externl', false)
}

return result
}

handlers.push(wrappedHandler)
} else {
handlers.push(responseHandler)
return result
}

handlers.push(wrappedHandler)

let expressMethod = {
GET: 'get',
POST: 'post',
Expand Down
7 changes: 6 additions & 1 deletion src/p2p/Comms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,12 @@ export function registerInternalBinary(route: string, handler: InternalBinaryHan
warn('registerInternalBinary: internal routes can only be used by nodes in the network...')
return
}

if (
NodeList.nodes.get(header.sender_id).publicKey !== sign.owner
) {
warn('registerInternalBinary: Public key is not the same with sign owner')
return
}
// Checks to see if we can extract the actual payload from the wrapped message
const requestPayload = _extractPayloadBinary(wrappedPayload)
if (!requestPayload) {
Expand Down