Skip to content

Tighten string to non-empty-string where possible#936

Closed
Slamdunk wants to merge 2 commits into
lcobucci:5.0.xfrom
Slamdunk:non_empty_string
Closed

Tighten string to non-empty-string where possible#936
Slamdunk wants to merge 2 commits into
lcobucci:5.0.xfrom
Slamdunk:non_empty_string

Conversation

@Slamdunk

Copy link
Copy Markdown
Collaborator

No description provided.

throw CannotEncodeContent::jsonIssues($exception);
}

assert($jsonEncoded !== '');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hmm, I would expect psalm and phpstan to have better type inference here 🤔

Comment thread src/Parser.php
/**
* Parses the JWT and returns a token
*
* @param non-empty-string $jwt

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think a parser should accept an empty string here, with CannotDecodeContent as an outcome 🤔

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

An empty string JWT is always an error, just like a boolean would be, that's why there's a string native type ATM.
Why not preventing also empty string at all?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Need to think about it, but I see a CannotDecodeContent exception as quite valuable on an empty string scenario here: forcing further verification on the client side seems wasteful, at this stage.

I agree that a string&empty will always result in a crash here: just wondering whether forcing an additional upfront assertion is added value.

In the following (valid) code:

function foo(string $token) {
    Assert::notEmpty($token);
    $parsed = $this->parser->parse($token);

    // ...

If a CannotDecodeContent is thrown (explaining that the content was empty) is the same value of AssertionFailed, except with an additional piece of code for it.

I'm just trying to figure out the ergonomics of the library here:

  • correctness leads to string&empty -> never
  • usability leads to non-empty-string|(string&empty) -> Token|Exception

@Slamdunk Slamdunk Oct 11, 2022

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Now I understand better your doubts.

I can only say the sooner the type is safe, the better:

  • if the library allows an empty string, my whole app may allow it
  • if the library denies an empty string, I have to design the app upfront to get rid of it. This basically means input validation before consumption

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There is one good thing coming from this new type restriction: the existence of the token must be verified by the consumer.

Anyway, I'm just presenting the options: I think that the parser (being a parser) is responsible for upcasting what comes in (or throwing), so it's our decision to decide how wide the API is supposed to be.

The parser makes the type safe anyway, so I have doubts on the value of an early assertion, in this specific case.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That said, we can experiment with this, and then later-on relax the type constraint, if we consider it excessive.

Comment thread src/Signer/Ecdsa.php

final public function verify(string $expected, string $payload, Key $key): bool
{
assert($expected !== '');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wait, is the signature not supposed to be non-empty upfront?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not until we allow \Lcobucci\JWT\Signer\None to exist.

\Lcobucci\JWT\Signer::verify API must take into account all available signers.

$this->expectExceptionMessage('The JWT string must have two dots');

$parser->parse('');
$parser->parse('.');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

IMO OK to parse an empty string and get a crash here.

Comment thread src/Encoding/JoseEncoder.php
@Slamdunk

Slamdunk commented Nov 7, 2022

Copy link
Copy Markdown
Collaborator Author

Closed in favor of #939

@Slamdunk Slamdunk closed this Nov 7, 2022
@Slamdunk
Slamdunk deleted the non_empty_string branch March 8, 2023 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants