Skip to content
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

Validate appID before encrypting #1072

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions cmd/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package cmd
import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/spf13/cobra"
"go.starlark.net/starlark"

"tidbyt.dev/pixlet/manifest"
"tidbyt.dev/pixlet/runtime"
"tidbyt.dev/pixlet/tools/repo"
)

const PublicKeysetJSON = `{
Expand Down Expand Up @@ -40,6 +44,10 @@ func encrypt(cmd *cobra.Command, args []string) {
}

appID := args[0]
if err := validateAppID(args[0]); err != nil {
log.Fatalf("Cannot encrypt with appID '%s': %v", appID, err)
}

encrypted := make([]string, len(args)-1)

for i, val := range args[1:] {
Expand All @@ -54,3 +62,40 @@ func encrypt(cmd *cobra.Command, args []string) {
fmt.Println(starlark.String(val).String())
}
}

func validateAppID(appID string) error {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("something went wrong with your local filesystem: %v", err)
}
if !repo.IsInRepo(cwd, "community") {
log.Printf("Skipping validation of appID since command is not being run from inside the community repo.")
return nil // Can only apply check if running from the community repo
}
root, err := repo.RepoRoot(cwd)
if err != nil {
return fmt.Errorf("something went wrong with your community repo: %v", err)
}
entries, err := os.ReadDir(filepath.Join(root, "apps"))
if err != nil {
return fmt.Errorf("something went wrong listing existing apps: %v", err)
}
for _, e := range entries {
if !e.IsDir() {
continue
}
f, err := os.Open(filepath.Join(root, "apps", e.Name(), manifest.ManifestFileName))
if err != nil {
log.Printf("Skipping %s/%s/%s/%s: %v", root, "apps", e.Name(), manifest.ManifestFileName, err)
continue
}
m, err := manifest.LoadManifest(f)
if err != nil {
return fmt.Errorf("something went wrong loading manifest for %s: %v", e.Name(), err)
}
if m.ID == appID {
return nil
}
}
return fmt.Errorf("does not match manifest ID for any app in the community repo")
}
Loading