Manuel B. Garcia

Manuel B. Garcia serves as the Senior Director for Educational Technology and Digital Learning at FEU Institute of Technology, Manila, Philippines. Read More

Contact Info

1607, FEU Tech Building,
P. Paredes St, Sampaloc,
Manila, Philippines
mbgarcia@feutech.edu.ph

Follow Me

Can AI-Generated Research Code Introduce Security Vulnerabilities?

AI-generated research code can contain insecure dependencies, unsafe file operations, weak authentication, and vulnerabilities that expose confidential data or research systems. Learn how researchers can identify security risks, test generated scripts safely, and apply appropriate safeguards.

719
Security Risks in AI-Generated Research Code Guide 719 of 1196
01 · The Question

Can AI-Generated Research Code Work Correctly and Still Be Insecure?

A researcher asks generative AI to write a Python script that retrieves data from an institutional database and saves the results for analysis.

The model produces a working script. It connects successfully, retrieves the requested records, and exports the information to a file.

However, the code stores database credentials directly in the script and constructs queries by inserting user-supplied values into SQL strings.

The program appears functional, but its implementation introduces avoidable security risks.

This distinction is particularly important in research environments, where computational workflows may handle unpublished findings, identifiable participant records, proprietary information, and institutional systems.

Researchers may focus on whether AI-generated code produces the expected scientific results while overlooking how the code handles credentials, files, dependencies, and external connections.

The question is whether generative AI can introduce security vulnerabilities into research software and what researchers should do before executing or deploying generated code.

02 · The Short Answer

Yes, AI-Generated Code Can Be Functional but Insecure

In Brief

AI-generated research code can introduce security vulnerabilities, including unsafe credential handling, injection risks, insecure dependencies, insufficient input validation, unintended data disclosure, and destructive file operations. Code that executes successfully or produces correct analytical results is not necessarily secure.

Researchers should inspect generated code before execution, use appropriately restricted environments, protect secrets, validate external inputs, and review dependencies. Security controls should reflect the sensitivity of the data and systems involved, with qualified technical review for consequential or unfamiliar implementations.

03 · What You Need to Know

How AI-Generated Code Can Compromise Research Security

Why Security Matters in Research Programming

Research software often processes information that is valuable, confidential, or difficult to replace.

Examples include identifiable participant records, unpublished datasets, laboratory results, institutional credentials, proprietary algorithms, and preliminary intellectual property.

Security failures can expose information, alter research records, interrupt project activities, or compromise systems beyond the immediate research workflow.

For example, a script designed to retrieve survey responses may inadvertently disclose an API token that grants broader access to an institutional service.

Researchers should therefore evaluate generated code not only for computational correctness but also for how it accesses, stores, modifies, and transmits information.

Functional Correctness and Security Are Different Properties

A program may perform its intended task while remaining vulnerable to misuse or unintended behavior.

For example, an application may correctly retrieve database records but use unsafe SQL construction that allows untrusted input to alter the query.

Similarly, a script may successfully download research data while transmitting authentication credentials through an insecure channel.

Functional correctness Whether software performs its specified operations under the relevant conditions.
Security Whether appropriate safeguards protect the software, information, and systems against relevant threats.
Research integrity Whether the research process and its records support trustworthy, accountable scientific work.

These properties are related but not interchangeable. A script can produce valid statistics while exposing confidential data, and a technically secure implementation can still perform an inappropriate analysis.

What Security Vulnerabilities Can AI-Generated Code Introduce?

Generative AI may reproduce insecure programming patterns, omit safeguards, or suggest dependencies that introduce additional risks.

The likelihood and severity of these problems depend on the model, prompt, application, and execution environment.

