Add OpenFile option to Config#294
Merged
Merged
Conversation
This allows user to provide a custom file opener instead of a default os.OpenFile, making goawk compatible with a newer standard library features like os.Root protecting against path traversal attacks or simulating a read-only filesystems. Attached test cases for os.Root and a read-only filesystem.
Compare the string without unit spaces as end of line differs between Unix and Windows.
Co-authored-by: Ben Hoyt <benhoyt@gmail.com>
Co-authored-by: Ben Hoyt <benhoyt@gmail.com>
Co-authored-by: Ben Hoyt <benhoyt@gmail.com>
Co-authored-by: Ben Hoyt <benhoyt@gmail.com>
Co-authored-by: Ben Hoyt <benhoyt@gmail.com>
- drop OpenFile from goawk.go - modernize errorExit in goawk.go - rename openFileFunc to openFile and use type OpenFileFunc - move TestOpenFileCustom, which is compatible with go 1.20 outside of a tagged file - modernize getline in vm.go the same way as goawk.go - TestOpenFileCustom now has three subtests with the same openFile which disallow writing, but allow read and tests the getline and non existing file.
- add a test for a path traversal - use os.ReadFile for assertions
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Copying from @vyskocilm's original PR #291:
This allows user to provide a custom file opener instead of a default
os.OpenFile, makinggoawkcompatible with a newer standard library features likeos.Rootprotecting against path traversal attacks or simulating a read-only filesystems.Attached test cases for
os.Rootand a read-only filesystem.TL;DR;
My motivation is my opensource project https://codeberg.org/gonix/, which is an user-space implementation of a unix scripting in Go. Somewhere
goawkmatches perfectly. Thanks for spending your time on it ❤️ The first application of my userspace scripting is a secure, yet shell scriptable, MCP server for agents https://codeberg.org/gonix/mcp.At the moment it is secured by the fact most of available commands runs under Web Assembly and a shell layer
mvdan.cc/sh/v3allows one to define a proper handlers foros.OpenFile,os.Statoros.Readdir. Seehttps://codeberg.org/gonix/mcp/src/commit/e5cff85075e6191e6415fa66435a4c73470cc978/internal/tools/shell_execute/tool.go#L159-L163
I kept the
OpenFileFunccompatible withos.OpenFileandospackage, however an another possibility to consider is a form with a context, mainly because ashmodule uses it to pass various stuff like a working directory there.However it is up to your judgement.
BTW: an another task for an air-gaped
goawkis to replace*exec.Command, but I kept this PR focused on a single topic. Additionally I am not entirely sure how to solve that one :-).