Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions cmd/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd
Comment thread
thesp0nge marked this conversation as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @Prashansa-K for visibility - I figured this shouldn't be under deck file lint because the new command is intended for a specific purpose, and can't be configured by users.

import (
"github.com/spf13/cobra"
)

func newPluginCmd() *cobra.Command {
pluginCmd := &cobra.Command{
Use: "plugin",
Short: "Lint Kong plugins",
Long: `The plugin command set allows you to lint custom Kong plugin Lua code locally.`,
}

pluginCmd.AddCommand(newPluginLintCmd())

return pluginCmd
}
97 changes: 97 additions & 0 deletions cmd/plugin_lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cmd

import (
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/kong/deck/plugin/lua"
"github.com/spf13/cobra"
)

var (
pluginLintCode string
pluginLintEdition string
pluginLintSandbox string
)

// Executes plugin lint command.
func executePluginLint(_ *cobra.Command, _ []string) error {
var luaCode string
var err error

if pluginLintCode == "-" {
luaCode, err = readFromStdin()
Comment thread
thesp0nge marked this conversation as resolved.
} else {
content, readErr := os.ReadFile(pluginLintCode)
if readErr != nil {
return fmt.Errorf("failed to read file %s: %w", pluginLintCode, readErr)
}
luaCode = string(content)
}

if err != nil {
return fmt.Errorf("failed to read input: %w", err)
}

if strings.TrimSpace(luaCode) == "" {
return errors.New("no Lua code provided. Use --code or pipe code to stdin")
}

v, err := lua.NewValidator(pluginLintEdition, "")
if err != nil {
return err
}

violations, err := v.Validate(luaCode, pluginLintSandbox)
if err != nil {
return err
}

if len(violations) == 0 {
fmt.Println("Success: No violations found. Your Lua code is safe for the specified sandbox.")
return nil
}

fmt.Printf("Found %d violations:\n", len(violations))
for _, vio := range violations {
lineInfo := ""
if vio.Line > 0 {
lineInfo = fmt.Sprintf(" (line %d)", vio.Line)
}
fmt.Printf(" - [%s] %s: %s%s\n", vio.Severity, vio.ID, vio.Message, lineInfo)
}

return errors.New("lua validation failed")
}

func readFromStdin() (string, error) {
fmt.Fprintf(os.Stderr, "Reading input from stdin...\n")
data, err := io.ReadAll(os.Stdin)
if err != nil {
return "", fmt.Errorf("error reading from stdin: %w", err)
}

return string(data), err
}

func newPluginLintCmd() *cobra.Command {
pluginLintCmd := &cobra.Command{
Use: "lint [flags]",
Short: "Check custom LUA code for security and sandbox compatibility",
RunE: func(cmd *cobra.Command, args []string) error {
return executePluginLint(cmd, args)
},
}

pluginLintCmd.Flags().StringVarP(&pluginLintCode, "code", "c", "-",
"custom LUA code to validate. Use - to read from stdin.")
pluginLintCmd.Flags().StringVarP(&pluginLintEdition, "edition", "e", "ee",
"Kong Edition [choices: ee (enterprise), oss (open source)]")
pluginLintCmd.Flags().StringVarP(&pluginLintSandbox, "sandbox", "s", "strict",
"Kong sandbox profile [choices: lua, standard, strict, lax]")

return pluginLintCmd
}
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ It can be used to export, import, or sync entities to Kong.`,
rootCmd.AddCommand(newDiffCmd(true)) // deprecated, to exist under the `gateway` subcommand only
rootCmd.AddCommand(newConvertCmd(true)) // deprecated, to exist under the `file` subcommand only
rootCmd.AddCommand(newKonnectCmd()) // deprecated, to be removed

rootCmd.AddCommand(newPluginCmd())

{
gatewayCmd := newGatewaySubCmd()
rootCmd.AddCommand(gatewayCmd)
Expand Down Expand Up @@ -259,6 +262,7 @@ It can be used to export, import, or sync entities to Kong.`,
fileCmd.AddCommand(newKong2TfCmd())
fileCmd.AddCommand(newFileFormatCmd())
}