Security Risk Example Potential Consequence
Hardcoded credentials Embedding API keys or database passwords in source files Unauthorized access if the code is disclosed
SQL injection Constructing queries through unsafe string concatenation Unauthorized retrieval or modification of database records
Command injection Passing untrusted values into shell commands Execution of unintended system operations
Unsafe dependencies Installing unverified or vulnerable packages Compromise through third-party software
Insecure file handling Writing sensitive outputs to public directories Unintended disclosure or data loss
Insufficient input validation Accepting unexpected file paths or data structures Unauthorized access or unintended processing
Weak authentication Disabling verification or using insecure access patterns Unauthorized connections or credential exposure
Excessive permissions Running scripts with unnecessary administrative privileges Greater damage if the script behaves unexpectedly
Unintended external transmission Sending research records to an unapproved endpoint Confidentiality and data protection violations

These vulnerabilities are not unique to AI-generated code. Human programmers can introduce the same problems. The concern is that researchers may accept generated implementations without recognizing insecure behavior.

Hardcoded Credentials Are Particularly Dangerous

AI-generated examples sometimes include placeholders for passwords or API keys directly within source code.

Researchers may replace those placeholders with actual credentials and retain them in the script.

If the file is shared with collaborators or uploaded to a public repository, the credentials may become exposed.

Even private repositories can create risks when access controls are inadequate or credentials appear in logs and backups.

Researchers should use approved secret-management mechanisms and avoid storing credentials directly in code.

Environment variables may be suitable for some workflows, while more sensitive institutional systems may require dedicated secret-management services.

The related concern about AI-generated code exposing API keys or credentials deserves particular attention when scripts interact with external services.

AI May Generate Unsafe SQL Queries

When constructing database queries, AI may produce code that inserts user-supplied values directly into SQL statements.

This can create SQL injection vulnerabilities if untrusted input is interpreted as part of the query.

Parameterized queries separate data values from the SQL structure and are generally preferable when supported by the database interface.

However, parameters do not automatically make every query secure. Access permissions, dynamic identifiers, and query logic also require appropriate controls.

Researchers should review generated database code against recognized security guidance, including the OWASP SQL Injection Prevention Cheat Sheet.

This is especially important when using AI to create SQL queries for research databases.

Shell Commands Can Create Unexpected Risks

Research scripts sometimes call operating system commands to process files, execute programs, or manage computational environments.

AI-generated code may construct shell commands using untrusted input or invoke external programs unnecessarily.

If inputs are not handled safely, the resulting operations may differ from what the researcher intended.

Researchers should prefer appropriate programming-language APIs where possible and avoid unnecessary shell execution.

When external commands are required, arguments should be handled through secure interfaces rather than unsafe string construction.

Potentially destructive commands should be tested in controlled environments before use on research systems.

Dependencies Can Introduce Software Supply Chain Risks

AI-generated scripts may recommend installing external libraries or packages.

These dependencies can introduce vulnerabilities, malicious code, compatibility problems, or unexpected behavior.

A model may also suggest a package name that is incorrect or does not correspond to the intended software.

Researchers should verify package identities through official repositories and documentation before installation.

They should also examine dependency versions, maintenance status, and known vulnerabilities where relevant.

Dependency scanning and software composition analysis can support this review, although they do not guarantee that every risk will be detected.

Unnecessary dependencies should be avoided when established libraries or standard-language functionality can perform the task adequately.

AI-Generated Code May Disable Security Controls to Resolve Errors

When researchers ask AI to debug connection or permission problems, the model may propose shortcuts that weaken security.

For example, it might suggest disabling TLS certificate verification to make an HTTPS request succeed.

Such a change may suppress a connection error while increasing exposure to interception or impersonation risks.

Similarly, suggestions to broaden file permissions or disable authentication checks may create unnecessary vulnerabilities.

Researchers should investigate the underlying configuration problem rather than automatically accept a workaround that removes a protective control.

Security-sensitive debugging may require assistance from institutional information technology personnel.

File Operations Can Damage Research Records

Generated scripts may rename, overwrite, move, or delete files.

These operations can be useful for legitimate research automation, but incorrect paths or selection rules may affect unintended records.

For example, a script intended to remove temporary files could delete original research datasets if its directory selection is incorrect.

Researchers should inspect file operations and preserve backups before execution.

Testing on non-sensitive copies can help establish whether the script affects only the intended files.

Where possible, research workflows should separate raw source data from generated outputs and restrict write access to authoritative records.

