orhun / rustypaste

A minimal file upload/pastebin service.
https://blog.orhun.dev/blazingly-fast-file-sharing
MIT License
722 stars 45 forks source link

Store uploader token in xattr so uploader can delete #292

Open jdek opened 1 month ago

jdek commented 1 month ago

This may decrease performance by requiring an xattr lookup, but should be isolated to DELETEs. A configuration option for this feature would be added such that a user has to manually enable it, then on startup the service would check if the target directory has xattr support and error out if it was requested but there is no support.

On DELETE, first the global delete tokens would be checked, if they all fail then xattr would be checked. I think the impact for abuse here is less than just fetching the file itself.

Let me know what you think of this feature, I'd be happy to implement it.

orhun commented 1 month ago

Hey! 👋🏼

The idea sounds very interesting, but I have never used xattr myself before. My questions are:

jdek commented 1 month ago

I think it's worth it, the performance impact really depends on the system you're running it on but this method should be many times faster than a database lookup in any case, on my machine it's 800ns slower to read xattrs in addition to reading the file. It's important to keep in mind that a write will only apply on a successful upload and a read only on a potential delete.

Rust is new to me, so I would just be learning it as I go. I found this crate for xattrs which at a cursory look seems okay. No reason this couldn't be changed or improved later though. The only thing which needs to be decided beforehand is the xattr's name and the format for the token, we could decide to save a hash of the token instead if you think it that would be better (though I would want to make this optional, since for me, anyone who has read access to the directory itself also has a token anyway).

orhun commented 1 month ago

Sounds reasonable. We can move forward with the implementation if you'd like, I can provide more opinions once I see how it works!

tessus commented 1 month ago

I really like the idea. However, this means rustypaste can't be run on Windows anymore. Unless Windows supports extended attributes. TBH, I don't care, because I haven't touched Windows since Windows 3.11, but there might be people who are using rustypaste on Win. On the other hand, if you guard this feature behind an OS flag, all should be fine.

tessus commented 4 weeks ago

@jdek are you looking into this? You can start a PR and I can help you if you get stuck, although I am rather new to Rust too. But orhun is an expert Rust coder in case I get stuck too. ;-)

Just pick a name for the xattr that makes sense e.g. creator_token. We can always change that later.

jdek commented 4 weeks ago

@tessus has it been two weeks already... I've been a bit busy, I'll get a PR up this week.

jdek commented 4 weeks ago

I've had a quick look, and have a couple ideas:

In both of these the case for turning off deletes if there are no delete tokens would be removed, as long as there is one insert token the route should be active. I think for deletes the authentication has to be fully moved into the delete handler itself since we cannot know if it's valid until the file is checked. More generic token handling should probably be preferred since another feature I was interested in was the possibility a token being able to access the list endpoint when its globally disabled to list owned files.

tessus commented 4 weeks ago

I have to think about this.

We might not want that people are automatically allowed to delete the files they uploaded. Additionally an admin would still want to be able to remove other people's files.

I think we need an option that enables this feature: e.g. allow_uploader_to_delete = true|false

If that option is set, the deletion function checks wether the current token is the one stored in the xattr. Does that make sense? Or am I totally off? Maybe orhun also has some input. I am not the owner, so he might want to decide on the design.

jdek commented 4 weeks ago

My thought wasn't about whether it should be allowed in general (besides, from my understanding, GDPR requires you to provide the user with the ability to delete their own content). I'm more wondering about the mechanism by which the token goes from the header into the main logic. In any case I think expanding the tokens to a more generic permission system would be beneficial since it would allow you to do things such as:

tessus commented 4 weeks ago

Let's wait for @orhun's input.

orhun commented 3 weeks ago

I feel like the discussion here derails a bit from xattr into a better token mechanism. The mindset of this project is to keep everything as simple as possible so let's try not to overcomplicate things 🐻

I would much rather discuss this over a implementation/PoC so feel free to go ahead and hack something together :)

I think we need an option that enables this feature: e.g. allow_uploader_to_delete = true|false

Yes, that sounds reasonable.

tessus commented 2 weeks ago

@jdek are you still interested in working on this?

jdek commented 2 weeks ago

@tessus I would love to but am very pressed on time, I can only finish it in the late summer. Apologies. If you want to take it over feel free.

tessus commented 2 weeks ago

Ok, let's see who gets to it first. ;-)