audit and harden workspace

This commit is contained in:
2026-08-10 11:42:45 +08:00
parent c883cc52a2
commit 1a1fa521c0
140 changed files with 640 additions and 475 deletions

View File

@@ -3,9 +3,11 @@ package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
@@ -18,12 +20,24 @@ import (
var ServiceKey = "default"
var workspacePattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$`)
func normalizeWorkspace(value string) (string, error) {
value = strings.ToLower(strings.TrimSpace(value))
if !workspacePattern.MatchString(value) {
return "", fmt.Errorf("workspace must match %s", workspacePattern.String())
}
return value, nil
}
func main() {
workspace := flag.String("workspace", ServiceKey, "workspace used to select etc/{workspace}_{runtime}.yaml")
flag.Parse()
if value := strings.TrimSpace(*workspace); value != "" {
ServiceKey = value
value, err := normalizeWorkspace(*workspace)
if err != nil {
panic(err)
}
ServiceKey = value
config.New(ServiceKey)
impl.NewImpl()

26
all/cmd/main/main_test.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import "testing"
func TestNormalizeWorkspace(t *testing.T) {
tests := []struct {
input string
want string
ok bool
}{
{input: "default", want: "default", ok: true},
{input: " Tenant_01 ", want: "tenant_01", ok: true},
{input: "../prod", ok: false},
{input: "", ok: false},
{input: "tenant.name", ok: false},
}
for _, test := range tests {
got, err := normalizeWorkspace(test.input)
if (err == nil) != test.ok {
t.Fatalf("normalizeWorkspace(%q) error = %v", test.input, err)
}
if got != test.want {
t.Fatalf("normalizeWorkspace(%q) = %q, want %q", test.input, got, test.want)
}
}
}

View File

@@ -156,5 +156,3 @@ replace (
bsm/full/module/ec/order => ../module/ec/order
bsm/full/module/finance/wallet => ../module/finance/wallet
)
replace git.apinb.com/bsm-sdk/core => D:/work/bsm-sdk/core

View File

@@ -7,6 +7,7 @@ import (
"net"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
@@ -49,7 +50,13 @@ func (s *Server) Start(addr string) error {
}
s.HTTP.ServeHTTP(w, r)
}), &http2.Server{})
s.server = &http.Server{Addr: addr, Handler: handler}
s.server = &http.Server{
Addr: addr,
Handler: handler,
ReadHeaderTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
MaxHeaderBytes: 1 << 20,
}
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
@@ -59,9 +66,18 @@ func (s *Server) Start(addr string) error {
}
func (s *Server) Stop(ctx context.Context) error {
s.GRPC.GracefulStop()
stopped := make(chan struct{})
go func() {
s.GRPC.GracefulStop()
close(stopped)
}()
select {
case <-stopped:
case <-ctx.Done():
s.GRPC.Stop()
}
if s.server == nil {
return nil
return ctx.Err()
}
return s.server.Shutdown(ctx)
}