|
| 1 | +package shcheck |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + |
| 8 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 9 | + "github.com/rs/zerolog" |
| 10 | + "github.com/tb0hdan/wass-mcp/pkg/server" |
| 11 | + "github.com/tb0hdan/wass-mcp/pkg/tools" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + binaryName = "shcheck.py" |
| 16 | + description = "shcheck is a security headers checker that analyzes HTTP response headers for security best practices." |
| 17 | + headerVerb = "output" |
| 18 | +) |
| 19 | + |
| 20 | +// Tool implements the shcheck security headers scanner. |
| 21 | +type Tool struct { |
| 22 | + tools.BaseScanner |
| 23 | +} |
| 24 | + |
| 25 | +// Scan performs the shcheck scan and returns the output. |
| 26 | +func (t *Tool) Scan(ctx context.Context, params tools.ScanParams) tools.ScanResult { |
| 27 | + host, port := t.ResolveHostPort(params.Host, params.Port) |
| 28 | + |
| 29 | + targetURL := tools.BuildTargetURL(host, port) |
| 30 | + t.Logger.Info().Msgf("Running shcheck scan on %s", targetURL) |
| 31 | + |
| 32 | + args := []string{"-j", "-d", targetURL} |
| 33 | + if params.Vhost != "" { |
| 34 | + args = append(args, "-a", fmt.Sprintf("Host: %s", params.Vhost)) |
| 35 | + } |
| 36 | + |
| 37 | + cmd := exec.CommandContext(ctx, binaryName, args...) //nolint:gosec |
| 38 | + output, err := cmd.CombinedOutput() |
| 39 | + |
| 40 | + if err != nil { |
| 41 | + return tools.ScanResult{ |
| 42 | + Output: string(output), |
| 43 | + Error: fmt.Errorf("failed to execute shcheck: %w", err), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return tools.ScanResult{ |
| 48 | + Output: string(output), |
| 49 | + Error: nil, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Register registers the shcheck tool with the MCP server. |
| 54 | +func (t *Tool) Register(srv *server.Server) error { |
| 55 | + return t.RegisterTool(srv, t.Handler) |
| 56 | +} |
| 57 | + |
| 58 | +// Handler handles MCP tool requests. |
| 59 | +func (t *Tool) Handler(ctx context.Context, _ *mcp.CallToolRequest, input tools.ScannerInput) (*mcp.CallToolResult, any, error) { |
| 60 | + if err := t.ValidateInput(input); err != nil { |
| 61 | + return nil, nil, err |
| 62 | + } |
| 63 | + |
| 64 | + host, port := t.ResolveHostPort(input.Host, input.Port) |
| 65 | + |
| 66 | + params := tools.ScanParams{ |
| 67 | + Host: host, |
| 68 | + Port: port, |
| 69 | + Vhost: input.Vhost, |
| 70 | + } |
| 71 | + |
| 72 | + scanResult := t.Scan(ctx, params) |
| 73 | + if scanResult.Error != nil { |
| 74 | + return nil, nil, fmt.Errorf("%w\nOutput: %s", scanResult.Error, scanResult.Output) |
| 75 | + } |
| 76 | + |
| 77 | + targetURL := tools.BuildTargetURL(host, port) |
| 78 | + resultText := tools.FormatScannerOutput(binaryName, headerVerb, targetURL, scanResult.Output, input.MaxLines, input.Offset) |
| 79 | + |
| 80 | + return &mcp.CallToolResult{ |
| 81 | + Content: []mcp.Content{ |
| 82 | + &mcp.TextContent{Text: resultText}, |
| 83 | + }, |
| 84 | + }, nil, nil |
| 85 | +} |
| 86 | + |
| 87 | +// New creates a new shcheck scanner tool. |
| 88 | +func New(logger zerolog.Logger) tools.Scanner { |
| 89 | + return &Tool{ |
| 90 | + BaseScanner: tools.NewBaseScanner(binaryName, description, logger), |
| 91 | + } |
| 92 | +} |
0 commit comments