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

safety: rework #2279

Merged
merged 9 commits into from Jun 10, 2022
Merged

safety: rework #2279

merged 9 commits into from Jun 10, 2022

Conversation

half-duplex
Copy link
Member

@half-duplex half-duplex commented May 14, 2022

Description

This fixes (or works towards fixing) a bunch of issues with safety.py:

  • .safety doesn't say what the current state is
  • Uses VirusTotal API v2 (though they have no current plans to deprecate it) instead of v3
  • Doesn't find URLs in messages containing anything else ("foo http://example.com" fails)
  • Doesn't make local cache accessible to other plugins
  • Doesn't use already-cached VT results in local mode
  • Doesn't allow configuration of local or strict defaults
  • url.py was not checking the domain blacklist and was also failing to handle "foo http://example.com"
  • Other tweaks and improvements

Checklist

  • I have read CONTRIBUTING.md
  • I can and do license this contribution under the EFLv2
  • No issues are reported by make qa (runs make quality and make test)
  • I have tested the functionality of the things this change touches

@half-duplex half-duplex added the Bugfix Generally, PRs that reference (and fix) one or more issue(s) label May 14, 2022
@half-duplex half-duplex added this to the 8.0.0 milestone May 14, 2022
@half-duplex half-duplex force-pushed the safety-current-status branch 3 times, most recently from 2039502 to e70ae7b Compare May 14, 2022 06:18
@half-duplex half-duplex marked this pull request as ready for review May 14, 2022 06:18
sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/url.py Outdated Show resolved Hide resolved
sopel/modules/url.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
@half-duplex half-duplex force-pushed the safety-current-status branch 6 times, most recently from fd75068 to 0544f91 Compare May 15, 2022 06:51
Exirel
Exirel previously approved these changes May 15, 2022
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

Non-blocking style/correctness nits.

sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
@half-duplex
Copy link
Member Author

I meant to keep this as at least two separate commits, but accidentally squashed them together 😭
Re-requesting reviews because of nontrivial logic changes and new command.

image

@half-duplex half-duplex requested review from Exirel and dgw May 15, 2022 20:11
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

OK, I have been nudged into "actual review" territory.

sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
@half-duplex half-duplex force-pushed the safety-current-status branch 2 times, most recently from c53f07f to 57ec25c Compare May 16, 2022 00:46
@dgw
Copy link
Member

dgw commented May 19, 2022

I'm not going to dig into this much more. Just noting that I added another flake8 plugin to the todo list in #1765 after seeing the mess of imports that type-hinting this plugin added.

Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

I actually thought this was ready to go, but apparently not. Now that I've gone through it once more it's time to find out why GH still shows it in my "Approved 8.0.0" view, and make it not do that.

sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Outdated Show resolved Hide resolved
sopel/modules/safety.py Show resolved Hide resolved
@dgw dgw dismissed Exirel’s stale review June 3, 2022 02:54

Stale/re-requested

Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

All good from my perspective. Probably still wait for re-review from @Exirel to be sure everything he requested is satisfactory too.

sopel/modules/url.py Outdated Show resolved Hide resolved
Copy link
Contributor

@Exirel Exirel left a comment

Choose a reason for hiding this comment

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

While we are upgrading the safety plugin, I think we should replace all the "safety_cache" hard-coded key into a constant, so bot.memory["satefy_cache"] would become bot.memory[SAFETY_CACHE_KEY].

They are also some probable nitpick around docstrings that lack a period here and there, but I couldn't be bothered to check them all.

sopel/modules/url.py Outdated Show resolved Hide resolved
@half-duplex
Copy link
Member Author

Should url.py import safety.SAFETY_CACHE_KEY, or just use the string?
Also considering whether I should just find a way to merge safety_cache and safety_cache_local...

@Exirel
Copy link
Contributor

Exirel commented Jun 4, 2022

Should url.py import safety.SAFETY_CACHE_KEY, or just use the string?

You can import it if you want, both option are fine by me. We can accept that URL totally depends on the safety plugin, because it kind of does. As long as safety is part of core, anyway. Which shouldn't change yet.

@half-duplex
Copy link
Member Author

I don't think URL does depend on the safety plugin - if safety is absent, url just skips those checks. Importing the constant would require a conditional to keep the dependency separate.

@Exirel
Copy link
Contributor

Exirel commented Jun 9, 2022

I don't think URL does depend on the safety plugin

It does, through the data structure that safety exposes: safety_cache[url]["positives"] > 0. This small line implies that safety_cache[url] (when it exists) is a dict that always contains a positive key that is always a numeric that you can compare to 0.

@half-duplex
Copy link
Member Author

Ok, if something uses safety.py's key without following the format there will be issues, but url.py doesn't fail to perform any of its functions if safety.py is absent. I can add two more .get()s if you want, but either way I think I prefer using the string.

@dgw
Copy link
Member

dgw commented Jun 9, 2022

If the code only even tries to read the safety info if the safety plugin is available, you minimize the chance of stepping on something else that has coopted its key name. That, to me, would be the main advantage of e.g.

try:
    from sopel.modules.safety import SAFETY_CACHE_KEY
except ImportError:
    SAFETY_CACHE_KEY = None

# ... other code ...

# in the relevant function
    if SAFETY_CACHE_KEY is not None:
        # use it

Is that perfect? No, because safety might not be enabled. But there's also bot.has_plugin('safety') for additional sanity checking if you want that.

@Exirel
Copy link
Contributor

Exirel commented Jun 9, 2022

both option are fine by me

I'm still OK with that, as I don't think it matters too much at the moment. The Safety feature of Sopel would probably be better with a proper interface dedicated to that, instead of a Plugin. So for now, a string is fine by me. I'm just glad we could talk a bit about it, even tho it's nothing more than nitpicking at this point.

@dgw dgw merged commit eac71b0 into sopel-irc:master Jun 10, 2022
@half-duplex half-duplex deleted the safety-current-status branch June 10, 2022 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bugfix Generally, PRs that reference (and fix) one or more issue(s)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants