1
0
Fork 0

Ability to enable unsafe in yaegi through plugin manifest

This commit is contained in:
Ryan Melendez 2025-04-25 05:26:04 -04:00 committed by GitHub
parent a092c4f535
commit 8f37c8f0c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 45 additions and 10 deletions

View file

@ -2,6 +2,7 @@ package plugins
import (
"context"
"errors"
"fmt"
"net/http"
"os"
@ -15,6 +16,7 @@ import (
"github.com/traefik/traefik/v3/pkg/logs"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
"github.com/traefik/yaegi/stdlib/unsafe"
)
type yaegiMiddlewareBuilder struct {
@ -119,7 +121,7 @@ func (m *YaegiMiddleware) NewHandler(ctx context.Context, next http.Handler) (ht
return m.builder.newHandler(ctx, next, m.config, m.middlewareName)
}
func newInterpreter(ctx context.Context, goPath string, manifestImport string) (*interp.Interpreter, error) {
func newInterpreter(ctx context.Context, goPath string, manifest *Manifest, settings Settings) (*interp.Interpreter, error) {
i := interp.New(interp.Options{
GoPath: goPath,
Env: os.Environ(),
@ -132,14 +134,25 @@ func newInterpreter(ctx context.Context, goPath string, manifestImport string) (
return nil, fmt.Errorf("failed to load symbols: %w", err)
}
if manifest.UseUnsafe && !settings.UseUnsafe {
return nil, errors.New("this plugin uses unsafe import. If you want to use it, you need to allow useUnsafe in the settings")
}
if settings.UseUnsafe && manifest.UseUnsafe {
err := i.Use(unsafe.Symbols)
if err != nil {
return nil, fmt.Errorf("failed to load unsafe symbols: %w", err)
}
}
err = i.Use(ppSymbols())
if err != nil {
return nil, fmt.Errorf("failed to load provider symbols: %w", err)
}
_, err = i.Eval(fmt.Sprintf(`import "%s"`, manifestImport))
_, err = i.Eval(fmt.Sprintf(`import "%s"`, manifest.Import))
if err != nil {
return nil, fmt.Errorf("failed to import plugin code %q: %w", manifestImport, err)
return nil, fmt.Errorf("failed to import plugin code %q: %w", manifest.Import, err)
}
return i, nil