-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use code signature for build info
- Loading branch information
Showing
18 changed files
with
316 additions
and
282 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
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
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
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
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,76 @@ | ||
import type { FastifyPluginAsync } from 'fastify' | ||
import fp from 'fastify-plugin' | ||
import fs from 'node:fs/promises' | ||
import path from 'node:path' | ||
import { | ||
sceauSchema, | ||
SceauVerificationSuccess, | ||
SCEAU_FILE_NAME, | ||
verify, | ||
} from 'sceau' | ||
import { fileURLToPath } from 'url' | ||
import { env } from '../env.js' | ||
import type { App } from '../types' | ||
|
||
type Decoration = Omit<SceauVerificationSuccess, 'outcome'> | ||
|
||
declare module 'fastify' { | ||
interface FastifyInstance { | ||
codeSignature: Decoration | ||
} | ||
} | ||
|
||
const codeSignaturePlugin: FastifyPluginAsync = async (app: App) => { | ||
if (env.NODE_ENV !== 'production') { | ||
const decoration: Decoration = { | ||
timestamp: 'unknown', | ||
buildURL: 'local', | ||
sourceURL: 'local', | ||
} | ||
app.decorate('codeSignature', decoration) | ||
return | ||
} | ||
const rootDir = path.resolve( | ||
path.dirname(fileURLToPath(import.meta.url)), | ||
'../..' | ||
) | ||
const sceauFilePath = path.resolve(rootDir, SCEAU_FILE_NAME) | ||
const sceauFileContents = await fs | ||
.readFile(sceauFilePath, { encoding: 'utf8' }) | ||
.catch(error => { | ||
app.log.fatal({ msg: 'Failed to read code signature file', error }) | ||
process.exit(1) | ||
}) | ||
const sceau = sceauSchema.parse(JSON.parse(sceauFileContents)) | ||
const result = await verify( | ||
app.sodium, | ||
sceau, | ||
rootDir, | ||
app.sodium.from_hex(sceau.publicKey) | ||
) | ||
if (result.outcome === 'failure') { | ||
app.log.fatal({ | ||
msg: 'Invalid code signature', | ||
manifestErrors: result.manifestErrors, | ||
signatureVerified: result.signatureVerified, | ||
}) | ||
process.exit(0) | ||
} | ||
app.log.info({ | ||
msg: 'Code signature verified', | ||
signedOn: result.timestamp, | ||
sources: result.sourceURL, | ||
build: result.buildURL, | ||
}) | ||
const { outcome: _, ...decoration } = result | ||
app.decorate('codeSignature', decoration) | ||
} | ||
|
||
export default fp(codeSignaturePlugin, { | ||
fastify: '4.x', | ||
name: 'codeSignature', | ||
dependencies: ['sodium'], | ||
decorators: { | ||
fastify: ['sodium'], | ||
}, | ||
}) |
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
Oops, something went wrong.