Skip to content

Dev/sftp-basic#41

Draft
jubeormk1 wants to merge 29 commits intomkj:mainfrom
jubeormk1:dev/sftp-basic
Draft

Dev/sftp-basic#41
jubeormk1 wants to merge 29 commits intomkj:mainfrom
jubeormk1:dev/sftp-basic

Conversation

@jubeormk1
Copy link
Copy Markdown
Contributor

@jubeormk1 jubeormk1 commented Mar 5, 2026

Intro

Please consider this simplified and improved version of the pull request dev/sftp-start #29. It privides basic sftp server functionality responding to request in order to:

Overview of the package

The Sftp requests and responses are implemented in proto.rs.

Uses SSHEncode and SSHDecode together with SftpSink (SshSink) and SftpSource (SshSource) to handle the packets, even when they are received fragmented.

The point of entry of the package is SftpHandler, which will deserialise request and pass them to an structure implementing the SftpServer trait. This allows the library user to program an implementation suitable for its use case. See the provided demo/sftp/std example for more details.

Readability improvements:

The original pull request followed every local commit made, including a lot of trial and error, and would add too many details for main.

For that reason I have decided to make the next improvements:

  • Groups different parts of the code in commits so it is easier to follow the changes and avoids all the intermediate steps
  • improved testing scripts

Back to Draft stage

I decided to step back and make this PR a draft again because I want to cover several things that came up, such as changes to main and some action items from SSH-Stamp security audit performed by Radically Open Security. These are the elements that I will be working on:

  • 2026/04/16 04:40 UTC: Rebase to main upstream branch
  • Review changes on sshwire-derive/src/lib.rs
  • Remove default features
  • Fix "Peak" typo in sftpsource.rs
  • Chase redundant lifetimes, specially in proto.rs
  • proto.rs encode return WireError::Bug instead of WireError::PacketWrong
  • sftphandler.rs select cancellation policy
  • Use AtomicUsize for sftpoutputchannelhandler.rs CounterMutex type instead of Mutex
  • Rebase and split commit 1f38033 to keep src/packet.rs edits in its own commit.
  • Sftp/Std example: base root should be non traversable. Use strict-path
  • Sftp/Std example: Note explaining that tiny_hash is not secure or replace it by a secure hash as the hash space can be easily explored
  • sftpserver.rs ReadReply send_header+send_data:
    • Counter to u64
    • Implement a write guard for ReadReply to guide the intended use send_header then send_data

changes

First commit summarizing the implementation in the draft pull request contained on the dev/sftp-start branch.
I am trying to keep the commit number low and focused on different parts of the implementation.

- sshwire-derive/src/lib.rs: Modified enconde_enum to allow encoding of enums discriminants

- Added the full proto and sftpsource definitions and Cargo.toml for the sftp crate.

- sftp/src/lib.rs: Will experience many changes as the functionality is implemented, but for now it just re-exports the proto and sftpsource modules.
- lib.rs: Now it contains the main library code for the sunset-sftp crate, including module declarations and public exports. Updated documentation to reflect the current state of the library and its features including issue mkj#40.

Main additions include:

- sftphandler module: Implementation of the main entrypoint for the SFTP server, which will handle incoming SFTP requests and manage the server's state.
- sftpserver.rs: Contains the trait definition for the SFTP server that is to be implemented by the user of the library, defining the required methods for handling SFTP operations.
- sftperror.rs: Defines error types and handling for the SFTP server operations.

Additional files:

- sftpsink.rs: An implementation of SSHSink with extra functionality for handling SFTP packets
- opaquefilehandle.rs: Collection of traits that a filehandle is expected to implement.

About SftpHandler:

Main entry point for the SFTP server. It requires to take ownership of an async_channel.rs::ChanInOut in order to write long responses to the client.
This makes it not exactly sans-io and not completely observable, but this compromise facilitates the implementation of the SftpServer trait thanks to an internal embassy pipe (See sftpoutputchannelhandler.rs).
… challenges and other changes to sunset

From 'matt/sftptesting' into dev/sftp-start commits:

- 947cd6e
- 2869501

And @jubeormk1

- a44f70c
TODO: Improve the tests. Running them is a bit hairy. You need to run the demo server from the root of the repo, and then run the test scripts from the testing folder.

This is an implementation of the sunset-sftp basic functionality.

It also has a testing folder with scripts to test different operations
- Can be run from the repo base folder. Other pwd will fail
- All of them will return a value coherent with the test result so they can be used for CI
- Improved, but complicating dir listing and stat listing test: require expect and use expect script for the test
@jubeormk1
Copy link
Copy Markdown
Contributor Author

Apologies, I will fix the CI

Should have run testing/ci.sh beforehand
Copy link
Copy Markdown
Owner

@mkj mkj left a comment

Choose a reason for hiding this comment

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

