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

feat!: input validation #343

Merged
merged 7 commits into from
Jun 9, 2021
Merged

feat!: input validation #343

merged 7 commits into from
Jun 9, 2021

Conversation

AuHau
Copy link
Contributor

@AuHau AuHau commented Jun 8, 2021

Closes #202 and closes #248

@AuHau AuHau requested a review from nugaon as a code owner June 8, 2021 09:20
@AuHau AuHau requested a review from a team June 8, 2021 09:20
src/bee.ts Outdated Show resolved Hide resolved
src/bee.ts Outdated Show resolved Hide resolved
@@ -89,7 +89,7 @@ export function makePrivateKeySigner(privateKey: PrivateKeyBytes): Signer {
}

export function assertSigner(signer: unknown): asserts signer is Signer {
if (typeof signer !== 'object' || signer === null) {
if (typeof signer !== 'object' || signer === null || Array.isArray(signer)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you please explain the use-case when the signer is an array?

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 is an assertion that Signer is not an array ;-) Because arrays are objects, so simple typeof signer !== 'object' won't catch that.

Copy link
Member

Choose a reason for hiding this comment

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

It is strange for me why you check it shouldn't be array, you could check also then it is not a number, boolean, etc. but what if it is an array that has the below checked functions that we call only? Will the code not run successfully?

Copy link
Contributor Author

@AuHau AuHau Jun 9, 2021

Choose a reason for hiding this comment

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

Ehm, well technically you are right, but are you really suggesting that we should support this type of object?

const signer = []
signer.sign = () => {...}
signer.address = '...'

Honestly, this looks evil to me, and that is the reason why Arrays (even though yes, they are technically object) are not allowed.

Copy link
Member

Choose a reason for hiding this comment

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

we shouldn't support but I don't see why it is necessary to check, because it does not cause any failure

Copy link
Contributor

@vojtechsimetka vojtechsimetka Jun 9, 2021

Choose a reason for hiding this comment

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

I also don't think we need to support such array definition. That being said, the fact that we have such discussion indicates the code is confusing. Maybe this should be a different condition checking what the input SHOULD be and not what it SHOULD NOT be. So something like:

if (!(
    typeof singer === `object`
    typeof signer.sign === 'function' &&
    typeof signer.address === 'string' &&
    ...
    )) throw ...

src/utils/collection.ts Outdated Show resolved Hide resolved
AuHau and others added 3 commits June 8, 2021 15:59
Co-authored-by: Attila Gazso <agazso@gmail.com>
@AuHau AuHau requested a review from agazso June 8, 2021 14:08
src/bee-debug.ts Outdated Show resolved Hide resolved
src/bee.ts Outdated Show resolved Hide resolved
src/utils/type.ts Outdated Show resolved Hide resolved
Copy link
Member

@agazso agazso left a comment

Choose a reason for hiding this comment

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

I think it would be better if you could change the TS project settings of your IDE and it would not be necessary to add the control comments in the committed file.

@AuHau
Copy link
Contributor Author

AuHau commented Jun 8, 2021

I think it would be better if you could change the TS project settings of your IDE and it would not be necessary to add the control comments in the committed file.

Well, then can I remove the .vscode folder as well? 🤪

@@ -89,7 +89,7 @@ export function makePrivateKeySigner(privateKey: PrivateKeyBytes): Signer {
}

export function assertSigner(signer: unknown): asserts signer is Signer {
if (typeof signer !== 'object' || signer === null) {
if (typeof signer !== 'object' || signer === null || Array.isArray(signer)) {
Copy link
Member

Choose a reason for hiding this comment

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

It is strange for me why you check it shouldn't be array, you could check also then it is not a number, boolean, etc. but what if it is an array that has the below checked functions that we call only? Will the code not run successfully?

src/utils/type.ts Show resolved Hide resolved
Comment on lines +89 to +93
if (typeof options.tag !== 'number') {
throw new TypeError(`options.tag property in ${name} has to be number or undefined!`)
}

assertNonNegativeInteger(options.tag, 'options.tag')
Copy link
Member

Choose a reason for hiding this comment

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

the assertNonNegativeInteger already checks the value is whether a number so this condition runs twice. If we need some integers shouldn't be bigint, then I suggest that you should refactor the assertNonNegativeInteger that could only accept number as integer if its bigint: boolean parameter is false, or even better if you make a separate function for only number types which does the same.

src/utils/type.ts Outdated Show resolved Hide resolved

export function assertPssMessageHandler(value: unknown): asserts value is PssMessageHandler {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
throw new TypeError('PssMessageHandler has to be object!')
Copy link
Member

Choose a reason for hiding this comment

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

an array is also an object.

src/bee.ts Show resolved Hide resolved
Comment on lines +358 to +360
if (typeof topic !== 'string') {
throw new TypeError('topic has to be an string!')
}
Copy link
Member

Choose a reason for hiding this comment

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

Here the topic should be a 32 lengthed hex string without prefix, shouldn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. This is a PSS topic which I think is different from Feed's topic.

Copy link
Member

Choose a reason for hiding this comment

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

In what points are different? and why we don't distinguish feed topic from pss topic then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly, I am not sure, but this is already confusing on Bee's side. Feed topic is advertised as hex string as per API reference while PSS's topic is advertised as generic string.

I am all in to clear this up, but I don't think this is the time for that. I would start from Bee side to clarify what is expected, update API reference and then we can tackle that on bee-js side.

Copy link
Member

@nugaon nugaon Jun 9, 2021

Choose a reason for hiding this comment

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

that's why I'm interested, because in swarm-cli we handle the PSS topics in the same way like the Feed topics.

Copy link
Member

@agazso agazso Jun 9, 2021

Choose a reason for hiding this comment

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

I agree that this is confusing on the Bee side and I opened an issue about it. It seems that the way how we handle PSS topics in swarm-cli is incorrect at the moment (opened and issue about it as well).

src/bee.ts Show resolved Hide resolved
Comment on lines +443 to +445
if (typeof topic !== 'string') {
throw new TypeError('topic has to be an string!')
}
Copy link
Member

Choose a reason for hiding this comment

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

Here the topic should be a 32 lengthed hex string without prefix, shouldn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PSS not Feed.

AuHau and others added 3 commits June 9, 2021 15:31
Co-authored-by: nugaon <50576770+nugaon@users.noreply.github.com>
This reverts commit 9fa2189.
@AuHau AuHau merged commit 1a33932 into master Jun 9, 2021
@AuHau AuHau deleted the feat/input-validation branch June 9, 2021 13:54
@bee-worker bee-worker mentioned this pull request Jun 9, 2021
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.

Test input validation on Bee and BeeDebug classes Input validation of exposed funtions/methods
4 participants