Research Code Can Expose Confidential Data Through Logs

Logging is useful for diagnosing errors and documenting computational workflows.

However, generated code may record sensitive values unnecessarily.

For example, an API debugging script might print complete request headers containing an authorization token.

A data processing script might also write identifiable participant records into an error log.

Researchers should determine which information is necessary for debugging and avoid logging secrets or unnecessary personal data.

Logs themselves may require access controls and retention procedures.

External Connections Require Careful Inspection

AI-generated scripts may communicate with websites, APIs, package repositories, or other network services.

Researchers should identify every external endpoint and determine whether the connection is necessary and authorized.

For example, a script intended to process a local dataset should not unexpectedly transmit the dataset to an external service.

Network access can sometimes be restricted during testing to identify unnecessary dependencies or attempted connections.

Where external services are required, researchers should verify the provider, purpose, data transmitted, and applicable security arrangements.

AI-Generated Code Should Not Automatically Receive Production Access

Research systems may include operational databases, institutional servers, laboratory equipment, and repositories containing confidential information.

Generated code should not be granted broad access to these systems merely because it performs successfully in a test environment.

Permissions should be limited to the operations necessary for the approved task.

For example, a script retrieving records for analysis generally does not require permission to delete database tables or modify user accounts.

The principle of least privilege can reduce the consequences of unintended or malicious behavior.

How Should Researchers Prompt AI to Generate More Secure Code?

Security requirements should be included in the initial programming instructions rather than introduced only after the script has been written.

However, prompting alone cannot guarantee that generated code follows secure programming practices.

Example Prompt

Generating Security-Conscious Research Code

Write code for the research task described using secure programming practices appropriate to the specified environment. Do not hardcode credentials, disable security verification, or introduce unnecessary external dependencies. Validate untrusted inputs, use parameterized database queries where appropriate, and apply least-privilege access. Avoid destructive file operations unless explicitly required and authorized. Identify external network connections and explain what information they transmit. Include safe error handling without exposing secrets or confidential data in logs. Provide tests and identify security assumptions requiring independent review. Do not claim that the code is secure merely because it executes successfully.

This instruction may encourage safer implementation, but generated code still requires inspection, testing, and appropriate security review.

What Is a Secure Review Workflow for AI-Generated Code?

Researchers should begin by examining the code's purpose, inputs, outputs, dependencies, and permissions.

They should identify operations that access confidential information, execute external commands, modify files, or communicate with remote services.

Static analysis tools may help identify certain vulnerabilities without executing the program.

Dependency scanning can identify known issues in third-party packages.

Controlled execution using fictional or non-sensitive data can reveal unexpected behavior.

However, no single testing method establishes complete security. The review process should reflect the sensitivity and complexity of the intended deployment.

Static Analysis and Dynamic Testing Serve Different Purposes

Static analysis examines code without necessarily executing it.

It may identify suspicious patterns, insecure functions, or violations of coding rules.

Dynamic testing examines software behavior during execution under specified conditions.

For example, researchers may observe which files a script modifies or which network connections it attempts.

Both approaches can contribute useful evidence, but each has limitations.

Static tools may produce false positives or miss context-dependent vulnerabilities. Dynamic tests may fail to exercise dangerous behavior that occurs only under unusual conditions.

Qualified review may be necessary when code processes sensitive data or operates within consequential institutional systems.

How Can Researchers Test Code Without Exposing Real Data?

Researchers can begin with fictional datasets that reproduce the structure of the intended inputs without containing actual participant information.

Testing environments should have restricted permissions and avoid unnecessary access to production systems.

Where appropriate, containers or isolated virtual environments can help separate dependencies and execution processes.

However, isolation technologies have limitations and require correct configuration.

Researchers should not assume that running code in a container automatically eliminates every security risk.

The selected safeguards should reflect the potential consequences of execution.

Security Vulnerabilities Can Affect Research Integrity

A security incident can have consequences beyond confidentiality.

Unauthorized modifications may alter datasets, analytical scripts, or research records.

Malicious or accidental changes can undermine confidence in the provenance and integrity of scientific results.

