Skip to content
New issue

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

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

Already on GitHub? # to your account

Prevent m̶a̶l̶i̶c̶i̶o̶u̶s̶ invalid code updates #250

Merged
merged 6 commits into from
May 25, 2017
Merged

Prevent m̶a̶l̶i̶c̶i̶o̶u̶s̶ invalid code updates #250

merged 6 commits into from
May 25, 2017

Conversation

ad-m
Copy link
Contributor

@ad-m ad-m commented May 25, 2017

No description provided.

# Prepare code auditing environment
REPO_URL=$(git remote get-url origin)
TEST_DIR=$(mktemp -d)
git clone "$REPO_URL" "$TEST_DIR";
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of a tmpdir, maybe just use a constant folder so we can just git pull instead of a fresh clone every time.

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 may be embarrassing for future tests, which may leave data - junk.

Copy link
Contributor

Choose a reason for hiding this comment

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

https://stackoverflow.com/a/1210012/345059 could be used to speed up the temp clone as the repo grows

@PlasmaPower
Copy link
Contributor

Note that this doesn't prevent malicious code changes, but rather just syntax errors. Still 👍.

Copy link
Contributor

@reddraggone9 reddraggone9 left a comment

Choose a reason for hiding this comment

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

I'd really rather test each PR individually and just not merge them in the first place if they don't pass, but this is still better than what we have now.

pushd "$TEST_DIR";

# Code auditing section
python -m py_compile $(find . -name '*.py');
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this will use the right python since (from what I see) we're using a virtualenv without actually modifying the environment (e.g. PATH).

Also, something like flake8 would pick up more problems (e.g. use of undefined variables).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Syntax analysis only needs a consistent version of Python. It deliberately does not implement PEP8 analysis, as it may be too rigorous and embarrassing.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mentioned flake8 because it's what I use. I can see why you'd not want to fail on style though. Maybe use pyflakes directly instead? Regardless, like I said above, this is still better than what we have. We can refine it later.

You really do need to use the correct version of python though.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ad-m @reddraggone9 yep the chaos venv python lives in /root/.virtualenvs/chaos/bin/python. We should do something so we don't have to hardcode it for too much longer in our script files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@amoffat , drop virtualenv at all 👍


# Prepare code auditing environment
REPO_URL=$(git remote get-url origin)
TEST_DIR=$(mktemp -d)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should either be cleaned up so we're not wasting disk space or (preferably) use a single, fixed directory. With the latter option, you could fetch and hard reset to origin/master instead of cloning the entire repo every time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, it could be cleaned up.


# Code auditing section
python -m py_compile $(find . -name '*.py');
if [ "$?" != "0" ]
Copy link
Contributor

Choose a reason for hiding this comment

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

Just if [ $? ] will do here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nevermind me, I'm an idiot.

@ad-m
Copy link
Contributor Author

ad-m commented May 25, 2017

@PlasmaPower , every syntax error interferes with our program, so I think it is malicious. Additional analysis will be available in the future.

@PlasmaPower
Copy link
Contributor

@ad-m Malicious implies meaning evil, which most syntax errors aren't.

@@ -16,6 +16,7 @@ fi

# End code auditing section
popd
rm -r "$TEST_DIR";
Copy link
Contributor

@reddraggone9 reddraggone9 May 25, 2017

Choose a reason for hiding this comment

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

But what if it exits up above? Also needs -f since git objects are write-protected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@patricknelson
Copy link
Contributor

malicious:

characterized by malice; intending or intended to do harm.

Aside from the fact that this is still a useful PR, regardless of being incorrectly titled.

@amoffat
Copy link
Contributor

amoffat commented May 25, 2017

@ad-m are you able to test this in the docker dev env to confirm that everything works as designed?

@ad-m
Copy link
Contributor Author

ad-m commented May 25, 2017

@amoffat , I have never use docker yet.

@ad-m ad-m changed the title Prevent malicious code updates Prevent m̶a̶l̶i̶c̶i̶o̶u̶s̶ invalid code updates May 25, 2017
@ad-m
Copy link
Contributor Author

ad-m commented May 25, 2017

@patricknelson , fixed!

@reddraggone9
Copy link
Contributor

This will fail on files with spaces in their names:

$ mkdir test
$ cd test
$ touch test.py 'with space.py'
$ python3 -m py_compile $(find . -name '*.py');
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.5/py_compile.py", line 186, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.5/py_compile.py", line 178, in main
    compile(filename, doraise=True)
  File "/usr/local/lib/python3.5/py_compile.py", line 122, in compile
    source_bytes = loader.get_data(file)
  File "<frozen importlib._bootstrap_external>", line 815, in get_data
