Skip to content

Comments

test: add authz unit tests#296

Open
xwid138 wants to merge 10 commits intopubky:mainfrom
xwid138:fix-authz
Open

test: add authz unit tests#296
xwid138 wants to merge 10 commits intopubky:mainfrom
xwid138:fix-authz

Conversation

@xwid138
Copy link

@xwid138 xwid138 commented Jan 8, 2026

When reading a path not starting with /pub/ Homeserver responds with Writing to directories other than '/pub/' is forbidden error message which is confusing for GET requests.

WARN request{method=GET uri="/test/hello.txt" version=HTTP/1.1}: pubky_homeserver::client_server::layers::authz: Writing to directories other than '/pub/' is forbidden: 8kcqwm7fw43j73jo4s4thzawzhjodc6zrn9cjyko4pzwxpkmwwey//test/hello.txt. Access forbidden
cargo run user get /test/hello.txt ./recovery.key
.
.
.
Error: Failed to get data

Caused by:
    0: Request failed: Server responded with an error: 403 Forbidden - Writing to directories other than '/pub/' is forbidden
    1: Server responded with an error: 403 Forbidden - Writing to directories other than '/pub/' is forbidden

After change:

Error: Failed to get data

Caused by:
    0: Request failed: Server responded with an error: 400 Bad Request - Invalid URL: Path must start with /pub/
    1: Server responded with an error: 400 Bad Request - Invalid URL: Path must start with /pub/

Copy link
Contributor

@SHAcollision SHAcollision left a comment

Choose a reason for hiding this comment

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

Hey xwid138:fix-authz thank you for bringing this up and coming up with a fix. 🙌

I left a comment for a different take of this fix that could equally solve the problem you exposed without opening up some possible unsafe behaviour.

// return Ok(());
// }
} else {
} else if method == Method::PUT {
Copy link
Contributor

@SHAcollision SHAcollision Jan 8, 2026

Choose a reason for hiding this comment

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

Could potentially allow other writing methods like DELETE into non /pub/ paths.

Feels like this whole conditional is hacky, but it's kindda correct "as is". So we might simply want to change L123 and L128 messages to "Access to non-/pub/ paths is forbidden” so it is accurate for reads too. What do you think?

Copy link
Author

@xwid138 xwid138 Jan 8, 2026

Choose a reason for hiding this comment

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

That's true - my bad.

I suggest adding some unit tests to authz, just to be safe. Wdyt ?

edit: should we allow for OPTIONS method as well in case of /pub/ path ? Only GET and HEADER are allowed.

Copy link
Author

Choose a reason for hiding this comment

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

Message update.

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, we should allow OPTIONS too, and yes, this will greatly benefit from some tests 😅

@SHAcollision
Copy link
Contributor

SHAcollision commented Jan 13, 2026

LGTM! Let me know if you'd like to add more to this PR ( OPTIONS and some tests) or if you would like do that on a new PR and we merge this message change.

Edit: looks like we need to adjust this test for the test suite to be happy

test("forbidden: writing outside /pub returns 403", async (t) => {
const sdk = Pubky.testnet();
const signer = sdk.signer(Keypair.random());
const signupToken = await createSignupToken();
const session = await signer.signup(HOMESERVER_PUBLICKEY, signupToken);
const forbiddenPath = "/priv/example.com/arbitrary";
try {
await session.storage.putText(forbiddenPath as unknown as Path, "Hello");
t.fail("putText to /priv should fail with 403");
} catch (error) {
assertPubkyError(t, error);
t.equal(error.name, "RequestError", "mapped error name");
t.equal(getStatusCode(error), 403, "status code 403");
t.ok(
String(error.message || "").includes(
"Writing to directories other than '/pub/'",
),
"error message mentions /pub restriction",
);
}
t.end();
});

@xwid138
Copy link
Author

xwid138 commented Jan 13, 2026

Thanks for the review @SHAcollision .

I'll work on this PR and add mentioned changes. I will re-request a review once it's ready.

@xwid138 xwid138 changed the title fix: forbid non pub writing only for PUT requests test: add authz unit tests Jan 14, 2026
@xwid138 xwid138 requested a review from SHAcollision January 14, 2026 10:26
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