Skip to content

Commit b789bdf

Browse files
Merge pull request #760 from ossf/sql_injection_answers
Lab sql-injection: Move answers to JavaScript
2 parents 29a9b44 + 807fd9b commit b789bdf

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

docs/labs/sql-injection.html

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,6 @@
1212

1313
<!-- See create_labs.md for how to create your own lab! -->
1414

15-
<!-- Sample expected answer -->
16-
<script id="expected0" type="plain/text">
17-
String QueryString = "select * from authors where lastname=?";
18-
PreparedStatement pstmt = connection.prepareStatement(QueryString);
19-
</script>
20-
21-
<script id="correct0" type="plain/text">
22-
\s* String\s+QueryString =
23-
\"select\s+\*\s+from\s+authors\s+where\s+lastname\s*\=\s*\?\s*;?\s*\" \;
24-
\s* PreparedStatement\s+pstmt = connection \. prepareStatement \( QueryString \) \; \s*
25-
</script>
26-
27-
<script id="expected1" type="plain/text">
28-
pstmt.setString(1, search_lastname);
29-
ResultSet results = pstmt.executeQuery( );
30-
</script>
31-
32-
<!--
33-
Note: Java Statement has an "executeQuery" method, of form:
34-
ResultSet executeQuery(String sql)
35-
It requires exactly one parameter and does NOT accept added parameters.
36-
So `executeQuery(sql, search_lastname)` is not legal Java.
37-
38-
See:
39-
https://docs.oracle.com/en/java/javase/22/docs/api/java.sql/java/sql/Statement.html
40-
-->
41-
<script id="correct1" type="plain/text">
42-
\s* pstmt \. setString \( 1 , search_lastname \) \;
43-
\s* ResultSet\s+results = pstmt \. executeQuery \( \) \; \s*
44-
</script>
45-
46-
47-
4815
</head>
4916
<body>
5017
<!-- For GitHub Pages formatting: -->

docs/labs/sql-injection.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ info =
33
hints: [
44
{
55
present: "search_lastname",
6-
text: "You should replace \"search_lastname\" with a placeholder.",
6+
text: "You should replace \"search_lastname\" with a placeholder (?).",
77
index: 0,
88
examples: [
99
[
@@ -30,7 +30,7 @@ info =
3030
{
3131
absent: String.raw`\s* PreparedStatement\s+pstmt = connection \.
3232
prepareStatement \( QueryString \) \; \s*`,
33-
text: "Your second line should have the form `PreparedStatement pstmt = connection.prepareStatement(QueryString);`"
33+
text: "After defining the query string you should create a prepared statement, using the form `PreparedStatement pstmt = connection.prepareStatement(QueryString);`"
3434
},
3535
{
3636
absent: "search_lastname",
@@ -54,5 +54,26 @@ info =
5454
index: 1,
5555
text: "After using `setString` execute the query and place the results in `results`, something like `ResultSet results = pstmt.executeQuery();`"
5656
}
57-
]
57+
],
58+
expected: [
59+
String.raw` String QueryString = "select * from authors where lastname=?";
60+
PreparedStatement pstmt = connection.prepareStatement(QueryString);`,
61+
String.raw` pstmt.setString(1, search_lastname);
62+
ResultSet results = pstmt.executeQuery( );`,
63+
],
64+
correct: [
65+
String.raw`\s* String\s+QueryString =
66+
\"select\s+\*\s+from\s+authors\s+where\s+lastname\s*\=\s*\?\s*;?\s*\" \;
67+
\s* PreparedStatement\s+pstmt = connection \.
68+
prepareStatement \( QueryString \) \; \s*`,
69+
// Note: Java Statement has an "executeQuery" method, of form:
70+
// ResultSet executeQuery(String sql)
71+
// It requires exactly one parameter and does NOT accept added parameters.
72+
// So `executeQuery(sql, search_lastname)` is not legal Java.
73+
// Some documents and online help forums get this wrong.
74+
// For the authoritative answer (in Java 22), see:
75+
// https://docs.oracle.com/en/java/javase/22/docs/api/java.sql/java/sql/Statement.html
76+
String.raw`\s* pstmt \. setString \( 1 , search_lastname \) \;
77+
\s* ResultSet\s+results = pstmt \. executeQuery \( \) \; \s*`,
78+
],
5879
}

0 commit comments

Comments
 (0)