Skip to content

Dev/sftp-basic#41

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

Dev/sftp-basic#41
jubeormk1 wants to merge 47 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
  • Use AtomicUsize for sftpoutputchannelhandler.rs CounterMutex type instead of Mutex
  • sftphandler.rs select cancellation policy
  • fix SftpSource ctx todo!() with @mkj contribution
  • 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
  • Rebase and split commit 1f38033 to keep src/packet.rs edits in its own commit.

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).
@jubeormk1
Copy link
Copy Markdown
Contributor Author

Apologies, I will fix the CI

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.

@jubeormk1 jubeormk1 marked this pull request as draft April 8, 2026 21:22
@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.

@jubeormk1
Copy link
Copy Markdown
Contributor Author

Doing a search for lifetimes returns that after @mkj intervention there is no more structures with multiple lifetimes defined.

type Err;

/// Used in the `OpaqueFileHandleManager` to generate a Key (OpaqueFileHandle)
pub trait InitFileHandler: Sized {
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 why this needs to be a trait? init() isn't called by the sftp crate, it can be an implementation detail

Comment on lines 21 to 24
let mut tiny_hash = [0u8; ID_LEN];
tiny_hash.copy_from_slice(file_handle.0 .0);
Ok(DemoOpaqueFileHandle { tiny_hash })
Ok(DemoOpaqueFileHandle { handle_id: tiny_hash })
}
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.

Naming it "hash" here is a bit unclear, since it's just an ID not hashed.

format!("{:}-{:}", &private_handle.get_path_ref(), salt).as_str(),
)
.map_err(|_| StatusCode::SSH_FX_FAILURE)?;
let handle = K::init();
Copy link
Copy Markdown
Owner

@mkj mkj Apr 22, 2026

Choose a reason for hiding this comment

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

Would it be easier to call init(counter) rather than having a RNG? The RNG works OK though.

jubeormk1 and others added 22 commits May 1, 2026 17:04
My apologies, I though that It had been merged to the main branch and merged when the env_var (pull 35) feature was included
… use #[sshwire(unknown)]

Without changing some visiblitities, the code generated by SSHDecode (SSHDecodeEnum) for proto.rs would try accessing s.ctx().seen_unknown and Unknown::new() throwing errors.

Am I doing using SSHDecode wrong?
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
Should have run testing/ci.sh beforehand
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.
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
mkj and others added 23 commits May 1, 2026 17:05
Aren't needed now embassy doesn't need rust nightly
This is used to catch bad usages of encode_request. @mkj Please let me know if you want this reverted
Looking for structures with redundant lifetimes resulted in no more of them. The methodology has been analyzing the results of a reg-exp search ( <'[^>]+, ) within sftp containing paths
Also modifying the receiver counter to use a an usize and fencing the counters in a debug_assertions configuration as they are of no use during release
To eliminate the risk in the select for the SftpOutputConsumer to persist on select, receiving 0 bytes on the PipeReader.read will end receive_task function.
This replaces the todo!() for ctx(). Blob::dec() will call ctx()
if the inner length doesn't match the blob length.
After cherry picking mkj@a8a27eb
In order to provide a safe example for future SftpServer implementers.
To provide better usage practices as recommended by Radically open Security, I have removed the "tiny_hash" and replaced it by an unpredictable 32 bytes array.

All salt and seed mention removed. This should make
@mkj thanks for catching this one. I believe that with this name adjustment there is no more references to hash or tiny in variables whiting the demo/sftp/std
Thanks to @mkj for spotting and proposing a fix

Co-authored-by: Copilot <copilot@github.com>
Creating a new module for the read replies and refactoring the SFTP read
operations to use the new types. This is a step towards better separation of concerns and clearer code structure, especially around the handling of read operations in the SFTP server.

This decluttering allows us to isolate the logic related to read replies,
improving readability and maintainability.
This new structures enforce the correct sequence of operations for
handling SFTP read requests, guiding the library user to provide the
necessary information in simple types in the correct order.

Other changes are related to the sftpoutputchannelhandler, where the OutChan has
been replaced by a trait bound Write with an associated error type. This helps testing.
…quests

- Base structure for readdir replies, similar to readreplies.rs
- Added a test to demonstrate how the structure can be used to compose a readdir response
- Refactoring helpers
- Cleaning comments
I moved the std_helpers to the demo to keep the library more readable and not force the user into a particular implementation.

Fixed an issue caused by copy-paste the readreplies.rs where I was not sending the right response back.

Refactored sftphandle to use the new interfaces.

Refreshing lib.rs, renaming some constants, etc
This made me find many problems in the docs and now the tests are finally part of the CI
- 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).

After that I have tested all my ./demo/sftp/std/testing/test_*.sh script and all worked fine except test_get_file_long.sh which triggers a known bug mkj#40

I edited error.rs to avoid a CI check fail around unused use statements.

All test passed after this
@jubeormk1
Copy link
Copy Markdown
Contributor Author

jubeormk1 commented May 1, 2026

Looks like the force-push has caused some unintended changes.

  • Having channel.rs::Channel.check_window_adjust with inmutable &self makes sense since there is no self mutation.
  • The other change is a comment, which might be outdated

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