-
Notifications
You must be signed in to change notification settings - Fork 581
feat: add FileIO extension for file transfer abstraction #314
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,31 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package fileio | ||
|
|
||
| import "sync" | ||
|
|
||
| var ( | ||
| mu sync.Mutex | ||
| provider Provider | ||
| ) | ||
|
|
||
| // Register registers a FileIO Provider. | ||
| // Later registrations override earlier ones (last-write-wins). | ||
| // Unlike credential.Register which appends to a chain (multiple credential | ||
| // sources are tried in order), FileIO uses a single active provider because | ||
| // only one file I/O backend is active at a time (local vs server mode). | ||
| // Typically called from init() via blank import. | ||
| func Register(p Provider) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| provider = p | ||
| } | ||
|
|
||
| // GetProvider returns the currently registered Provider. | ||
| // Returns nil if no provider has been registered. | ||
| func GetProvider() Provider { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| return provider | ||
| } |
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,71 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package fileio | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| ) | ||
|
|
||
| // Provider creates FileIO instances. | ||
| // Follows the same API style as extension/credential.Provider. | ||
| type Provider interface { | ||
| Name() string | ||
| ResolveFileIO(ctx context.Context) FileIO | ||
| } | ||
|
|
||
| // FileIO abstracts file transfer operations for CLI commands. | ||
| // The default implementation operates on the local filesystem with | ||
| // path validation, directory creation, and atomic writes. | ||
| // Inject a custom implementation via Factory.FileIOProvider to replace | ||
| // file transfer behavior (e.g. streaming in server mode). | ||
| type FileIO interface { | ||
| // Open opens a file for reading (upload, attachment, template scenarios). | ||
| // The default implementation validates the path via SafeInputPath. | ||
| Open(name string) (File, error) | ||
|
|
||
| // Stat returns file metadata (size validation, existence checks). | ||
| // The default implementation validates the path via SafeInputPath. | ||
| // Use os.IsNotExist(err) to distinguish "file not found" from "invalid path". | ||
| Stat(name string) (FileInfo, error) | ||
|
|
||
| // ResolvePath returns the validated, absolute path for the given output path. | ||
| // The default implementation delegates to SafeOutputPath. | ||
| // Use this to obtain the canonical saved path for user-facing output. | ||
| ResolvePath(path string) (string, error) | ||
|
|
||
| // Save writes content to the target path and returns a SaveResult. | ||
| // The default implementation validates via SafeOutputPath, creates | ||
| // parent directories, and writes atomically. | ||
| Save(path string, opts SaveOptions, body io.Reader) (SaveResult, error) | ||
| } | ||
|
|
||
| // FileInfo is a minimal subset of os.FileInfo covering actual CLI usage. | ||
| // os.FileInfo satisfies this interface. | ||
| type FileInfo interface { | ||
| Size() int64 | ||
| IsDir() bool | ||
| } | ||
|
|
||
| // File is the interface returned by FileIO.Open. | ||
| // It covers the subset of *os.File methods actually used by CLI commands. | ||
| // *os.File satisfies this interface without adaptation. | ||
| type File interface { | ||
| io.Reader | ||
| io.ReaderAt | ||
| io.Closer | ||
| } | ||
|
|
||
| // SaveResult holds the outcome of a Save operation. | ||
| type SaveResult interface { | ||
| Size() int64 // actual bytes written | ||
| } | ||
|
|
||
| // SaveOptions carries metadata for Save. | ||
| // The default (local) implementation ignores these fields; | ||
| // server-mode implementations use them to construct streaming response frames. | ||
| type SaveOptions struct { | ||
| ContentType string // MIME type | ||
| ContentLength int64 // content length; -1 if unknown | ||
| } |
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,45 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| // Package charcheck provides character-level security checks shared across | ||
| // path validation (localfileio) and input validation (validate) packages. | ||
| // Keeping these checks in one place ensures consistent detection of dangerous | ||
| // Unicode and control characters throughout the codebase. | ||
| package charcheck | ||
|
|
||
| import "fmt" | ||
|
|
||
| // RejectControlChars rejects C0 control characters (except \t and \n) and | ||
| // dangerous Unicode characters (Bidi overrides, zero-width, line/paragraph | ||
| // separators) that enable visual spoofing attacks. | ||
| func RejectControlChars(value, flagName string) error { | ||
| for _, r := range value { | ||
| if r != '\t' && r != '\n' && (r < 0x20 || r == 0x7f) { | ||
| return fmt.Errorf("%s contains invalid control characters", flagName) | ||
| } | ||
| if IsDangerousUnicode(r) { | ||
| return fmt.Errorf("%s contains dangerous Unicode characters", flagName) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // IsDangerousUnicode identifies Unicode code points used for visual spoofing | ||
| // attacks. These characters are invisible or alter text direction, allowing | ||
| // attackers to make "report.exe" display as "report.txt" (Bidi override) or | ||
| // insert hidden content (zero-width characters). | ||
| func IsDangerousUnicode(r rune) bool { | ||
| switch { | ||
| case r >= 0x200B && r <= 0x200D: // zero-width space/non-joiner/joiner | ||
| return true | ||
| case r == 0xFEFF: // BOM / ZWNBSP | ||
| return true | ||
| case r >= 0x202A && r <= 0x202E: // Bidi: LRE/RLE/PDF/LRO/RLO | ||
| return true | ||
| case r >= 0x2028 && r <= 0x2029: // line/paragraph separator | ||
| return true | ||
| case r >= 0x2066 && r <= 0x2069: // Bidi isolates: LRI/RLI/FSI/PDI | ||
| return true | ||
| } | ||
| return false | ||
| } |
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
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
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.