Skip to content

Commit 2d301ff

Browse files
Adding CodeQL advanced config (microsoft#49)
* Update instructions to add more clarity * Initial import of exercise files * Initial devcontainer config for cmake * Updating container config * Completing exercises * Update main README * Create codeql.yml Adding advanced CodeQL scanning since autobuild fails for c++ projects * Update DemoController.java Fixing user-defined path vulnerability * Update DemoController.java Fix user-defined path * Update DemoResource.java Fix user-defined path vulnerability
1 parent b2c4e00 commit 2d301ff

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

.github/workflows/codeql.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: "CodeQL Advanced"
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
schedule:
9+
- cron: '26 17 * * 6'
10+
11+
jobs:
12+
analyze:
13+
name: Analyze (${{ matrix.language }})
14+
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
15+
permissions:
16+
security-events: write
17+
packages: read
18+
actions: read
19+
contents: read
20+
21+
strategy:
22+
fail-fast: false
23+
matrix:
24+
include:
25+
- language: c-cpp
26+
build-mode: manual
27+
- language: csharp
28+
build-mode: none
29+
- language: java-kotlin
30+
build-mode: none
31+
- language: javascript-typescript
32+
build-mode: none
33+
steps:
34+
- name: Checkout repository
35+
uses: actions/checkout@v4
36+
37+
- name: Initialize CodeQL
38+
uses: github/codeql-action/init@v3
39+
with:
40+
languages: ${{ matrix.language }}
41+
build-mode: ${{ matrix.build-mode }}
42+
43+
# this is necessary because autobuild does not work
44+
- if: matrix.language == 'c-cpp'
45+
shell: bash
46+
run: |
47+
cd exercisefiles/c++
48+
cmake -S . -B build
49+
cmake --build build
50+
cd ../../completesolution/c++
51+
cmake -S . -B build
52+
cmake --build build
53+
54+
- name: Perform CodeQL Analysis
55+
uses: github/codeql-action/analyze@v3
56+
with:
57+
category: "/language:${{matrix.language}}"

completesolution/quarkus/copilot-demo/src/main/java/com/microsoft/hackathon/quarkus/DemoResource.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ public Response parseurl(@QueryParam("url") String url) {
186186
@Produces(MediaType.APPLICATION_JSON)
187187
public Response listfiles(@QueryParam("path") String path) {
188188
Objects.requireNonNull(path, "path must not be null");
189+
if (path.contains("..") || path.contains("/") || path.contains("\\")) {
190+
throw new IllegalArgumentException("Invalid path");
191+
}
189192
ObjectMapper mapper = new ObjectMapper();
190193
try {
191194
List<JsonNode> fileList = new ArrayList<>();
@@ -213,6 +216,9 @@ public Response listfiles(@QueryParam("path") String path) {
213216
public Response countWord(@QueryParam("path") String path, @QueryParam("word") String word) {
214217
Objects.requireNonNull(path, "path must not be null");
215218
Objects.requireNonNull(word, "word must not be null");
219+
if (path.contains("..") || path.contains("/") || path.contains("\\")) {
220+
throw new IllegalArgumentException("Invalid path");
221+
}
216222
java.nio.file.Path filePath = Paths.get(path);
217223
String content;
218224
int count = 0;
@@ -241,6 +247,9 @@ public Response countWord(@QueryParam("path") String path, @QueryParam("word") S
241247
@Produces(MediaType.APPLICATION_OCTET_STREAM)
242248
public Response zipFolder(@QueryParam("path") String path) {
243249
Objects.requireNonNull(path, "path must not be null");
250+
if (path.contains("..") || path.contains("/") || path.contains("\\")) {
251+
throw new IllegalArgumentException("Invalid path");
252+
}
244253
java.nio.file.Path folderPath = Paths.get(path);
245254
File folder = folderPath.toFile();
246255
if (!folder.exists()) {

completesolution/springboot/copilot-demo/src/main/java/com/microsoft/hackathon/copilotdemo/controller/DemoController.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ public String parseurl(@RequestParam(name = "url", required = false) String url)
141141
@GetMapping(value = "/list-files", produces = MediaType.APPLICATION_JSON_VALUE)
142142
public ResponseEntity<String> listFiles(@RequestParam(name = "path") String pathString) {
143143
try {
144+
if (pathString.contains("..") || pathString.contains("/") || pathString.contains("\\")) {
145+
throw new IllegalArgumentException("Invalid pathString");
146+
}
144147
File path = new File(pathString);
145148
if (!path.exists()) {
146149
return ResponseEntity.notFound().build();
@@ -166,6 +169,9 @@ public ResponseEntity<String> listFiles(@RequestParam(name = "path") String path
166169
@GetMapping(value = "/count-word", produces = MediaType.APPLICATION_JSON_VALUE)
167170
public ResponseEntity<String> countWord(@RequestParam(name = "path") String pathString, @RequestParam(name = "word") String word) {
168171
try {
172+
if (pathString.contains("..") || pathString.contains("/") || pathString.contains("\\")) {
173+
throw new IllegalArgumentException("Invalid pathString");
174+
}
169175
File file = new File(pathString);
170176
if (!file.exists()) {
171177
return ResponseEntity.notFound().build();
@@ -193,6 +199,9 @@ public ResponseEntity<String> countWord(@RequestParam(name = "path") String path
193199
@GetMapping(value = "/zip-folder", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
194200
public ResponseEntity<Resource> zipFolder(@RequestParam(name = "path") String pathString) {
195201
try {
202+
if (pathString.contains("..") || pathString.contains("/") || pathString.contains("\\")) {
203+
throw new IllegalArgumentException("Invalid pathString");
204+
}
196205
File folder = new File(pathString);
197206
if (!folder.exists()) {
198207
return ResponseEntity.notFound().build();

0 commit comments

Comments
 (0)