-
Notifications
You must be signed in to change notification settings - Fork 29
chore: sign the vsix package, not the manifest #83
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -7,35 +7,53 @@ import ( | |
"io" | ||
"io/fs" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/spf13/afero/mem" | ||
"golang.org/x/xerrors" | ||
|
||
"cdr.dev/slog" | ||
|
||
"github.com/coder/code-marketplace/extensionsign" | ||
) | ||
|
||
var _ Storage = (*Signature)(nil) | ||
|
||
const ( | ||
SigzipFilename = "extension.sigzip" | ||
sigManifestName = ".signature.manifest" | ||
SigzipFileExtension = ".signature.p7s" | ||
sigManifestName = ".signature.manifest" | ||
) | ||
|
||
func SignatureZipFilename(manifest *VSIXManifest) string { | ||
return ExtensionVSIXNameFromManifest(manifest) + SigzipFileExtension | ||
} | ||
|
||
// Signature is a storage wrapper that can sign extensions on demand. | ||
type Signature struct { | ||
// Signer if provided, will be used to sign extensions. If not provided, | ||
// no extensions will be signed. | ||
Signer crypto.Signer | ||
Logger slog.Logger | ||
// SaveSigZips is a flag that will save the signed extension to disk. | ||
// This is useful for debugging, but the server will never use this file. | ||
saveSigZips bool | ||
Storage | ||
} | ||
|
||
func NewSignatureStorage(signer crypto.Signer, s Storage) *Signature { | ||
func NewSignatureStorage(logger slog.Logger, signer crypto.Signer, s Storage) *Signature { | ||
return &Signature{ | ||
Signer: signer, | ||
Storage: s, | ||
} | ||
} | ||
|
||
func (s *Signature) SaveSigZips() { | ||
if !s.saveSigZips { | ||
s.Logger.Info(context.Background(), "extension signatures will be saved to disk, do not use this in production") | ||
} | ||
s.saveSigZips = true | ||
} | ||
|
||
func (s *Signature) SigningEnabled() bool { | ||
return s.Signer != nil | ||
} | ||
|
@@ -49,14 +67,26 @@ func (s *Signature) AddExtension(ctx context.Context, manifest *VSIXManifest, vs | |
return "", xerrors.Errorf("generate signature manifest: %w", err) | ||
} | ||
|
||
data, err := json.Marshal(sigManifest) | ||
sigManifestJSON, err := json.Marshal(sigManifest) | ||
if err != nil { | ||
return "", xerrors.Errorf("encode signature manifest: %w", err) | ||
} | ||
|
||
if s.SigningEnabled() && s.saveSigZips { | ||
signed, err := s.SigZip(ctx, vsix, sigManifestJSON) | ||
if err != nil { | ||
s.Logger.Error(ctx, "signing manifest", slog.Error(err)) | ||
return "", xerrors.Errorf("sign and zip manifest: %w", err) | ||
} | ||
extra = append(extra, File{ | ||
RelativePath: SignatureZipFilename(manifest), | ||
Content: signed, | ||
}) | ||
} | ||
|
||
return s.Storage.AddExtension(ctx, manifest, vsix, append(extra, File{ | ||
RelativePath: sigManifestName, | ||
Content: data, | ||
Content: sigManifestJSON, | ||
})...) | ||
} | ||
|
||
|
@@ -68,14 +98,14 @@ func (s *Signature) Manifest(ctx context.Context, publisher, name string, versio | |
|
||
if s.SigningEnabled() { | ||
for _, asset := range manifest.Assets.Asset { | ||
if asset.Path == SigzipFilename { | ||
if asset.Path == SignatureZipFilename(manifest) { | ||
// Already signed | ||
return manifest, nil | ||
} | ||
} | ||
manifest.Assets.Asset = append(manifest.Assets.Asset, VSIXAsset{ | ||
Type: VSIXSignatureType, | ||
Path: SigzipFilename, | ||
Path: SignatureZipFilename(manifest), | ||
Addressable: "true", | ||
}) | ||
return manifest, nil | ||
|
@@ -84,7 +114,7 @@ func (s *Signature) Manifest(ctx context.Context, publisher, name string, versio | |
} | ||
|
||
// Open will intercept requests for signed extensions payload. | ||
// It does this by looking for 'SigzipFilename' or p7s.sig. | ||
// It does this by looking for 'SigzipFileExtension' or p7s.sig. | ||
// | ||
// The signed payload and signing process is taken from: | ||
// https://github.com/filiptronicek/node-ovsx-sign | ||
|
@@ -105,7 +135,10 @@ func (s *Signature) Manifest(ctx context.Context, publisher, name string, versio | |
// source implementation. Ideally this marketplace would match Microsoft's | ||
// marketplace API. | ||
func (s *Signature) Open(ctx context.Context, fp string) (fs.File, error) { | ||
if s.SigningEnabled() && filepath.Base(fp) == SigzipFilename { | ||
if s.SigningEnabled() && strings.HasSuffix(filepath.Base(fp), SigzipFileExtension) { | ||
base := filepath.Base(fp) | ||
vsixPath := strings.TrimSuffix(base, SigzipFileExtension) | ||
|
||
// hijack this request, sign the sig manifest | ||
manifest, err := s.Storage.Open(ctx, filepath.Join(filepath.Dir(fp), sigManifestName)) | ||
if err != nil { | ||
|
@@ -121,15 +154,39 @@ func (s *Signature) Open(ctx context.Context, fp string) (fs.File, error) { | |
return nil, xerrors.Errorf("read signature manifest: %w", err) | ||
} | ||
|
||
signed, err := extensionsign.SignAndZipManifest(s.Signer, manifestData) | ||
vsix, err := s.Storage.Open(ctx, filepath.Join(filepath.Dir(fp), vsixPath+".vsix")) | ||
if err != nil { | ||
// If this file is missing, it means the extension was added before | ||
// signatures were handled by the marketplace. | ||
// TODO: Generate the sig manifest payload and insert it? | ||
return nil, xerrors.Errorf("open signature manifest: %w", err) | ||
} | ||
defer vsix.Close() | ||
|
||
vsixData, err := io.ReadAll(vsix) | ||
if err != nil { | ||
return nil, xerrors.Errorf("read signature manifest: %w", err) | ||
} | ||
|
||
// TODO: Fetch the VSIX payload from the storage | ||
signed, err := s.SigZip(ctx, vsixData, manifestData) | ||
if err != nil { | ||
return nil, xerrors.Errorf("sign and zip manifest: %w", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops meant to say before submitting, really big nit, but looks like this error would end up saying |
||
} | ||
|
||
f := mem.NewFileHandle(mem.CreateFile(SigzipFilename)) | ||
f := mem.NewFileHandle(mem.CreateFile(fp)) | ||
_, err = f.Write(signed) | ||
return f, err | ||
} | ||
|
||
return s.Storage.Open(ctx, fp) | ||
} | ||
|
||
func (s *Signature) SigZip(ctx context.Context, vsix []byte, sigManifest []byte) ([]byte, error) { | ||
signed, err := extensionsign.SignAndZipManifest(s.Signer, vsix, sigManifest) | ||
if err != nil { | ||
s.Logger.Error(ctx, "signing manifest", slog.Error(err)) | ||
return nil, xerrors.Errorf("sign and zip manifest: %w", err) | ||
} | ||
return signed, nil | ||
} |
This file contains hidden or 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 hidden or 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.
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.
Looks like this opens the
.vsix
, which should always exist (if not installing will also fail). Or I could be misreading this 😅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.
you are right!
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.
Wait no, the manifest is expected to be there. Now that we are signing the vsix though, I might change this later to not require the file to be there.
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.
This comment refers to loading the vsix though, not the manifest, and the vsix must always exist. Possibly it was supposed to go in the block above where we read the manifest?
Uh oh!
There was an error while loading. Please reload this page.
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.
Oh yeah probably, the todo and error below also says manifest instead of vsix :D