Researchers should preserve authoritative source records, maintain appropriate backups, and control access to computational workflows.

Version control and audit logs may help identify changes, although they do not independently prevent every compromise.

Security should therefore be understood as one component of trustworthy research computing rather than a separate technical concern.

What Does Research Say About AI-Generated Code Security?

Empirical research has identified security weaknesses in some AI-generated programming outputs.

For example, Pearce and colleagues examined security-relevant code suggestions produced by GitHub Copilot under specified experimental conditions and identified vulnerable outputs.

These findings demonstrate that generated code can contain insecure patterns, but they should not be interpreted as establishing a universal vulnerability rate for every model, language, or application.

Model capabilities and development practices continue to change.

The relevant question for researchers is whether the particular generated implementation satisfies the security requirements of its intended environment.

Recognized resources such as the OWASP Top 10 and NIST Secure Software Development Framework can inform this assessment.

04 · A Practical Example

When an AI-Generated Research Database Script Exposes Credentials

Hypothetical Example

Automating Institutional Learning Analytics Extraction

A researcher is developing a script to retrieve authorized learning analytics records from an institutional database.

Generative AI produces Python code that connects to the database, runs a query, and exports the results.

The script works during initial testing, but its authentication and query construction practices require review.

Step 1: Identify insecure credential handling

The researcher notices that the generated script contains a database username and password directly in the source file.

Step 2: Inspect database query construction

The script also inserts user-supplied filtering values into SQL strings rather than using an appropriate parameterized interface.

Step 3: Revise the implementation

The researcher moves credentials to an approved secret-management mechanism and replaces unsafe query construction with parameterized statements.

Step 4: Restrict database permissions

The research account is configured with the read-only permissions necessary for the approved extraction rather than broad database privileges.

Step 5: Test and review the script

The revised code is tested using fictional records in a controlled environment. Appropriate technical personnel review the relevant security controls before the script is used with institutional data.

The example illustrates why functional success is not sufficient evidence of secure implementation.

The original script retrieved the intended information, but its handling of credentials and database inputs introduced unnecessary risks.

05 · What Researchers Often Get Wrong

Misconceptions About Security in AI-Generated Research Code

Misconception

Code That Produces Correct Results Must Be Secure

Functional correctness does not establish secure credential handling, input validation, dependency safety, or appropriate access controls.

Misconception

AI-Generated Code Is Secure Because the Model Was Trained on Programming Examples

Training data may contain insecure patterns, and models can omit safeguards. Generated implementations require independent security review.

Misconception

Hardcoded Credentials Are Acceptable in Private Research Scripts

Private files can still be shared, logged, backed up, or exposed. Approved secret-management practices are preferable to embedding credentials in source code.

Misconception

Installing an AI-Recommended Package Is Automatically Safe

Package names, sources, dependencies, and known vulnerabilities should be verified before installation.

Misconception

Disabling Security Checks Is a Reasonable Way to Resolve Connection Errors

Removing protective controls may suppress an error while creating new vulnerabilities. Researchers should investigate the underlying configuration problem.

Misconception

Testing Code in a Container Guarantees Complete Isolation

Containers and other isolated environments can reduce some risks, but their effectiveness depends on configuration and the permissions granted to the process.

06 · What This Means for You

How to Use AI-Generated Research Code Without Creating Avoidable Security Risks

Researchers should evaluate generated code according to the sensitivity of the information and systems it will access.

Simple scripts operating on fictional data may require relatively modest safeguards. Code connecting to institutional databases, confidential records, or production services requires more substantial review.

Security should be considered before execution rather than after an incident occurs.

A practical decision framework

If the script processes non-sensitive local test data
Inspect file operations and dependencies, then test in a controlled environment.
If the script requires API keys or database credentials
Use approved secret-management mechanisms and avoid exposing credentials in source files or logs.
If the script accepts external input
Validate inputs and use secure interfaces such as parameterized queries where appropriate.
If the script installs packages or connects to external services
Verify dependencies, destinations, permissions, and the information transmitted.
If the script accesses confidential or production systems
Apply least-privilege permissions, controlled testing, and qualified security review.
If the code's security cannot be adequately evaluated
Do not deploy it until appropriate technical assistance or verification is available.

