Skip to content

Commit

Permalink
add an error to kotlin-sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Perdiga committed Oct 18, 2024
1 parent 98b4c89 commit 41dab80
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/codeql-kotlin.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "CodeQL Advanced"
name: "CodeQL Advanced - Kotlin"

on:
pull_request:
Expand Down Expand Up @@ -40,7 +40,6 @@ jobs:
shell: bash
run: |
cd kotlin-sample; gradle build -Dorg.gradle.jvmargs=-Xmx2g --info
exit 1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
Expand Down
3 changes: 3 additions & 0 deletions kotlin-sample/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ dependencies {

// This dependency is used by the application.
implementation(libs.guava)

implementation("mysql:mysql-connector-java:8.0.33") // MySQL driver for JDBC
testImplementation(kotlin("test"))
}

// Apply a specific Java toolchain to ease working on different environments.
Expand Down
44 changes: 43 additions & 1 deletion kotlin-sample/app/src/main/kotlin/org/example/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,55 @@
*/
package org.example

import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.Statement

class App {
val greeting: String
get() {
return "Hello World!"
}
}

fun getUserData(userId: String): ResultSet? {
var resultSet: ResultSet? = null
var connection: Connection? = null
var statement: Statement? = null

try {
// Create a connection to the database (replace with your DB credentials)
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "user", "password")

// Create a statement
statement = connection.createStatement()

// Vulnerable query (user input directly inserted into SQL query, leading to SQL injection)
val query = "SELECT * FROM users WHERE id = '$userId'"

// Execute the query
resultSet = statement.executeQuery(query)

} catch (e: Exception) {
e.printStackTrace()
} finally {
statement?.close()
connection?.close()
}

return resultSet
}

fun main() {
println(App().greeting)
// Example input that could be used for SQL injection
val userId = "1 OR 1=1"
val resultSet = getUserData(userId)

// Process the result set
resultSet?.let {
while (it.next()) {
println("User ID: ${it.getString("id")}, Name: ${it.getString("name")}")
}
}
}

0 comments on commit 41dab80

Please sign in to comment.