Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions languages/java/custom/models/path-injection.model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
extensions:
- addsTo:
pack: codeql/java-all
extensible: sinkModel
data:
# Servlet multipart upload: `Part.write(fileName)` writes the uploaded content to `fileName`.
- ["jakarta.servlet.http", "Part", True, "write", "(String)", "", "Argument[0]", "path-injection", "manual"]
- ["javax.servlet.http", "Part", True, "write", "(String)", "", "Argument[0]", "path-injection", "manual"]
# Spring multipart upload: `MultipartFile.transferTo(dest)` writes the uploaded content to `dest`.
- [
"org.springframework.web.multipart",
"MultipartFile",
True,
"transferTo",
"(File)",
"",
"Argument[0]",
"path-injection",
"manual"
]
- [
"org.springframework.web.multipart",
"MultipartFile",
True,
"transferTo",
"(Path)",
"",
"Argument[0]",
"path-injection",
"manual"
]
7 changes: 7 additions & 0 deletions languages/java/custom/models/qlpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: languages-java-custom-models
version: 0.0.1
library: true
extensionTargets:
codeql/java-all: "*"
dataExtensions:
- "**/*.model.yml"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.function.Supplier;
import org.springframework.web.multipart.MultipartFile;

// Upload handler that stores the file under a fixed root using the raw client-supplied
// original file name, via a shared helper that resolves the name against the root.
public class ContentDispositionUpload {
private Path contentDispositionRoot = Paths.get("/content-disposition/");

private Path root = Paths.get("/static/uploads/");

private static String store(Path root, String fileName, Supplier<Boolean> validator, MultipartFile file)
throws IOException {
if (validator.get()) {
Files.copy(file.getInputStream(), root.resolve(fileName), StandardCopyOption.REPLACE_EXISTING); // $ Alert
return fileName;
}
return null;
}

public String uploadContentDisposition(MultipartFile file) throws IOException {
return store(contentDispositionRoot, file.getOriginalFilename(), () -> true, file); // $ Source
}

public String uploadPrefixed(MultipartFile file) throws IOException {
return store(root, System.nanoTime() + "_" + file.getOriginalFilename(), () -> true, file); // $ Source
}

public String uploadSafe(MultipartFile file) throws IOException {
return store(root, java.util.UUID.randomUUID() + ".png", () -> true, file);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.io.IOException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.Part;

public class JakartaServletUpload {
private final String uploadDir = "/var/app/uploads";

public void doPost(HttpServletRequest request) throws IOException {
String username = request.getParameter("username"); // $ Source
Part part = request.getPart("file");

part.write(uploadDir + "/" + username + ".png"); // $ Alert

part.write(uploadDir + "/avatar.png");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

public class JavaxServletUpload {
private final String uploadDir = "/var/app/uploads";

public void doPost(HttpServletRequest request) throws IOException {
String username = request.getParameter("username"); // $ Source
Part part = request.getPart("file");

part.write(uploadDir + "/" + username + ".png"); // $ Alert

part.write(uploadDir + "/avatar.png");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class ProfileController {
private final String uploadDir = "/var/app/resources/images";

private String storedUsername = "alice";

@RequestMapping("/profile")
public String processProfile(
@RequestParam(value = "username", required = true) String username, // $ Source
@RequestParam(value = "file", required = false) MultipartFile file)
throws IOException {
String extension = ".png";

// String concatenation into `new File(String)` then `MultipartFile.transferTo(File)`
String path = uploadDir + File.separator + username + extension;
file.transferTo(new File(path)); // $ Alert

File dest = new File(uploadDir + "/" + username + ".png");
file.transferTo(dest); // $ Alert

// `new File(File, String)` child argument
file.transferTo(new File(new File(uploadDir), username + ".png")); // $ Alert

// `MultipartFile.transferTo(Path)`
file.transferTo(Paths.get(uploadDir, username + ".png")); // $ Alert
file.transferTo(Paths.get(uploadDir).resolve(username + ".png")); // $ Alert

// Already detected without custom models: `File` constructor summary into a built-in sink
new FileOutputStream(new File(uploadDir + "/" + username + ".png")).close(); // $ Alert

return "profile";
}

@RequestMapping("/profile/update")
public String updateProfile(
@RequestParam(value = "username", required = true) String username, // $ Source
@RequestParam(value = "file", required = false) MultipartFile file) // $ Source
throws IOException {
// An equality check on one branch does not sanitize the merged path below
if (!username.equals(storedUsername)) {
storedUsername = username.toLowerCase();
}

if (file != null && !file.isEmpty()) {
String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); // $ Source
String path = uploadDir + File.separator + username + extension;
file.transferTo(new File(path)); // $ Alert
}

return "profile";
}

@RequestMapping("/profile/safe")
public String processProfileSafe(
@RequestParam(value = "username", required = true) String username,
@RequestParam(value = "file", required = false) MultipartFile file)
throws IOException {
// Constant destination: not tainted
file.transferTo(new File(uploadDir + "/avatar.png"));

// Value that does not come from the request: not a source
file.transferTo(new File(uploadDir + "/" + storedUsername + ".png"));

// Path traversal check sanitizes the request value
if (username.contains("..") || username.contains("/") || username.contains("\\")) {
return "error";
}
file.transferTo(new File(uploadDir + "/" + username + ".png"));

return "profile";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#select
| ContentDispositionUpload.java:19:47:19:68 | resolve(...) | ContentDispositionUpload.java:26:46:26:71 | getOriginalFilename(...) : String | ContentDispositionUpload.java:19:47:19:68 | resolve(...) | This path depends on a $@. | ContentDispositionUpload.java:26:46:26:71 | getOriginalFilename(...) | user-provided value |
| ContentDispositionUpload.java:19:47:19:68 | resolve(...) | ContentDispositionUpload.java:30:54:30:79 | getOriginalFilename(...) : String | ContentDispositionUpload.java:19:47:19:68 | resolve(...) | This path depends on a $@. | ContentDispositionUpload.java:30:54:30:79 | getOriginalFilename(...) | user-provided value |
| JakartaServletUpload.java:12:20:12:54 | ... + ... | JakartaServletUpload.java:9:27:9:58 | getParameter(...) : String | JakartaServletUpload.java:12:20:12:54 | ... + ... | This path depends on a $@. | JakartaServletUpload.java:9:27:9:58 | getParameter(...) | user-provided value |
| JavaxServletUpload.java:12:20:12:54 | ... + ... | JavaxServletUpload.java:9:27:9:58 | getParameter(...) : String | JavaxServletUpload.java:12:20:12:54 | ... + ... | This path depends on a $@. | JavaxServletUpload.java:9:27:9:58 | getParameter(...) | user-provided value |
| ProfileController.java:25:25:25:38 | new File(...) | ProfileController.java:18:13:18:78 | username : String | ProfileController.java:25:25:25:38 | new File(...) | This path depends on a $@. | ProfileController.java:18:13:18:78 | username | user-provided value |
| ProfileController.java:28:25:28:28 | dest | ProfileController.java:18:13:18:78 | username : String | ProfileController.java:28:25:28:28 | dest | This path depends on a $@. | ProfileController.java:18:13:18:78 | username | user-provided value |
| ProfileController.java:31:25:31:72 | new File(...) | ProfileController.java:18:13:18:78 | username : String | ProfileController.java:31:25:31:72 | new File(...) | This path depends on a $@. | ProfileController.java:18:13:18:78 | username | user-provided value |
| ProfileController.java:34:25:34:63 | get(...) | ProfileController.java:18:13:18:78 | username : String | ProfileController.java:34:25:34:63 | get(...) | This path depends on a $@. | ProfileController.java:18:13:18:78 | username | user-provided value |
| ProfileController.java:35:25:35:71 | resolve(...) | ProfileController.java:18:13:18:78 | username : String | ProfileController.java:35:25:35:71 | resolve(...) | This path depends on a $@. | ProfileController.java:18:13:18:78 | username | user-provided value |
| ProfileController.java:38:30:38:74 | new File(...) | ProfileController.java:18:13:18:78 | username : String | ProfileController.java:38:30:38:74 | new File(...) | This path depends on a $@. | ProfileController.java:18:13:18:78 | username | user-provided value |
| ProfileController.java:56:29:56:42 | new File(...) | ProfileController.java:45:13:45:78 | username : String | ProfileController.java:56:29:56:42 | new File(...) | This path depends on a $@. | ProfileController.java:45:13:45:78 | username | user-provided value |
| ProfileController.java:56:29:56:42 | new File(...) | ProfileController.java:46:13:46:78 | file : MultipartFile | ProfileController.java:56:29:56:42 | new File(...) | This path depends on a $@. | ProfileController.java:46:13:46:78 | file | user-provided value |
| ProfileController.java:56:29:56:42 | new File(...) | ProfileController.java:54:32:54:57 | getOriginalFilename(...) : String | ProfileController.java:56:29:56:42 | new File(...) | This path depends on a $@. | ProfileController.java:54:32:54:57 | getOriginalFilename(...) | user-provided value |
edges
| ContentDispositionUpload.java:16:44:16:58 | fileName : String | ContentDispositionUpload.java:19:60:19:67 | fileName : String | provenance | |
| ContentDispositionUpload.java:19:60:19:67 | fileName : String | ContentDispositionUpload.java:19:47:19:68 | resolve(...) | provenance | MaD:12 Sink:MaD:3 |
| ContentDispositionUpload.java:26:46:26:71 | getOriginalFilename(...) : String | ContentDispositionUpload.java:16:44:16:58 | fileName : String | provenance | Src:MaD:9 |
| ContentDispositionUpload.java:30:28:30:79 | ... + ... : String | ContentDispositionUpload.java:16:44:16:58 | fileName : String | provenance | |
| ContentDispositionUpload.java:30:54:30:79 | getOriginalFilename(...) : String | ContentDispositionUpload.java:30:28:30:79 | ... + ... : String | provenance | Src:MaD:9 |
| JakartaServletUpload.java:9:27:9:58 | getParameter(...) : String | JakartaServletUpload.java:12:20:12:54 | ... + ... | provenance | Src:MaD:7 Sink:MaD:1 |
| JavaxServletUpload.java:9:27:9:58 | getParameter(...) : String | JavaxServletUpload.java:12:20:12:54 | ... + ... | provenance | Src:MaD:8 Sink:MaD:4 |
| ProfileController.java:18:13:18:78 | username : String | ProfileController.java:25:34:25:37 | path : String | provenance | |
| ProfileController.java:18:13:18:78 | username : String | ProfileController.java:27:30:27:64 | ... + ... : String | provenance | |
| ProfileController.java:18:13:18:78 | username : String | ProfileController.java:31:25:31:72 | new File(...) | provenance | AdditionalTaintStep Sink:MaD:5 |
| ProfileController.java:18:13:18:78 | username : String | ProfileController.java:34:46:34:62 | ... + ... : String | provenance | |
| ProfileController.java:18:13:18:78 | username : String | ProfileController.java:35:54:35:70 | ... + ... : String | provenance | |
| ProfileController.java:18:13:18:78 | username : String | ProfileController.java:38:39:38:73 | ... + ... : String | provenance | |
| ProfileController.java:25:34:25:37 | path : String | ProfileController.java:25:25:25:38 | new File(...) | provenance | MaD:10 Sink:MaD:5 |
| ProfileController.java:27:21:27:65 | new File(...) : File | ProfileController.java:28:25:28:28 | dest | provenance | Sink:MaD:5 |
| ProfileController.java:27:30:27:64 | ... + ... : String | ProfileController.java:27:21:27:65 | new File(...) : File | provenance | MaD:10 |
| ProfileController.java:34:25:34:63 | new ..[] { .. } : String[] [[]] : String | ProfileController.java:34:25:34:63 | get(...) | provenance | MaD:13 Sink:MaD:6 |
| ProfileController.java:34:46:34:62 | ... + ... : String | ProfileController.java:34:25:34:63 | new ..[] { .. } : String[] [[]] : String | provenance | |
| ProfileController.java:35:54:35:70 | ... + ... : String | ProfileController.java:35:25:35:71 | resolve(...) | provenance | MaD:12 Sink:MaD:6 |
| ProfileController.java:38:39:38:73 | ... + ... : String | ProfileController.java:38:30:38:74 | new File(...) | provenance | MaD:10 Sink:MaD:2 |
| ProfileController.java:45:13:45:78 | username : String | ProfileController.java:56:38:56:41 | path : String | provenance | |
| ProfileController.java:46:13:46:78 | file : MultipartFile | ProfileController.java:54:32:54:35 | file : MultipartFile | provenance | |
| ProfileController.java:54:32:54:35 | file : MultipartFile | ProfileController.java:54:32:54:57 | getOriginalFilename(...) : String | provenance | MaD:14 |
| ProfileController.java:54:32:54:57 | getOriginalFilename(...) : String | ProfileController.java:54:32:54:112 | substring(...) : String | provenance | Src:MaD:9 MaD:11 |
| ProfileController.java:54:32:54:112 | substring(...) : String | ProfileController.java:56:38:56:41 | path : String | provenance | |
| ProfileController.java:56:38:56:41 | path : String | ProfileController.java:56:29:56:42 | new File(...) | provenance | MaD:10 Sink:MaD:5 |
models
| 1 | Sink: jakarta.servlet.http; Part; true; write; (String); ; Argument[0]; path-injection; manual |
| 2 | Sink: java.io; FileOutputStream; false; FileOutputStream; ; ; Argument[0]; path-injection; manual |
| 3 | Sink: java.nio.file; Files; false; copy; (InputStream,Path,CopyOption[]); ; Argument[1]; path-injection; manual |
| 4 | Sink: javax.servlet.http; Part; true; write; (String); ; Argument[0]; path-injection; manual |
| 5 | Sink: org.springframework.web.multipart; MultipartFile; true; transferTo; (File); ; Argument[0]; path-injection; manual |
| 6 | Sink: org.springframework.web.multipart; MultipartFile; true; transferTo; (Path); ; Argument[0]; path-injection; manual |
| 7 | Source: jakarta.servlet; ServletRequest; false; getParameter; (String); ; ReturnValue; remote; manual |
| 8 | Source: javax.servlet; ServletRequest; false; getParameter; (String); ; ReturnValue; remote; manual |
| 9 | Source: org.springframework.web.multipart; MultipartFile; true; getOriginalFilename; (); ; ReturnValue; remote; manual |
| 10 | Summary: java.io; File; false; File; ; ; Argument[0]; Argument[this]; taint; manual |
| 11 | Summary: java.lang; String; false; substring; ; ; Argument[this]; ReturnValue; taint; manual |
| 12 | Summary: java.nio.file; Path; true; resolve; ; ; Argument[0]; ReturnValue; taint; manual |
| 13 | Summary: java.nio.file; Paths; true; get; ; ; Argument[1].ArrayElement; ReturnValue; taint; manual |
| 14 | Summary: org.springframework.web.multipart; MultipartFile; true; getOriginalFilename; ; ; Argument[this]; ReturnValue; taint; manual |
nodes
| ContentDispositionUpload.java:16:44:16:58 | fileName : String | semmle.label | fileName : String |
| ContentDispositionUpload.java:19:47:19:68 | resolve(...) | semmle.label | resolve(...) |
| ContentDispositionUpload.java:19:60:19:67 | fileName : String | semmle.label | fileName : String |
| ContentDispositionUpload.java:26:46:26:71 | getOriginalFilename(...) : String | semmle.label | getOriginalFilename(...) : String |
| ContentDispositionUpload.java:30:28:30:79 | ... + ... : String | semmle.label | ... + ... : String |
| ContentDispositionUpload.java:30:54:30:79 | getOriginalFilename(...) : String | semmle.label | getOriginalFilename(...) : String |
| JakartaServletUpload.java:9:27:9:58 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| JakartaServletUpload.java:12:20:12:54 | ... + ... | semmle.label | ... + ... |
| JavaxServletUpload.java:9:27:9:58 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| JavaxServletUpload.java:12:20:12:54 | ... + ... | semmle.label | ... + ... |
| ProfileController.java:18:13:18:78 | username : String | semmle.label | username : String |
| ProfileController.java:25:25:25:38 | new File(...) | semmle.label | new File(...) |
| ProfileController.java:25:34:25:37 | path : String | semmle.label | path : String |
| ProfileController.java:27:21:27:65 | new File(...) : File | semmle.label | new File(...) : File |
| ProfileController.java:27:30:27:64 | ... + ... : String | semmle.label | ... + ... : String |
| ProfileController.java:28:25:28:28 | dest | semmle.label | dest |
| ProfileController.java:31:25:31:72 | new File(...) | semmle.label | new File(...) |
| ProfileController.java:34:25:34:63 | get(...) | semmle.label | get(...) |
| ProfileController.java:34:25:34:63 | new ..[] { .. } : String[] [[]] : String | semmle.label | new ..[] { .. } : String[] [[]] : String |
| ProfileController.java:34:46:34:62 | ... + ... : String | semmle.label | ... + ... : String |
| ProfileController.java:35:25:35:71 | resolve(...) | semmle.label | resolve(...) |
| ProfileController.java:35:54:35:70 | ... + ... : String | semmle.label | ... + ... : String |
| ProfileController.java:38:30:38:74 | new File(...) | semmle.label | new File(...) |
| ProfileController.java:38:39:38:73 | ... + ... : String | semmle.label | ... + ... : String |
| ProfileController.java:45:13:45:78 | username : String | semmle.label | username : String |
| ProfileController.java:46:13:46:78 | file : MultipartFile | semmle.label | file : MultipartFile |
| ProfileController.java:54:32:54:35 | file : MultipartFile | semmle.label | file : MultipartFile |
| ProfileController.java:54:32:54:57 | getOriginalFilename(...) : String | semmle.label | getOriginalFilename(...) : String |
| ProfileController.java:54:32:54:112 | substring(...) : String | semmle.label | substring(...) : String |
| ProfileController.java:56:29:56:42 | new File(...) | semmle.label | new File(...) |
| ProfileController.java:56:38:56:41 | path : String | semmle.label | path : String |
subpaths
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
query: Security/CWE/CWE-022/TaintedPath.ql
postprocess:
- utils/test/PrettyPrintModels.ql
- utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package jakarta.servlet;

public interface ServletRequest {
String getParameter(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package jakarta.servlet.http;

import jakarta.servlet.ServletRequest;

public interface HttpServletRequest extends ServletRequest {
Part getPart(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package jakarta.servlet.http;

import java.io.IOException;

public interface Part {
String getSubmittedFileName();

void write(String fileName) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package javax.servlet;

public interface ServletRequest {
String getParameter(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package javax.servlet.http;

import javax.servlet.ServletRequest;

public interface HttpServletRequest extends ServletRequest {
Part getPart(String name);
}
Loading
Loading