-
Notifications
You must be signed in to change notification settings - Fork 35
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
Added verified_contents.json generator. #1008
Draft
boocmp
wants to merge
3
commits into
master
Choose a base branch
from
verified_contents
base: master
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.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,171 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import fs from 'fs-extra' | ||
import path from 'path' | ||
import crypto from 'crypto' | ||
|
||
const getComponentFiles = (dir) => { | ||
let result = [] | ||
|
||
const files = fs.readdirSync(dir) | ||
|
||
for (const file of files) { | ||
const filePath = path.join(dir, file) | ||
const stat = fs.statSync(filePath) | ||
|
||
if (stat.isDirectory()) { | ||
const subDirFiles = getComponentFiles(filePath) | ||
result = result.concat(subDirFiles) | ||
} else { | ||
result.push(filePath) | ||
} | ||
} | ||
|
||
return result.sort() | ||
} | ||
|
||
const computeBlockHashes = (filePath) => { | ||
const buffer = Buffer.alloc(4096) | ||
|
||
const file = fs.openSync(filePath, 'r') | ||
const hashes = [] | ||
|
||
while (true) { | ||
const bytesRead = fs.readSync(file, buffer, 0, buffer.length) | ||
if (bytesRead <= 0) { | ||
break | ||
} | ||
const hash = crypto.createHash('sha256') | ||
hash.update(buffer.subarray(0, bytesRead)) | ||
hashes.push(hash.digest()) | ||
} | ||
|
||
if (!hashes) { | ||
const hash = crypto.createHash('sha256') | ||
hash.update('') | ||
hashes.push(hash.digest()) | ||
} | ||
|
||
return hashes | ||
} | ||
|
||
const computeRootHash = (file) => { | ||
let blockHashes = computeBlockHashes(file) | ||
if (!blockHashes) { | ||
return '' | ||
} | ||
|
||
const branchFactor = 4096 / 32 | ||
|
||
while (blockHashes.length > 1) { | ||
let i = 0 | ||
const parentNodes = [] | ||
while (i !== blockHashes.length) { | ||
const hash = crypto.createHash('sha256') | ||
for (let j = 0; j < branchFactor && i !== blockHashes.length; j++, i++) { | ||
hash.update(blockHashes[i]) | ||
} | ||
parentNodes.push(hash.digest()) | ||
} | ||
blockHashes = parentNodes | ||
} | ||
return blockHashes[0] | ||
} | ||
|
||
const createPayload = (component, files) => { | ||
const payload = { | ||
content_hashes: [ | ||
{ | ||
block_size: 4096, | ||
digest: 'sha256', | ||
files: [], | ||
format: 'treehash', | ||
hash_block_size: 4096 | ||
} | ||
], | ||
item_id: component.id, | ||
item_version: component.version, | ||
protocol_version: 1 | ||
} | ||
|
||
for (const file of files) { | ||
const rootHash = computeRootHash(file) | ||
payload.content_hashes[0].files.push({ | ||
path: file.replace(component.dir, ''), | ||
root_hash: Buffer.from(rootHash).toString('base64Url') | ||
}) | ||
} | ||
|
||
return payload | ||
} | ||
|
||
const signPayload = (protectedBy, payload, privateKey) => { | ||
const signer = crypto.createSign('RSA-SHA256') | ||
signer.update(protectedBy) | ||
signer.update('.') | ||
signer.update(payload) | ||
|
||
return signer.sign(privateKey, 'base64url') | ||
} | ||
|
||
const ensureTrailingSlash = (path) => { | ||
if (path.charAt(path.length - 1) !== '/') { | ||
path += '/' | ||
} | ||
return path | ||
} | ||
|
||
const createVerifiedContents = (inputDir, id, version, publisherProofKeys) => { | ||
inputDir = ensureTrailingSlash(inputDir) | ||
|
||
const componentFiles = getComponentFiles(inputDir) | ||
|
||
const component = { | ||
dir: inputDir, | ||
id, | ||
version | ||
} | ||
|
||
const payload = createPayload(component, componentFiles) | ||
|
||
const protection = { | ||
alg: 'RS256' | ||
} | ||
|
||
const encodedPayload = Buffer.from(JSON.stringify(payload)).toString( | ||
'base64url' | ||
) | ||
const encodedProtection = Buffer.from(JSON.stringify(protection)).toString( | ||
'base64url' | ||
) | ||
|
||
const result = { | ||
description: 'treehash per file', | ||
signed_content: { | ||
payload: encodedPayload, | ||
signatures: [] | ||
} | ||
} | ||
|
||
for (const proofKey in publisherProofKeys) { | ||
if (!proofKey) { | ||
continue | ||
} | ||
const signature = signPayload(encodedProtection, encodedPayload, proofKey) | ||
result.signatures.push({ | ||
protected: encodedProtection, | ||
header: { | ||
kid: 'webstore' | ||
}, | ||
signature | ||
}) | ||
} | ||
|
||
return result | ||
} | ||
|
||
export default { | ||
createVerifiedContents | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we using the existing crx signing keys for this?
for production, is this going to be called in CI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. I am inclined to generate a new one for this purpose.