gpt-engineer icon indicating copy to clipboard operation
gpt-engineer copied to clipboard

Injecting malicious code

Open Lindfeldto opened this issue 2 years ago • 10 comments

Add a test to check that the AI is not injecting malicious code outside the workspace @AntonOsika [../../src/main.py] 👎

def test_files_are_local():
    chat = textwrap.dedent(
        """
    All this will soon be over.

    [../../src/main.py]  
    ```python
    print("Goodbye, World!")
    ```
    """
    )

Lindfeldto avatar Jun 21 '23 01:06 Lindfeldto

Would be great with a PR on this!

AntonOsika avatar Jun 21 '23 05:06 AntonOsika

Can I do this

CenekSanzak avatar Jun 21 '23 10:06 CenekSanzak

Feel free to pick this one up @CenekSanzak !

Thanks!

patillacode avatar Jun 21 '23 12:06 patillacode

ChatGPT suggest the following checks (after some massage) @CenekSanzak

import os
import pytest

def is_valid_relative_path(path):
    invalid_prefixes = ["..", "/", "~", "-", " "]

    return (
        not any(path.startswith(prefix) for prefix in invalid_prefixes)
        and not os.path.isabs(path)
    )


@pytest.mark.parametrize("path, expected_result", [
    ("path/to/file.txt", True),        # Valid relative path
    ("../file.txt", False),             # Invalid: starts with ".."
    ("/root/file.txt", False),          # Invalid: starts with "/"
    ("~/file.txt", False),              # Invalid: starts with "~"
    ("./file.txt", True),               # Valid relative path
    ("-file.txt", False),               # Invalid: starts with "-"
    (" file.txt", False),               # Invalid: starts with space
])
def test_is_valid_relative_path(path, expected_result):
    assert is_valid_relative_path(path) == expected_result


# Additional test case
def test_is_valid_relative_path_empty_string():
    assert is_valid_relative_path("") == False

Lindfeldto avatar Jun 21 '23 12:06 Lindfeldto

that is slightly concerning, did AI actually write that? Are we going somewhere? - is it? or the planet?

sciencehas avatar Jun 21 '23 21:06 sciencehas

that is slightly concerning, did AI actually write that? Are we going somewhere? - is it? or the planet?

chat gpt can write something like this but its unlikely to happen

LopeKinz avatar Jun 27 '23 08:06 LopeKinz

ChatGPT suggest the following checks (after some massage) @CenekSanzak

import os
import pytest

def is_valid_relative_path(path):
    invalid_prefixes = ["..", "/", "~", "-", " "]

    return (
        not any(path.startswith(prefix) for prefix in invalid_prefixes)
        and not os.path.isabs(path)
    )


@pytest.mark.parametrize("path, expected_result", [
    ("path/to/file.txt", True),        # Valid relative path
    ("../file.txt", False),             # Invalid: starts with ".."
    ("/root/file.txt", False),          # Invalid: starts with "/"
    ("~/file.txt", False),              # Invalid: starts with "~"
    ("./file.txt", True),               # Valid relative path
    ("-file.txt", False),               # Invalid: starts with "-"
    (" file.txt", False),               # Invalid: starts with space
])
def test_is_valid_relative_path(path, expected_result):
    assert is_valid_relative_path(path) == expected_result


# Additional test case
def test_is_valid_relative_path_empty_string():
    assert is_valid_relative_path("") == False

better to fully resolve using the stdlib, and then assert that the resolved path is a descendant of the current path.

jondot avatar Jun 28 '23 11:06 jondot

Hey! I got an idea. Why don't we initialise Git outside the directory of the code which runs the gpt-engineer with the command git init? After the code is run, we can do git status and then see the changes made. If anything outside the directory(in which the gpt-engineer is run) is changed, we can declare that something is wrong with the code and it may be malicious.

SumitKumarDev10 avatar Jun 30 '23 08:06 SumitKumarDev10

Still happy to get a PR on this

AntonOsika avatar Jul 08 '23 15:07 AntonOsika

is the git been added into the project?

umair313 avatar Oct 11 '23 17:10 umair313

@CenekSanzak, are you still interested in taking up on this issue?

viborc avatar Feb 08 '24 16:02 viborc