generated from ExpTechTW/Example
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
fix: verify
Showing
3 changed files
with
119 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const crypto = require('crypto'); | ||
|
||
class PluginVerifier { | ||
constructor(officialKey) { | ||
this.officialKey = officialKey; | ||
this.publicKeys = new Map(); | ||
} | ||
|
||
loadKeysFromDirectory(keysDir) { | ||
try { | ||
if (!fs.existsSync(keysDir)) { | ||
fs.mkdirSync(keysDir, { recursive: true }); | ||
return; | ||
} | ||
|
||
const files = fs.readdirSync(keysDir); | ||
files.forEach((file) => { | ||
if (file.endsWith('.pem')) { | ||
try { | ||
const keyPath = path.join(keysDir, file); | ||
const keyContent = fs.readFileSync(keyPath, 'utf8'); | ||
const keyId = path.basename(file, '.pem'); | ||
this.publicKeys.set(keyId, keyContent); | ||
} | ||
catch (error) { | ||
console.error(`Failed to load key ${file}:`, error); | ||
} | ||
} | ||
}); | ||
} | ||
catch (error) { | ||
console.error('Failed to load public keys:', error); | ||
} | ||
} | ||
|
||
verify(pluginPath) { | ||
try { | ||
const signaturePath = path.join(pluginPath, 'signature.json'); | ||
if (!fs.existsSync(signaturePath)) { | ||
return { valid: false, error: 'Missing signature.json' }; | ||
} | ||
|
||
const signatureData = JSON.parse(fs.readFileSync(signaturePath)); | ||
const { fileHashes, signature, keyId } = signatureData; | ||
|
||
if (!fileHashes || !signature) { | ||
return { valid: false, error: 'Invalid signature data format' }; | ||
} | ||
|
||
for (const [file, expectedHash] of Object.entries(fileHashes)) { | ||
if (file === 'signature.json') { | ||
continue; | ||
} | ||
|
||
const filePath = path.join(pluginPath, file); | ||
if (!fs.existsSync(filePath)) { | ||
return { valid: false, error: `Missing file: ${file}` }; | ||
} | ||
|
||
const content = fs.readFileSync(filePath); | ||
const actualHash = crypto.createHash('sha256').update(content).digest('hex'); | ||
|
||
if (actualHash !== expectedHash) { | ||
return { valid: false, error: `Modified file: ${file}` }; | ||
} | ||
} | ||
|
||
let publicKey; | ||
if (!keyId) { | ||
publicKey = this.officialKey; | ||
} | ||
else { | ||
publicKey = this.publicKeys.get(keyId); | ||
if (!publicKey) { | ||
return { valid: false, error: `Unknown key ID: ${keyId}` }; | ||
} | ||
} | ||
|
||
const verify = crypto.createVerify('SHA256'); | ||
verify.write(JSON.stringify(fileHashes)); | ||
verify.end(); | ||
|
||
const isValid = verify.verify(publicKey, signature, 'base64'); | ||
|
||
return { | ||
valid: isValid, | ||
error: isValid ? null : 'Invalid signature', | ||
keyId: keyId || 'official', | ||
}; | ||
} | ||
catch (error) { | ||
return { valid: false, error: error.message }; | ||
} | ||
} | ||
} | ||
|
||
module.exports = PluginVerifier; |
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