Skip to content

feat: reject binary files in AddFile#107

Merged
k1LoW merged 13 commits intomainfrom
text-only
Mar 11, 2026
Merged

feat: reject binary files in AddFile#107
k1LoW merged 13 commits intomainfrom
text-only

Conversation

@k1LoW
Copy link
Owner

@k1LoW k1LoW commented Mar 11, 2026

Summary

  • Add text-only file check to AddFile using Git's NUL byte detection heuristic (first 8KB)
  • Binary files are rejected with ErrBinaryFile; non-regular files (FIFO, device, socket) are also rejected
  • Non-existent files are allowed through in internal flows (glob watch, backup restore) but HTTP API endpoints (POST /_/api/files, POST /_/api/files/open) still validate file existence via os.Stat before calling AddFile
  • All entry points (CLI, HTTP API, glob patterns, backup restore) go through the same AddFile check
  • Server startup fails with an error when all files (including restored session files) are skipped and no patterns or uploaded files exist

k1LoW added 3 commits March 11, 2026 10:34
Add a text-only check using the same heuristic as Git (NUL byte
detection in the first 8KB). Binary files are rejected with
ErrBinaryFile. Non-existent files are still allowed through.
- Use io.EOF instead of string comparison for EOF check
- Use bytes.IndexByte instead of bytes.ContainsRune for NUL detection
- Check for duplicate files before binary check to avoid unnecessary I/O
- Use errors.Is(err, io.EOF) instead of direct comparison
- Check AddFile error return values in tests
@github-actions

This comment has been minimized.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds binary-file detection to the Go server’s AddFile path so non-text files can be rejected early (using Git’s “NUL byte in first 8KB” heuristic), and updates callers/tests to handle the new error-returning API.

Changes:

  • Introduce ErrBinaryFile, an isBinaryFile helper, and change State.AddFile to return (*FileEntry, error).
  • Update CLI/server entry points (glob expansion, HTTP endpoints, startup) to handle AddFile errors and skip/log when appropriate.
  • Update server tests to use temp files and add coverage for binary rejection + non-existent path behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
internal/server/server.go Adds binary detection + error-returning AddFile; updates pattern/glob + HTTP handlers to handle errors.
cmd/root.go Updates server startup file loading to handle AddFile errors (skip + warn).
internal/server/server_test.go Updates existing backup tests to use temp files; adds a test for binary rejection and non-existent file behavior.

You can also share your feedback on Copilot code review. Take the survey.

Reorder checks so non-EOF errors (e.g. EISDIR for directories) are
returned even when n==0, instead of silently passing the binary check.
@github-actions

This comment has been minimized.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.


You can also share your feedback on Copilot code review. Take the survey.

- Fix import sort order to pass gofmt
- Error when all CLI-specified files are skipped
- Add handler-level tests for POST /_/api/files with binary/text files
@github-actions

This comment has been minimized.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


You can also share your feedback on Copilot code review. Take the survey.

@github-actions

This comment has been minimized.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


You can also share your feedback on Copilot code review. Take the survey.

@github-actions

This comment has been minimized.

@k1LoW k1LoW self-assigned this Mar 11, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


You can also share your feedback on Copilot code review. Take the survey.

Stat the path before opening to ensure it is a regular file. This
prevents blocking reads on FIFOs, device files, and sockets.
@k1LoW k1LoW requested a review from Copilot March 11, 2026 06:39
@github-actions

This comment has been minimized.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


You can also share your feedback on Copilot code review. Take the survey.

@github-actions

This comment has been minimized.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


You can also share your feedback on Copilot code review. Take the survey.

@github-actions

This comment has been minimized.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


You can also share your feedback on Copilot code review. Take the survey.

@github-actions

This comment has been minimized.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


You can also share your feedback on Copilot code review. Take the survey.

@github-actions

This comment has been minimized.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


You can also share your feedback on Copilot code review. Take the survey.

@github-actions
Copy link
Contributor

Code Metrics Report

main (b91e1b2) #107 (71cfe54) +/-
Coverage 51.5% 52.1% +0.6%
Code to Test Ratio 1:0.5 1:0.5 -0.1
Test Execution Time 37s 36s -1s
Details
  |                     | main (b91e1b2) | #107 (71cfe54) |  +/-  |
  |---------------------|----------------|----------------|-------|
+ | Coverage            |          51.5% |          52.1% | +0.6% |
  |   Files             |             36 |             36 |     0 |
  |   Lines             |           2819 |           2886 |   +67 |
+ |   Covered           |           1454 |           1506 |   +52 |
- | Code to Test Ratio  |          1:0.5 |          1:0.5 |  -0.1 |
  |   Code              |           4363 |           4427 |   +64 |
+ |   Test              |           2589 |           2590 |    +1 |
+ | Test Execution Time |            37s |            36s |   -1s |

Code coverage of files in pull request scope (50.2% → 51.2%)

Files Coverage +/- Status
cmd/root.go 22.9% -0.3% modified
internal/server/server.go 72.4% +1.0% modified

Reported by octocov

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


You can also share your feedback on Copilot code review. Take the survey.

@k1LoW k1LoW merged commit bb29e6c into main Mar 11, 2026
7 checks passed
@k1LoW k1LoW deleted the text-only branch March 11, 2026 08:11
@github-actions github-actions bot mentioned this pull request Mar 11, 2026
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