ayushtkn commented on code in PR #536:
URL: https://github.com/apache/tez/pull/536#discussion_r4685767995
##########
tez-plugins/tez-aux-services/src/main/java/org/apache/tez/auxservices/ShuffleHandler.java:
##########
@@ -970,13 +979,19 @@ public void setPort(int port) {
this.port = port;
}
- private List<String> splitMaps(List<String> mapq) {
+ private List<String> splitMaps(List<String> mapq) throws IOException {
if (null == mapq) {
return null;
}
final List<String> ret = new ArrayList<>();
for (String s : mapq) {
- Collections.addAll(ret, s.split(","));
+ for (String mapId : s.split(",")) {
+ // Self-defending sink: reject values that could escape the output
dir.
+ if (mapId == null || !ATTEMPT_ID_PATTERN.matcher(mapId).matches()) {
+ throw new IOException("Invalid mapId: " + mapId);
Review Comment:
This isn't an I/O failure, so `IllegalArgumentException` fits better.
##########
tez-plugins/tez-aux-services/src/test/java/org/apache/tez/auxservices/TestShuffleHandler.java:
##########
@@ -1751,8 +1751,8 @@ public void testShuffleHandlerSendsDiskError() throws
Exception {
String shuffleBaseURL = "http://127.0.0.1:"
+
shuffleHandler.getConfig().get(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY);
URL url = URI.create(
- shuffleBaseURL +
"/mapOutput?job=job_12345_1&dag=1&reduce=1&map=attempt_12345_1_m_1_0").toURL();
- shuffleHandler.secretManager.addTokenForJob("job_12345_1",
+ shuffleBaseURL +
"/mapOutput?job=job_12345_0001&dag=1&reduce=1&map=attempt_12345_1_m_1_0").toURL();
+ shuffleHandler.secretManager.addTokenForJob("job_12345_0001",
Review Comment:
why?
##########
tez-plugins/tez-aux-services/src/test/java/org/apache/tez/auxservices/TestShuffleHandler.java:
##########
@@ -1810,6 +1810,226 @@ public FullHttpRequest createHttpRequest() {
return new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
uri);
}
+ /** Traversal-shaped dag/vertex/map params must be rejected. */
+ @Test
+ @Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
+ public void testTraversalInDagVertexMapIsRejected() throws Exception {
+ Configuration conf = getInitialConf();
+ conf.setInt(ShuffleHandler.MAX_SHUFFLE_CONNECTIONS, 3);
+ conf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION,
+ "simple");
+ UserGroupInformation.setConfiguration(conf);
+ conf.set(YarnConfiguration.NM_LOCAL_DIRS, TEST_DIR.getAbsolutePath());
+ ApplicationId appId = ApplicationId.newInstance(12345, 1);
+ String appAttemptId = "attempt_12345_1_m_1_0";
+ String user = "randomUser";
+ List<File> fileMap = new ArrayList<File>();
+ createShuffleHandlerFiles(TEST_DIR, user, appId.toString(), appAttemptId,
+ conf, fileMap);
+ ShuffleHandler shuffleHandler = new ShuffleHandler() {
+ private AuxiliaryLocalPathHandler pathHandler = new
TestAuxiliaryLocalPathHandler();
+ @Override
+ protected Shuffle getShuffle(Configuration conf) {
+ return new Shuffle(conf) {
+ @Override
+ protected void verifyRequest(String appid, ChannelHandlerContext ctx,
+ HttpRequest request, HttpResponse response, URL requestUri)
+ throws IOException {
+ // Reject before auth.
+ }
+ };
+ }
+ @Override
+ public AuxiliaryLocalPathHandler getAuxiliaryLocalPathHandler() {
+ return pathHandler;
+ }
+ };
+ shuffleHandler.init(conf);
+ try {
+ shuffleHandler.start();
+ DataOutputBuffer outputBuffer = new DataOutputBuffer();
+ outputBuffer.reset();
+ Token<JobTokenIdentifier> jt =
+ new Token<JobTokenIdentifier>("identifier".getBytes(),
+ "password".getBytes(), new Text(user), new
Text("shuffleService"));
+ jt.write(outputBuffer);
+ shuffleHandler
+ .initializeApplication(new ApplicationInitializationContext(user,
+ appId, ByteBuffer.wrap(outputBuffer.getData(), 0,
+ outputBuffer.getLength())));
+ String base = "http://127.0.0.1:"
+ +
shuffleHandler.getConfig().get(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY);
+
+ // Must survive every attempt below.
+ File outside = new File(TEST_DIR, "outside.txt");
+ try (FileOutputStream out = new FileOutputStream(outside)) {
+ out.write("keep me\n".getBytes());
+ }
+ assertTrue(outside.exists());
+
+ // Traversing dag: must 4xx, must not delete.
+ String badDag = URI.create("http:///a").resolve(
+ "?dagAction=delete&job=job_12345_0001&dag=1/../../..").getRawQuery();
+ HttpURLConnection conn = (HttpURLConnection) URI.create(
+ base + "/mapOutput?" + badDag).toURL().openConnection();
+ conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_NAME,
+ ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
+ conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_VERSION,
+ ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
+ conn.connect();
+ int code = conn.getResponseCode();
+ assertTrue(code >= 400 && code < 600,
+ "Expected an error response for traversing dag, got " + code);
Review Comment:
These range assertions would also pass on a `500` or a `file-not-found`
failure, so they don't show the validation is what rejects the request. I think
we should assert exactly `400` here.
The same applies at lines below as well
##########
tez-plugins/tez-aux-services/src/main/java/org/apache/tez/auxservices/ShuffleHandler.java:
##########
@@ -1223,6 +1282,13 @@ private boolean deleteTaskAttemptDirectories(Channel
channel, List<String> taskA
}
if (notEmptyAndContains(taskAttemptFailedQ,"delete") &&
!isNullOrEmpty(taskAttemptIdQ)) {
for (String taskAttemptId : taskAttemptIdQ) {
+ // taskAttemptId is used as a startsWith prefix over the DAG output
+ // directory listing; keep the accepted form strict.
+ if (taskAttemptId == null
+ || !ATTEMPT_ID_PATTERN.matcher(taskAttemptId).matches()) {
+ LOG.warn("Ignoring taskAttempt delete for invalid attempt id: {}",
taskAttemptId);
Review Comment:
`splitMaps` already rejects invalid ids, so this branch can't run. If it
ever did, it would skip the delete and still return `200`. Maybe we shold drop
this check, or fail the request with a `400`.
##########
tez-plugins/tez-aux-services/src/main/java/org/apache/tez/auxservices/ShuffleHandler.java:
##########
@@ -970,13 +979,19 @@ public void setPort(int port) {
this.port = port;
}
- private List<String> splitMaps(List<String> mapq) {
+ private List<String> splitMaps(List<String> mapq) throws IOException {
if (null == mapq) {
return null;
}
final List<String> ret = new ArrayList<>();
for (String s : mapq) {
- Collections.addAll(ret, s.split(","));
+ for (String mapId : s.split(",")) {
+ // Self-defending sink: reject values that could escape the output
dir.
+ if (mapId == null || !ATTEMPT_ID_PATTERN.matcher(mapId).matches()) {
Review Comment:
I think after s.split, mapId can't be ``null``, String Split I guess
guraneets non-null elements
##########
tez-plugins/tez-aux-services/src/test/java/org/apache/tez/auxservices/TestShuffleHandler.java:
##########
@@ -1810,6 +1810,226 @@ public FullHttpRequest createHttpRequest() {
return new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
uri);
}
+ /** Traversal-shaped dag/vertex/map params must be rejected. */
+ @Test
+ @Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
+ public void testTraversalInDagVertexMapIsRejected() throws Exception {
Review Comment:
this and `testDuplicateDagOrVertexParamIsRejected` share ~30 lines of
identical `ShuffleHandler` scaffolding (init, token, start). A shared helper
method would help reduce duplication
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]