-
Notifications
You must be signed in to change notification settings - Fork 136
feat: add Lua sandbox validation for plugins via deck plugin lint #2068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thesp0nge
wants to merge
6
commits into
main
Choose a base branch
from
feat_plugin_security_linter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b3a6b7c
feat: add Lua sandbox validation for plugins via deck plugin lint
thesp0nge 64bd4e2
chore: fix linting violations in plugin lint command
thesp0nge 3a1f7fc
chore: use sigs.k8s.io/yaml and cleanup redundant policies
thesp0nge fed44d5
chore: add stdin hint
thesp0nge 15bc205
fix(cmd): correct flag descriptions in plugin lint command
thesp0nge 38908ff
doc(cmd): refine plugin command short/long descriptions to focus on…
thesp0nge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package cmd | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc: @Prashansa-K for visibility - I figured this shouldn't be under |
||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
|
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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| - 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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| - 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." |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.