FileNotFoundError: [Errno 2] No such file or directory: './with'

Working around this in the shell seems painful. It'd probably be a good idea to eventually rewrite these scripts in python.

@ad-m
Copy link
Contributor Author

ad-m commented May 25, 2017

@reddraggone9 , fixed.

$ touch test.py 'with space.py'
$ find . -name '*.py' -print0 | xargs -0 python -m py_compile;
Sorry: IndentationError: unindent does not match any outer indentation level (chaos.py, line 61)
$ echo $?
123
$ rm chaos.py
$ find . -name '*.py' -print0 | xargs -0 python -m py_compile;
$ echo $?
0

@patricknelson
Copy link
Contributor

Unvoted and revoted... will the vote count now? Relevant feature request: #196

@reddraggone9
Copy link
Contributor

reddraggone9 commented May 25, 2017

You don't need to recast your vote anymore. See #117.

@patricknelson
Copy link
Contributor

@reddraggone9 does this currently document the entirety of how voting works? https://github.com/chaosbot/chaos#voting

i.e. This means you can continuously push to a PR and it'll be valid, but the voting window closes entirely after 3hrs of opening the PR? Assuming this is is relative to that point in time:

Voting goes on for the duration of the voting window - currently 2 or 3 hours, depending on the local server time.

@reddraggone9
Copy link
Contributor

There are corner cases, but that's mostly right. The window gets reset every time you push to your repo, but old votes still count.

pushd "$TEST_DIR";

# Code auditing section
find . -name '*.py' -print0 | xargs -0 python -m py_compile;
Copy link
Contributor

@reddraggone9 reddraggone9 May 25, 2017

Choose a reason for hiding this comment

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

Looks like I commented on the wrong PR.

python3 -m py_compile **/*.py would be cleaner.

@reddraggone9 reddraggone9 mentioned this pull request May 25, 2017
chaosbot added a commit that referenced this pull request May 25, 2017
#250: Prevent m̶a̶l̶i̶c̶i̶o̶u̶s̶ invalid code updates 

Description:


:ok_woman: PR passed with a vote of 15 for and 1 against, with a weighted total of 14.0 and a threshold of 6.2.

Vote record:
@EtherTyper: 1
@JarJak: 1
@Mursaat: 1
@PlasmaPower: 1
@ad-m: 1
@chickenchuck040: 1
@dcstone09: 1
@ike709: 1
@kochmaxence: -1
@patricknelson: 1
@reddraggone9: 1
@rhengles: 1
@rudehn: 1
@sorcio: 1
@viktorsec: 1
@xyproto: 1
@chaosbot chaosbot merged commit 56ade60 into Chaosthebot:master May 25, 2017
@chaosbot
Copy link
Collaborator

🙆‍♀️ PR passed with a vote of 15 for and 1 against, with a weighted total of 14.0 and a threshold of 6.2.

See merge-commit 56ade60 for more details.

@rudehn
Copy link
Contributor

rudehn commented May 26, 2017

error: Unknown subcommand: get-url
usage: git remote [-v | --verbose]
or: git remote add [-t ] [-m ] [-f] [--tags|--no-tags] [--mirror=<fetch|push>]
or: git remote rename
or: git remote remove
or: git remote set-head (-a | --auto | -d | --delete |)
or: git remote [-v | --verbose] show [-n]
or: git remote prune [-n | --dry-run]
or: git remote [-v | --verbose] update [-p | --prune] [( | )...]
or: git remote set-branches [--add] ...
or: git remote set-url [--push] []
or: git remote set-url --add
or: git remote set-url --delete

-v, --verbose         be verbose; must be placed before a subcommand

fatal: The empty string is not a valid path
startup.d/10-git-pull.sh: 7: startup.d/10-git-pull.sh: pushd: not found
startup.d/10-git-pull.sh: 19: startup.d/10-git-pull.sh: popd: not found
Already on 'master'
From https://github.com/chaosbot/Chaos
ef69d70..c80f23f master -> origin/master
ERROR: provided hosts list is empty
ERROR: provided hosts list is empty

@ad-m
Copy link
Contributor Author

ad-m commented May 26, 2017

@rudehn , where did you find it?

@ad-m
Copy link
Contributor Author

ad-m commented May 26, 2017

git remote get-url was added on git/2.7.0. We have to rewrite that.

@rudehn
Copy link
Contributor

rudehn commented May 26, 2017

@ad-m I found it in the logs. http://chaosthebot.com:8081

@ad-m ad-m deleted the prevent-code-changes branch May 26, 2017 14:42
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants