Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NO-ISSUE] Prevent directory escape bypass through repeated URL decoding #4891

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.apache.zeppelin.scheduler.Job.Status.ABORT;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
Expand Down Expand Up @@ -239,7 +240,7 @@ String normalizeNotePath(String notePath) throws IOException {

notePath = notePath.replace("\r", " ").replace("\n", " ");

notePath = URLDecoder.decode(notePath, StandardCharsets.UTF_8.toString());
notePath = decodeRepeatedly(notePath);
if (notePath.endsWith("/")) {
throw new IOException("Note name shouldn't end with '/'");
}
Expand Down Expand Up @@ -1553,4 +1554,21 @@ private <T> boolean checkPermission(String noteId,
return false;
}
}

private static String decodeRepeatedly(final String encoded) throws IOException {
String previous = encoded;
int maxDecodeAttempts = 5;
int attempts = 0;

while (attempts < maxDecodeAttempts) {
String decoded = URLDecoder.decode(previous, StandardCharsets.UTF_8);
attempts++;
if (decoded.equals(previous)) {
return decoded;
}
previous = decoded;
}

throw new IOException("Exceeded maximum decode attempts. Possible malicious input.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,19 @@ void testNormalizeNotePath() throws IOException {
} catch (IOException e) {
assertEquals("Note name can not contain '..'", e.getMessage());
}
try {
// Double URL encoding of ".."
notebookService.normalizeNotePath("%252e%252e/%252e%252e/tmp/test333");
fail("Should fail");
} catch (IOException e) {
assertEquals("Note name can not contain '..'", e.getMessage());
}
try {
notebookService.normalizeNotePath("%252525252e%252525252e/tmp/test444");
fail("Should fail");
} catch (IOException e) {
assertEquals("Exceeded maximum decode attempts. Possible malicious input.", e.getMessage());
}
try {
notebookService.normalizeNotePath("./");
fail("Should fail");
Expand Down
Loading