Maintain a Secure Development Workflow

Researchers should preserve original data, maintain appropriate backups, and use version control for consequential code changes.

Dependencies should be documented and updated through controlled procedures.

Security findings should be resolved before generated scripts receive access to sensitive systems.

Where institutional information technology or security policies apply, those requirements should govern the development and deployment process.

Watch Out

Never execute unfamiliar AI-generated code with broad institutional privileges or access to confidential research data without appropriate inspection and authorization. A script may perform its stated task while exposing credentials, transmitting sensitive information, or modifying files outside the intended workflow.

Generative AI can accelerate research software development, but its convenience should not replace secure implementation, controlled execution, and responsible technical oversight.

07 · A Quick Checklist

Checklist for Security Review of AI-Generated Research Code

Before executing AI-generated research code, check:
Identify the script's intended purpose, inputs, outputs, and required permissions.
Inspect the code for hardcoded credentials, tokens, and confidential information.
Verify input validation and safe database or command execution practices.
Review external dependencies, package sources, and known vulnerabilities.
Identify file modifications, deletion operations, and external network connections.
Confirm that authentication and security verification controls remain enabled.
Apply least-privilege access and appropriate environment isolation.
Test the script using fictional or non-sensitive data before production use.
Obtain qualified technical review when the code accesses sensitive data or consequential systems.
08 · Frequently Asked Questions

Frequently Asked Questions About AI-Generated Research Code Security

Can ChatGPT generate insecure Python code?

Yes. Generated code may contain insecure credential handling, unsafe input processing, vulnerable dependencies, or other weaknesses. Researchers should review and test the implementation before use.

Is AI-generated code less secure than human-written code?

Security varies by model, programmer, task, and development process. Both AI-generated and human-written code can contain vulnerabilities. The particular implementation must be evaluated rather than judged solely by its origin.

Can AI-generated research code expose confidential data?

Yes. Insecure file handling, logs, external connections, or excessive permissions may expose sensitive information. Authorized processing and appropriate safeguards are essential.

Should researchers store API keys in AI-generated scripts?

No. Secret credentials should generally be managed through approved environment or secret-management mechanisms rather than embedded directly in source code.

Can AI-generated SQL queries create security vulnerabilities?

Yes. Unsafe query construction can introduce injection vulnerabilities. Parameterized queries and appropriate database permissions are important safeguards.

Is it safe to install packages recommended by AI?

Not without verification. Researchers should confirm package identity, source, maintenance status, and relevant security information before installation.

Can static analysis tools detect every vulnerability?

No. Static analysis can identify certain insecure patterns, but it may miss context-dependent problems or produce false positives. Other testing and review methods may be necessary.

Should AI-generated code be reviewed before accessing institutional systems?

Yes. Code accessing confidential data or production infrastructure should undergo appropriate security review, testing, and authorization before deployment.

09 · The Bottom Line

Working Research Code Is Not Necessarily Secure Research Code

The Bottom Line

AI-generated research code can introduce security vulnerabilities even when it executes correctly and produces valid analytical results. Secure implementation requires appropriate credential management, input validation, dependency review, restricted permissions, and controlled testing.

Researchers should evaluate generated code according to the sensitivity of the systems and information involved. AI can assist with programming, but it cannot replace the technical safeguards and institutional responsibilities necessary to protect research data and infrastructure.

10 · Sources and Further Reading

Research and Authoritative Guidance on Secure AI-Generated Code

11 · Cite this Guide

How to Cite This Guide

This guide is intended to be read, shared, and used in research, teaching, and academic work. If you draw on its ideas, explanations, or other content, please acknowledge the source by citing the guide. Doing so gives appropriate credit and helps your readers locate the original resource.

Has the Field Guide helped your research?

If a guide helped clarify a question, inform a research decision, or move your work forward, I would love to hear about your experience. Your story may also help other researchers discover the Field Guide.

Share Your Experience
Takes only a few minutes