-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[MINOR] Reject traversal segments in note and folder paths #5227
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
Open
jongyoul
wants to merge
3
commits into
apache:master
Choose a base branch
from
jongyoul:ZEPPELIN-fs-notebook-path
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
zeppelin-server/src/main/java/org/apache/zeppelin/notebook/repo/NotebookPathValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.zeppelin.notebook.repo; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URLDecoder; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Note-path validation helpers shared by {@link NotebookRepo} implementations | ||
| * and the service layer. A {@code final} class with {@code static} methods | ||
| * (rather than {@link NotebookRepo} default methods) prevents an | ||
| * implementation from accidentally — or intentionally — overriding the | ||
| * checks and bypassing them. | ||
| */ | ||
| public final class NotebookPathValidator { | ||
|
|
||
| private static final Pattern PATH_SEGMENT_SPLIT = Pattern.compile("/+"); | ||
| private static final String PARENT_SEGMENT = ".."; | ||
| private static final String CURRENT_SEGMENT = "."; | ||
| private static final int MAX_DECODE_LAYERS = 5; | ||
|
|
||
| private NotebookPathValidator() { | ||
| } | ||
|
|
||
| /** | ||
| * Refuses any {@code ..} or {@code .} segment in {@code notePath}. The | ||
| * input is URL-decoded repeatedly first so that variants such as | ||
| * {@code %2e%2e} or {@code %252e%252e} cannot bypass the check. | ||
| * | ||
| * @throws IOException if the path is null, contains a traversal segment, | ||
| * or has more URL-encoding layers than {@value #MAX_DECODE_LAYERS} | ||
| */ | ||
| public static void rejectTraversalSegments(String notePath) throws IOException { | ||
| if (notePath == null) { | ||
| throw new IOException("Path must not be null"); | ||
| } | ||
| String decoded = decodeRepeatedly(notePath); | ||
| String stripped = decoded.startsWith("/") ? decoded.substring(1) : decoded; | ||
| for (String segment : PATH_SEGMENT_SPLIT.split(stripped)) { | ||
| if (PARENT_SEGMENT.equals(segment) || CURRENT_SEGMENT.equals(segment)) { | ||
| throw new IOException("Path traversal segments are not allowed: " + notePath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Repeatedly URL-decodes {@code encoded} until it stabilises, capped at | ||
| * {@value #MAX_DECODE_LAYERS} layers. Detecting stability after {@code N} | ||
| * layers of encoding requires {@code N + 1} decode passes — the final pass | ||
| * confirms the result is unchanged — so the loop runs one extra iteration. | ||
| * | ||
| * @throws IOException if the input is malformed or has more URL-encoding | ||
| * layers than {@value #MAX_DECODE_LAYERS} | ||
| */ | ||
| public static String decodeRepeatedly(String encoded) throws IOException { | ||
| String previous = encoded; | ||
| for (int pass = 0; pass <= MAX_DECODE_LAYERS; pass++) { | ||
| String decoded; | ||
| try { | ||
| decoded = URLDecoder.decode(previous, StandardCharsets.UTF_8); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new IOException("Malformed URL-encoded path: " + encoded, e); | ||
| } | ||
| if (decoded.equals(previous)) { | ||
| return decoded; | ||
| } | ||
| previous = decoded; | ||
| } | ||
| throw new IOException("Exceeded maximum decode attempts. Possible malicious input."); | ||
|
Comment on lines
+62
to
+85
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Copilot Good catch on both. Fixed in dd53762:
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...erver/src/test/java/org/apache/zeppelin/notebook/repo/NotebookRepoPathValidationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.zeppelin.notebook.repo; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| class NotebookRepoPathValidationTest { | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { | ||
| "/../etc/passwd", | ||
| "/foo/../../etc/passwd", | ||
| "/foo/../bar", | ||
| "/foo/./bar", | ||
| "/./bar", | ||
| "/foo/..", | ||
| "/..", | ||
| "/.", | ||
| // URL-encoded variants must be rejected after decoding. | ||
| "/%2e%2e/etc/passwd", | ||
| "/foo/%2E%2E/bar", | ||
| "/%2e/foo", | ||
| // Double-encoded. | ||
| "/%252e%252e/etc/passwd", | ||
| }) | ||
| void rejectTraversalSegments_rejects_traversal(String malicious) { | ||
| assertThrows(IOException.class, | ||
| () -> NotebookPathValidator.rejectTraversalSegments(malicious), | ||
| "expected rejection for: " + malicious); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { | ||
| "/MyNote", | ||
| "/folder/MyNote", | ||
| "/folder/sub-folder/My Note With Spaces", | ||
| "/한글노트", | ||
| "/foo.bar.baz", | ||
| // ".." and "." are rejected only as exact segments — names containing | ||
| // dots remain valid. | ||
| "/...", | ||
| "/foo/...", | ||
| "/foo..bar", | ||
| }) | ||
| void rejectTraversalSegments_accepts_normal_paths(String safe) throws IOException { | ||
| NotebookPathValidator.rejectTraversalSegments(safe); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectTraversalSegments_rejects_null() { | ||
| assertThrows(IOException.class, () -> NotebookPathValidator.rejectTraversalSegments(null)); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectTraversalSegments_rejects_excessive_encoding_layers() { | ||
| // Six layers of "..", past the 5-layer decode cap. | ||
| String payload = "/%252525252e%252525252e/etc"; | ||
| assertThrows(IOException.class, () -> NotebookPathValidator.rejectTraversalSegments(payload)); | ||
| } | ||
|
|
||
| @Test | ||
| void decodeRepeatedly_returns_input_when_already_decoded() throws IOException { | ||
| assertEquals("/foo bar", NotebookPathValidator.decodeRepeatedly("/foo bar")); | ||
| } | ||
|
|
||
| @Test | ||
| void decodeRepeatedly_decodes_until_stable() throws IOException { | ||
| assertEquals("/..", NotebookPathValidator.decodeRepeatedly("/%252e%252e")); | ||
| } | ||
|
|
||
| @Test | ||
| void decodeRepeatedly_throws_on_too_many_layers() { | ||
| String payload = "/%2525252525252525252e"; | ||
| assertThrows(IOException.class, () -> NotebookPathValidator.decodeRepeatedly(payload)); | ||
| } | ||
|
|
||
| @Test | ||
| void decodeRepeatedly_wraps_malformed_percent_encoding_as_io_exception() { | ||
| // Trailing "%" without two hex digits makes URLDecoder.decode raise | ||
| // IllegalArgumentException; the validator must convert it to IOException | ||
| // so callers get a single, declared failure mode. | ||
| assertThrows(IOException.class, () -> NotebookPathValidator.decodeRepeatedly("/foo%")); | ||
| assertThrows(IOException.class, () -> NotebookPathValidator.decodeRepeatedly("/foo%ZZ")); | ||
| } | ||
|
|
||
| @Test | ||
| void decodeRepeatedly_accepts_max_decode_layers() throws IOException { | ||
| // Exactly five layers of "%2e" wrapping ("%252525252e") must decode | ||
| // cleanly; the constant means *layers*, not raw loop iterations. | ||
| assertEquals("/..", NotebookPathValidator.decodeRepeatedly("/%252525252e%252525252e")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Copilot You are right — the
mkDirline targets the wrong parent. However, that line was not introduced by this PR (the PR only added the tworejectTraversalSegmentscalls just above it; the buggytryMkDir(folderPath...)predates this change in master). Since this PR is a focused security fix for path traversal, I would prefer to keep its scope tight and address themove(folderPath, newFolderPath, ...)mkdir bug in a separate follow-up.Filed as ZEPPELIN-6415.