Thanks for your efforts and getting that tidied up.
I will have to add more doc comments to the rest of Sunset to keep up!

I've added a few first-pass review comments looking over it, fairly superficial. I'll have a closer look at the details later.

Comment thread sshwire-derive/src/lib.rs
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Don't think this file should be included, it reverts some more recent upstream changes

Comment thread sftp/Cargo.toml Outdated
edition = "2024"

[features]
default = []
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Don't need default feature if it's empty

Comment thread sftp/src/sftpsource.rs Outdated
debug!("New source with content: : {:?}", buffer);
SftpSource { buffer: buffer, index: 0 }
}
/// Peaks the buffer for packet type [`SftpNum`]. This does not advance
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

"Peek" rather than "Peak"

Comment thread sftp/src/proto.rs Outdated
Comment on lines +736 to +737
impl<'a: 'de, 'de> SSHDecode<'de> for SftpPacket<'a>
where 'de: 'a // This implies that both lifetimes are equal
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The where 'de: 'a doesn't seem right, as you comment it implies just a single lifetime 'a or 'de could be used there.

I'll try some experiments simplifying it.

Comment thread sftp/src/proto.rs Outdated
Comment thread sftp/src/opaquefilehandle.rs Outdated
{
/// Creates a new instance using a given string slice as `seed` which
/// content should not clearly related to the seed
fn new(seed: &str) -> Self;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'm not sure that OpaqueFileHandle should have a constructor from a string, it seems brittle unless using >128 bit identifiers and cryptographic hashes.

To me it seems that it would be better for OpaqueFileHandleManager to have private knowledge of the OpaqueFileHandle, then it could construct it directly with a plain counter or something like that, and avoid duplicates by checking its own handle_map etc. Wouldn't need to worry about salts either.

(Can leave it for the time being though)

Comment thread sftp/src/sftpserver.rs Outdated
}

/// Provides the stats of the given file path
fn stats(
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Maybe call it attrs()? Or stat() to match the spec/posix syscall.

Comment thread testing/ci.sh Outdated
Comment thread testing/ci.sh Outdated
Comment thread src/packets.rs
Comment on lines 844 to -885
@@ -882,7 +882,7 @@ pub struct ParseContext {

// Set to true if an unknown variant is encountered.
// Packet length checks should be omitted in that case.
pub(crate) seen_unknown: bool,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Maybe the changes in packets.rs should be their own commit, since they're good to keep.

The other changes in that commit are bodge workarounds that might get reverted later, wouldn't want to lose the packet.rs changes.

Thanks for the review, you have risen some good points. I am going to continue addressing your review, for now these are my changes:

- removed default = [] as it is unnecessary
- warn->debug for From request_packet_type for SftpPacket
- requestholder.rs::RequestHolder.valid_request() : explicit None on Err() try_get_ref()
- SftpServer.rs::SftpServe.stats()->attrs() and uses replaced
- sftpsource.rs::SftpSource.peak_packet_type()->peek_packet_type()
- ci.sh undo revert from [342a515](mkj@342a515) and now builds `demo/sftp/std` without release or bloat
These should have been added to the previous commit but I did not track the changes:

- ci.sh: Changing back teh target folder
- sftpserver.rs: Renaming stats to attrs trait and changing all related implementations and calls
- sftpsource.rs: Fixing typo peak-> peek in file and related calls
- [x] All tests in testing passing
- [ ] Deleting forgotten 512B_random file from the testing output directory
Addressing needed changes in proto.rs. I looked at the code generated by the macro before reverting lib.rs (cargo-expand expand) and applied equivalent code.
As pointed out by @mkj, the new(& str) method in `OpaqueFileHandle` is brittle. I added it mindlessly by the DemoFileHandleManager implementation. Now I replaced the `new(&str)`by adding the condition to whoever decides to use `FileHandleManager` trait to implement `InitWithSeed` + `OpaqueFileHandle` for the key.
@jubeormk1 jubeormk1 marked this pull request as draft April 8, 2026 21:22
mkj added 10 commits April 13, 2026 00:21
The read refcount wasn't incremented on ChanIn or ChanInOut clone.
This could result in input being discarded if the read refcount hit
zero. It isn't clear whether this could result in missed EOF (which has
been reported).
This is required by time 0.3.47
These were reported by cargo audit

keccak
bytes
time
instant
Aren't needed now embassy doesn't need rust nightly
@jubeormk1
Copy link
Copy Markdown
Contributor Author

jubeormk1 commented Apr 16, 2026

Rebasing from mkj/sunset main branch happened from annotation for commit a5e42fa.

The tests in demo/sftp/std/testing/ passed as usual excluding the get_file_long.sh that catches the known issue #40.

This is used to catch bad usages of encode_request. @mkj Please let me know if you want this reverted
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