Skip to content

Add OpenFile option to Config#294

Merged
benhoyt merged 12 commits into
benhoyt:masterfrom
vyskocilm:vyskocilm/master
Jun 30, 2026
Merged

Add OpenFile option to Config#294
benhoyt merged 12 commits into
benhoyt:masterfrom
vyskocilm:vyskocilm/master

Conversation

@benhoyt

@benhoyt benhoyt commented Jun 30, 2026

Copy link
Copy Markdown
Owner

Copying from @vyskocilm's original PR #291:

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.

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 goawk matches 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/v3 allows one to define a proper handlers for os.OpenFile, os.Stat or os.Readdir. See

https://codeberg.org/gonix/mcp/src/commit/e5cff85075e6191e6415fa66435a4c73470cc978/internal/tools/shell_execute/tool.go#L159-L163

I kept the OpenFileFunc compatible with os.OpenFile and os package, however an another possibility to consider is a form with a context, mainly because a sh module uses it to pass various stuff like a working directory there.

// OpenFileFunc specifies a function used to open files instead of the default
// [os.OpenFile]. ctx is a context passed via [Interp.ExecContext], or [context.Background()]
// if [Interp.Exec] has been called.
OpenFileFunc func(ctx context.Context, name string, flag int, perm os.FileMode) (*os.File, error)

However it is up to your judgement.

BTW: an another task for an air-gaped goawk is 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 :-).

vyskocilm and others added 11 commits June 25, 2026 20:34
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
@benhoyt
benhoyt merged commit 4c907fb into benhoyt:master Jun 30, 2026
9 checks passed
@benhoyt
benhoyt deleted the vyskocilm/master branch June 30, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants