Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block invalid poptoken #1660

Merged

Conversation

dayan9265
Copy link
Contributor

@dayan9265 dayan9265 commented Jun 8, 2023

This PR blocks invalid pop token when scanning (this avoid bugs with wrong channel created, and then when trying to connect to these channel it fails, which could lead to the app thinking it is offline)

PS: the updated snap test is due to the current state of the release not being correct

@dayan9265 dayan9265 requested a review from a team as a code owner June 8, 2023 15:36
@dayan9265 dayan9265 requested a review from MeKHell June 8, 2023 15:37
@dayan9265 dayan9265 self-assigned this Jun 8, 2023
@dayan9265 dayan9265 added fe1-web prod-ready Production Ready related pull requests bug-fix Fixes a bug labels Jun 8, 2023
Tyratox
Tyratox previously approved these changes Jun 8, 2023
Copy link
Contributor

@Tyratox Tyratox left a comment

Choose a reason for hiding this comment

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

LGTM! 😄We already had something like this in the past, I marked it above. Feel free to check it out or just merge this one. I think the other regex was even a bit more strict because the pop tokens are all of the same size :)

Copy link
Contributor

Choose a reason for hiding this comment

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

At some time in the past we had something like that already ..

const tokenMatcher = new RegExp('^[A-Za-z0-9_-]{43}=$');

It might be nice to put this validation into the constructor of ScannablePopToken?

Comment on lines 115 to 118
if (!popToken.match(/^[a-zA-Z0-9_-]+={0,3}$/) || popToken.length % 4 !== 0) {
throw new Error('Invalid popToken format');
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is better if this is moved to at least another function or the ScannablePopToken.fromJson which is used line 144

@dayan9265
Copy link
Contributor Author

It was indeed a better idea to put the verification inside ScannablePopToken, so I moved it. (I let the tests inside the scanner, since we want to make sure that wrong tokens are refused)

@dayan9265
Copy link
Contributor Author

Okay it seems that there's now a risk when calling the ScannablePopToken generator with empty strings (such as in the RollCallOpen), I saw there is an another place where it could be the case:

https://github.com/dedis/popstellar/blob/release-2023-spring/fe1-web/src/features/digital-cash/screens/SendReceive/SendReceive.tsx#LL332C1-L334C15

@Tyratox since this is code that you had written, do you know if it can cause an issue?

@Tyratox
Copy link
Contributor

Tyratox commented Jun 9, 2023

Hmm true. While not ideal, this is the reality and I think the best solution for now would be to add a check like

const serializedPopToken = useMemo(() => {
    try {
        return ScannablePopToken.encodePopToken({ pop_token: popToken })
    } catch {
        return null;
    }
}, [popToken]);

if serializedPopToken == null {
    return <SomeNiceErrorMessage/>;
}

While rollCallId should always be set, it might be that you somehow land on this screen for a roll call where you don't have a pop token yet (because the roll call is still ongoing). In that case that could indeed happen I think 😅

Comment on lines 186 to 188
popToken !== ''
? ScannablePopToken.encodePopToken({ pop_token: popToken })
: undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

although I don't see how, this technically could throw.
Would it make sense to do this operation beforehand and wrap it like you did in digital cash ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this could throw since the popToken is initially empty, and it is changed through a promise (generateToken), so for a brief moment the popToken will be an empty string
I will wrap it earlier 👍

Xelowak
Xelowak previously approved these changes Jun 10, 2023
Copy link
Contributor

@Xelowak Xelowak left a comment

Choose a reason for hiding this comment

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

LGTM!
I made a small comment but I won't be blocking you right now so feel free to merge.

Comment on lines +307 to +312
const serializedPopToken = useMemo(() => {
try {
return ScannablePopToken.encodePopToken({ pop_token: popToken });
} catch {
return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't you return an empty string there directly then (in catch)?
It seems that you return null to convert it in an empty string after if it's null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea was that if the popToken is null, it displays nothing (the return null after the toast show). In reality the || '' condition will never be used (I have removed it now)

@sonarcloud
Copy link

sonarcloud bot commented Jun 10, 2023

[PoP - Be1-Go] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarcloud
Copy link

sonarcloud bot commented Jun 10, 2023

[PoP - Be2-Scala] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarcloud
Copy link

sonarcloud bot commented Jun 10, 2023

[PoP - Fe2-Android] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarcloud
Copy link

sonarcloud bot commented Jun 10, 2023

[PoP - Fe1-Web] SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

50.0% 50.0% Coverage
0.0% 0.0% Duplication

Copy link
Contributor

@pierluca pierluca left a comment

Choose a reason for hiding this comment

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

Approving blindly. Can't review right now.

@dayan9265 dayan9265 merged commit 858c67d into release-2023-spring Jun 11, 2023
@dayan9265 dayan9265 deleted the work-fe1-dmassonn-block-invalid-poptoken branch June 11, 2023 10:44
Copy link
Contributor

@Xelowak Xelowak left a comment

Choose a reason for hiding this comment

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

It's ok for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-fix Fixes a bug fe1-web prod-ready Production Ready related pull requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants