mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
feat: surface audit risk score in update API and UI
Return auditRiskScore and auditRiskLabel in update results so the dashboard can show security posture after both tracked-repo and regular-skill updates. auditGateTrackedRepo now returns the audit result alongside the blocked indicator.
This commit is contained in:
@@ -22,10 +22,12 @@ type updateRequest struct {
|
||||
}
|
||||
|
||||
type updateResultItem struct {
|
||||
Name string `json:"name"`
|
||||
Action string `json:"action"` // "updated", "up-to-date", "skipped", "error", "blocked"
|
||||
Message string `json:"message,omitempty"`
|
||||
IsRepo bool `json:"isRepo"`
|
||||
Name string `json:"name"`
|
||||
Action string `json:"action"` // "updated", "up-to-date", "skipped", "error", "blocked"
|
||||
Message string `json:"message,omitempty"`
|
||||
IsRepo bool `json:"isRepo"`
|
||||
AuditRiskScore int `json:"auditRiskScore,omitempty"`
|
||||
AuditRiskLabel string `json:"auditRiskLabel,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) handleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -178,23 +180,29 @@ func (s *Server) updateTrackedRepo(name, repoPath string, force, skipAudit bool)
|
||||
}
|
||||
|
||||
// Post-pull audit gate
|
||||
if !skipAudit {
|
||||
if blocked := s.auditGateTrackedRepo(name, repoPath, info.BeforeHash); blocked != nil {
|
||||
return *blocked
|
||||
}
|
||||
}
|
||||
|
||||
return updateResultItem{
|
||||
item := updateResultItem{
|
||||
Name: name,
|
||||
Action: "updated",
|
||||
Message: fmt.Sprintf("%d commits, %d files changed", len(info.Commits), info.Stats.FilesChanged),
|
||||
IsRepo: true,
|
||||
}
|
||||
if !skipAudit {
|
||||
blocked, auditResult := s.auditGateTrackedRepo(name, repoPath, info.BeforeHash)
|
||||
if blocked != nil {
|
||||
return *blocked
|
||||
}
|
||||
if auditResult != nil {
|
||||
item.AuditRiskScore = auditResult.RiskScore
|
||||
item.AuditRiskLabel = auditResult.RiskLabel
|
||||
}
|
||||
}
|
||||
|
||||
return item
|
||||
}
|
||||
|
||||
// auditGateTrackedRepo scans a tracked repo after pull and rolls back if HIGH/CRITICAL.
|
||||
// Returns a blocked result item, or nil if the update should proceed.
|
||||
func (s *Server) auditGateTrackedRepo(name, repoPath, beforeHash string) *updateResultItem {
|
||||
// Returns (blocked item, audit result). blocked is non-nil when the update should be rejected.
|
||||
func (s *Server) auditGateTrackedRepo(name, repoPath, beforeHash string) (*updateResultItem, *audit.Result) {
|
||||
var result *audit.Result
|
||||
var err error
|
||||
if s.IsProjectMode() {
|
||||
@@ -213,7 +221,7 @@ func (s *Server) auditGateTrackedRepo(name, repoPath, beforeHash string) *update
|
||||
Action: "blocked",
|
||||
Message: msg,
|
||||
IsRepo: true,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
if result.HasHigh() {
|
||||
@@ -228,10 +236,10 @@ func (s *Server) auditGateTrackedRepo(name, repoPath, beforeHash string) *update
|
||||
Action: "blocked",
|
||||
Message: msg,
|
||||
IsRepo: true,
|
||||
}
|
||||
}, result
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil, result
|
||||
}
|
||||
|
||||
func (s *Server) updateRegularSkill(name, skillPath string, skipAudit bool) updateResultItem {
|
||||
@@ -249,7 +257,8 @@ func (s *Server) updateRegularSkill(name, skillPath string, skipAudit bool) upda
|
||||
if s.IsProjectMode() {
|
||||
opts.AuditProjectRoot = s.projectRoot
|
||||
}
|
||||
if _, err = install.Install(source, skillPath, opts); err != nil {
|
||||
result, err := install.Install(source, skillPath, opts)
|
||||
if err != nil {
|
||||
return updateResultItem{
|
||||
Name: name,
|
||||
Action: "error",
|
||||
@@ -257,11 +266,16 @@ func (s *Server) updateRegularSkill(name, skillPath string, skipAudit bool) upda
|
||||
}
|
||||
}
|
||||
|
||||
return updateResultItem{
|
||||
item := updateResultItem{
|
||||
Name: name,
|
||||
Action: "updated",
|
||||
Message: "reinstalled from source",
|
||||
}
|
||||
if result != nil && result.AuditRiskLabel != "" {
|
||||
item.AuditRiskScore = result.AuditRiskScore
|
||||
item.AuditRiskLabel = result.AuditRiskLabel
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
func (s *Server) updateAll(force, skipAudit bool) []updateResultItem {
|
||||
|
||||
@@ -63,7 +63,7 @@ func TestAuditGateTrackedRepo_RollbackFailure_ReportsWarning(t *testing.T) {
|
||||
s := &Server{cfg: cfg}
|
||||
|
||||
// Pass an invalid beforeHash so git reset --hard will fail
|
||||
result := s.auditGateTrackedRepo("test-repo", repoDir, "0000000000000000000000000000000000000000")
|
||||
result, _ := s.auditGateTrackedRepo("test-repo", repoDir, "0000000000000000000000000000000000000000")
|
||||
|
||||
if result == nil {
|
||||
t.Fatal("expected blocked result, got nil (audit should detect HIGH finding)")
|
||||
@@ -88,7 +88,7 @@ func TestAuditGateTrackedRepo_ScanError_RollbackFailure_ReportsWarning(t *testin
|
||||
|
||||
// Non-existent path → audit.ScanSkill returns error, git.ResetHard also fails
|
||||
nonExistentPath := filepath.Join(t.TempDir(), "does-not-exist")
|
||||
result := s.auditGateTrackedRepo("test-repo", nonExistentPath, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
|
||||
result, _ := s.auditGateTrackedRepo("test-repo", nonExistentPath, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
|
||||
|
||||
if result == nil {
|
||||
t.Fatal("expected blocked result")
|
||||
@@ -125,8 +125,14 @@ func TestAuditGateTrackedRepo_Clean_ReturnsNil(t *testing.T) {
|
||||
cfg := &config.Config{Source: t.TempDir()}
|
||||
s := &Server{cfg: cfg}
|
||||
|
||||
result := s.auditGateTrackedRepo("clean-repo", repoDir, "doesntmatter")
|
||||
if result != nil {
|
||||
t.Errorf("expected nil for clean repo, got action=%q message=%q", result.Action, result.Message)
|
||||
blocked, auditResult := s.auditGateTrackedRepo("clean-repo", repoDir, "doesntmatter")
|
||||
if blocked != nil {
|
||||
t.Errorf("expected nil for clean repo, got action=%q message=%q", blocked.Action, blocked.Message)
|
||||
}
|
||||
if auditResult == nil {
|
||||
t.Fatal("expected audit result for clean repo")
|
||||
}
|
||||
if auditResult.RiskLabel != "clean" {
|
||||
t.Errorf("expected riskLabel=clean, got %q", auditResult.RiskLabel)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,6 +298,8 @@ export interface UpdateResultItem {
|
||||
action: string; // "updated", "up-to-date", "skipped", "error", "blocked"
|
||||
message?: string;
|
||||
isRepo: boolean;
|
||||
auditRiskScore?: number;
|
||||
auditRiskLabel?: string;
|
||||
}
|
||||
|
||||
export interface AvailableTarget {
|
||||
|
||||
@@ -219,7 +219,10 @@ export default function SkillDetailPage() {
|
||||
const res = await api.update({ name: skillName, skipAudit });
|
||||
const item = res.results[0];
|
||||
if (item?.action === 'updated') {
|
||||
toast(`Updated: ${item.name} — ${item.message}`, 'success');
|
||||
const auditInfo = item.auditRiskLabel
|
||||
? ` · Security: ${item.auditRiskLabel.toUpperCase()}${item.auditRiskScore ? ` (${item.auditRiskScore}/100)` : ''}`
|
||||
: '';
|
||||
toast(`Updated: ${item.name} — ${item.message}${auditInfo}`, 'success');
|
||||
refetch();
|
||||
} else if (item?.action === 'up-to-date') {
|
||||
toast(`${item.name} is already up to date.`, 'info');
|
||||
|
||||
Reference in New Issue
Block a user