Comment thread
thesp0nge marked this conversation as resolved.
return rootCmd
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/spf13/pflag v1.0.10
github.com/spf13/viper v1.21.0
github.com/stretchr/testify v1.11.1
github.com/yuin/gopher-lua v1.1.2
golang.org/x/sync v0.21.0
k8s.io/api v0.35.4
k8s.io/apiextensions-apiserver v0.33.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ github.com/yuin/goldmark v1.8.2 h1:kEGpgqJXdgbkhcOgBxkC0X0PmoPG1ZyoZ117rDVp4zE=
github.com/yuin/goldmark v1.8.2/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc h1:+IAOyRda+RLrxa1WC7umKOZRsGq4QrFFMYApOeHzQwQ=
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
github.com/yuin/gopher-lua v1.1.2 h1:yF/FjE3hD65tBbt0VXLE13HWS9h34fdzJmrWRXwobGA=
github.com/yuin/gopher-lua v1.1.2/go.mod h1:7aRmXIWl37SqRf0koeyylBEzJ+aPt8A+mmkQ4f1ntR8=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
Expand Down
109 changes: 109 additions & 0 deletions plugin/lua/policies/kong_ee_3x.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Kong Enterprise Lua Sandbox Knowledge Base
# Accurate mapping from kong/tools/sandbox/environment/

version: "1.0"
edition: "EE"

profiles:
- name: lua
description: "Base Lua environment. Standard libraries only."
allowed_globals:
- _VERSION
- assert
- error
- ipairs
- next
- pairs
- pcall
- print
- select
- tonumber
- tostring
- type
- unpack
- xpcall
allowed_prefixes:
- bit.
- coroutine.
- io.type
- jit.
- math.
- os.clock
- os.date
- os.difftime
- os.time
- string.
- table.

- name: strict
extends: lua
description: "Standard Kong Sandbox. Safe PDK and Nginx utilities."
allowed_globals:
- kong
- ngx
allowed_prefixes:
- kong.client.
- kong.cluster.
- kong.default_workspace
- kong.ip.
- kong.jwe.
- kong.nginx.
- kong.node.
- kong.plugin.
- kong.request.
- kong.response.
- kong.service.request.
- kong.service.response.
- kong.table.
- kong.telemetry.
- kong.tracing.
- kong.version
- ngx.log
- ngx.sleep
- ngx.time
- ngx.re.
- ngx.req.
- ngx.resp.
- ngx.worker.
- ngx.config.
# Common constants
- ngx.HTTP_
- ngx.OK
- ngx.ERR
- ngx.INFO

- name: lax
extends: strict
description: "Permissive Sandbox. Advanced PDK capabilities."
allowed_prefixes:
- kong.cache.
- kong.db.consumers.select # Permesso solo select su tabelle specifiche
- kong.db.services.select
- kong.db.routes.select
- kong.db.upstreams.select
- kong.db.targets.select
- kong.db.plugins.select
- kong.dns.
- kong.vault.
- ngx.socket.
- ngx.thread.


rules:
- id: LUA-EV-001
name: "Global Environment Access"
pattern: "(_G\\[|getfenv|setfenv)"
severity: CRITICAL
message: "Potential sandbox evasion: global environment manipulation."

- id: LUA-SEC-001
name: "Forbidden Module Loading"
pattern: "require\\s*\\(['\"] (ffi|os|io) ['\"]\\)"
severity: CRITICAL
message: "Forbidden module loading."

- id: LUA-COMP-001
name: "Restricted OS/IO calls"
pattern: "(os\\.execute|os\\.getenv|io\\.open|io\\.popen)"
severity: ERROR
message: "Direct OS or IO system calls are strictly forbidden for security reasons."
60 changes: 60 additions & 0 deletions plugin/lua/policies/kong_oss_3x.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Kong OSS Lua Sandbox Knowledge Base
# Based on standard Kong OSS sandbox restrictions

version: "1.0"
edition: "OSS"

profiles:
- name: lua
description: "Base Lua environment for OSS"
allowed_globals:
- _VERSION
- assert
- error
- ipairs
- next
- pairs
- pcall
- print
- select
- tonumber
- tostring
- type
- unpack
- xpcall
allowed_prefixes:
- math.
- string.
- table.

- name: standard
extends: lua
description: "Standard OSS Sandbox. Core PDK only."
allowed_globals:
- kong
- ngx
allowed_prefixes:
- kong.log.
- kong.request.
- kong.response.
- kong.service.
- kong.ip.
- kong.table.
- ngx.log
- ngx.sleep
- ngx.time
- ngx.re.
- ngx.exit
- ngx.HTTP_
- ngx.OK

rules:
- id: LUA-EV-001
pattern: "(_G\\[|getfenv|setfenv)"
severity: CRITICAL
message: "Sandbox evasion: global environment access is strictly blocked."

- id: LUA-COMP-001
pattern: "(os\\.|io\\.)"
severity: ERROR
message: "System libraries (os, io) are completely unavailable in OSS sandbox."
Loading
Loading