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: extend detect non literal fs filename #92

Conversation

BuZZ-T
Copy link
Contributor

@BuZZ-T BuZZ-T commented Sep 2, 2022

I think the changes are now in a state where a PR can be created.

I've done the following things:

  • Added "@typescript-eslint/parser" so that TypeScript syntax (in this case: import) can be used in tests
  • Rewritten the detect-literal-fs-filename to check from where the inspected method / function is imported
  • Added "fs/promises" and "fs-extra" as packages to check
  • Also check for node: prefix in package names
  • Added many tests to cover the implemented logic
  • Created utils functions for getting the import or require of a method / function

I'm not sure if the last bullet point makes the code clearer or more complex. It's in an extra commit, so i can revert this part.
Additionally i'm pretty sure there are more useful test-cases, especially valid ones, to prevent false positives.

I appreciate comments and hints for improvement and i hope i'll have the time to implement them fast!

P.S.: This covers #26, #54, #65 and #88

@revelt
Copy link

revelt commented Sep 18, 2022

Good job!

@sakirma
Copy link

sakirma commented Oct 19, 2022

Any idea when these changes will be merged?

@BuZZ-T
Copy link
Contributor Author

BuZZ-T commented Oct 25, 2022

Any idea when these changes will be merged?

Hi @sakirma ! Looks like i still need a reviewer + approval for this. I'm not aware of the review process (if there is one ;) ) in this repository, maybe @nzakas may step in here?

@nzakas
Copy link
Contributor

nzakas commented Oct 27, 2022

So sorry, for some reason I didn’t get a notification about this! I’m buried under notifications at the moment but I’ll plan on looking at this next week.

Question: do any of the tests actually use TypeScript syntax? I just looked quickly and didn’t see any.

@BuZZ-T
Copy link
Contributor Author

BuZZ-T commented Oct 28, 2022

No worries! Thanks for your help!

Question: do any of the tests actually use TypeScript syntax? I just looked quickly and didn’t see any.

I only used the import statement. Is there an eslint-parser that supports that? Then i wouldn't need the typescript-parser.

@nzakas
Copy link
Contributor

nzakas commented Nov 3, 2022

The default parser will work with import statements. You just need to set parserOptions.ecmaVersion to 6 or higher and then set parserOptions.sourceType to “module”. See https://eslint.org/docs/latest/developer-guide/nodejs-api#ruletester

Copy link
Contributor

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

Thanks for this. Overall looks good, just some cleanup tasks to improve clarity for readers.

@@ -0,0 +1,3 @@
[
"fs", "node:fs", "fs/promises", "node:fs/promises", "fs-extra"
]
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we move this into the rule file? There doesn’t appear to be a need for this to be external.

return (argMeta || []).filter((argIndex) => node.arguments[argIndex].type !== 'Literal');
}

function generateReport({ context, node, packageName, methodName, indeces }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

indeces -> indices

return null;
}

const sinks = sinkPositions(node.parent, fsMetaData[methodName]);
Copy link
Contributor

Choose a reason for hiding this comment

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

You use the word “sink” throughout, but it’s not clear to me what this means. Can you rename so that it’s clearer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a taint analysis terminology (https://owasp.org/www-community/controls/Static_Code_Analysis). But i agree that's not a commonly known term. I tried to rename it.

.filter((entry) => entry.type === 'VariableDeclaration')
.flatMap((entry) => entry.declarations)
.find(
(d) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

d -> declaration

for clarity

/**
* Returns the ImportDeclaration for the import of the methodName from one of the packageNames
* import { methodName as a } from 'packageName';
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you include some JSDoc comments for the parameters and return types?

@BuZZ-T
Copy link
Contributor Author

BuZZ-T commented Nov 6, 2022

The default parser will work with import statements. You just need to set parserOptions.ecmaVersion to 6 or higher and then set parserOptions.sourceType to “module”. See https://eslint.org/docs/latest/developer-guide/nodejs-api#ruletester

Ah great. I changed it accordingly.

Thanks for your review! I tried to include all your comments.

I also need to resolve the merge conflicts. Do you prefer merging main in the feature branch or rebasing? I can do both.

@nzakas
Copy link
Contributor

nzakas commented Nov 10, 2022

If you could rebase and fix the conflicts, that would be great.

@nzakas
Copy link
Contributor

nzakas commented Nov 24, 2022

@BuZZ-T just checking back -- do you have time to update this?

@BuZZ-T
Copy link
Contributor Author

BuZZ-T commented Nov 25, 2022

@nzakas: Oh sorry, i missed the notification. I'll fix the conflict

@BuZZ-T BuZZ-T force-pushed the feature/extend-detect-non-literal-fs-filename branch from 76da428 to e46d3e2 Compare November 26, 2022 22:33
@BuZZ-T
Copy link
Contributor Author

BuZZ-T commented Nov 26, 2022

@nzakas: Done. Sorry that it took so long for me to respond!

// this only works, when imports are on top level!
const program = context.getAncestors()[0];

const methodCallSinkReport = detectOnMethodCall({
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we've got one "sink" left. :)

@nzakas
Copy link
Contributor

nzakas commented Nov 30, 2022

It also looks like we have failing tests in Node.js 12 due to using ?..

@BuZZ-T
Copy link
Contributor Author

BuZZ-T commented Dec 1, 2022

It also looks like we have failing tests in Node.js 12 due to using ?..

Ah, i didn't know node 12 should also be supported. I hope they are running now.

Copy link
Contributor

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

That works. Thanks so much for your time and effort.

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.

None yet

4 participants