-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix weird bug where CPU jumps and stays to 100%
Seems related to lazy import of custom-jsonld-signature So we refactored jsonld function calls a little bit
- Loading branch information
1 parent
f93bc6a
commit b017d4d
Showing
5 changed files
with
108 additions
and
105 deletions.
There are no files selected for viewing
This file contains 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 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 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,100 @@ | ||
import { sha256 } from '@peertube/peertube-node-utils' | ||
import { createSign, createVerify } from 'crypto' | ||
import cloneDeep from 'lodash-es/cloneDeep.js' | ||
import { MActor } from '../types/models/index.js' | ||
import { logger } from './logger.js' | ||
import { assertIsInWorkerThread } from './threads.js' | ||
import { jsonld } from './custom-jsonld-signature.js' | ||
|
||
export function isJsonLDSignatureVerified (fromActor: MActor, signedDocument: any): Promise<boolean> { | ||
if (signedDocument.signature.type === 'RsaSignature2017') { | ||
return isJsonLDRSA2017Verified(fromActor, signedDocument) | ||
} | ||
|
||
logger.warn('Unknown JSON LD signature %s.', signedDocument.signature.type, signedDocument) | ||
|
||
return Promise.resolve(false) | ||
} | ||
|
||
// Backward compatibility with "other" implementations | ||
export async function isJsonLDRSA2017Verified (fromActor: MActor, signedDocument: any) { | ||
const [ documentHash, optionsHash ] = await Promise.all([ | ||
createDocWithoutSignatureHash(signedDocument), | ||
createSignatureHash(signedDocument.signature) | ||
]) | ||
|
||
const toVerify = optionsHash + documentHash | ||
|
||
const verify = createVerify('RSA-SHA256') | ||
verify.update(toVerify, 'utf8') | ||
|
||
return verify.verify(fromActor.publicKey, signedDocument.signature.signatureValue, 'base64') | ||
} | ||
|
||
export async function signJsonLDObject <T> (options: { | ||
byActor: { url: string, privateKey: string } | ||
data: T | ||
disableWorkerThreadAssertion?: boolean | ||
}) { | ||
const { byActor, data, disableWorkerThreadAssertion = false } = options | ||
|
||
if (!disableWorkerThreadAssertion) assertIsInWorkerThread() | ||
|
||
const signature = { | ||
type: 'RsaSignature2017', | ||
creator: byActor.url, | ||
created: new Date().toISOString() | ||
} | ||
|
||
const [ documentHash, optionsHash ] = await Promise.all([ | ||
createDocWithoutSignatureHash(data), | ||
createSignatureHash(signature) | ||
]) | ||
|
||
const toSign = optionsHash + documentHash | ||
|
||
const sign = createSign('RSA-SHA256') | ||
sign.update(toSign, 'utf8') | ||
|
||
const signatureValue = sign.sign(byActor.privateKey, 'base64') | ||
Object.assign(signature, { signatureValue }) | ||
|
||
return Object.assign(data, { signature }) | ||
} | ||
|
||
// --------------------------------------------------------------------------- | ||
// Private | ||
// --------------------------------------------------------------------------- | ||
|
||
async function hashObject (obj: any): Promise<any> { | ||
const res = await (jsonld as any).promises.normalize(obj, { | ||
safe: false, | ||
algorithm: 'URDNA2015', | ||
format: 'application/n-quads' | ||
}) | ||
|
||
return sha256(res) | ||
} | ||
|
||
function createSignatureHash (signature: any) { | ||
const signatureCopy = cloneDeep(signature) | ||
Object.assign(signatureCopy, { | ||
'@context': [ | ||
'https://w3id.org/security/v1', | ||
{ RsaSignature2017: 'https://w3id.org/security#RsaSignature2017' } | ||
] | ||
}) | ||
|
||
delete signatureCopy.type | ||
delete signatureCopy.id | ||
delete signatureCopy.signatureValue | ||
|
||
return hashObject(signatureCopy) | ||
} | ||
|
||
function createDocWithoutSignatureHash (doc: any) { | ||
const docWithoutSignature = cloneDeep(doc) | ||
delete docWithoutSignature.signature | ||
|
||
return hashObject(docWithoutSignature) | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { signJsonLDObject } from '@server/helpers/peertube-crypto.js' | ||
import { signJsonLDObject } from '@server/helpers/peertube-jsonld.js' | ||
|
||
export default signJsonLDObject |
This file contains 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