Skip to content

Commit bbb473f

Browse files
committed
Limit Characters Allowed In Ids
Opencast allows almost arbitrary identifiers for media packages and elements to be used. This can be problematic for operation and security since such identifiers are sometimes used for file system operations which may lead to attacker being able to escape working directories and write files to other locations. In addition, Opencast's `Id.toString(…)` vs `Id.compact(…)` behavior, the latter trying to mitigate some of the file system problems, can cause errors due to identifier mismatch since an identifier may unintentionally change. This patch limits the characters allowed to be used in identifiers to ensure no unsafe operations are possible and no mismatch may happen.
1 parent 74bfb70 commit bbb473f

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

modules/common/src/main/java/org/opencastproject/mediapackage/identifier/Id.java

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public interface Id {
4141
*
4242
* @return a path separator-free representation of the identifier
4343
*/
44+
@Deprecated
4445
String compact();
4546

4647
class Adapter extends XmlAdapter<IdImpl, Id> {

modules/common/src/main/java/org/opencastproject/mediapackage/identifier/IdImpl.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
package org.opencastproject.mediapackage.identifier;
2424

25+
import java.util.regex.Pattern;
26+
2527
import javax.xml.bind.annotation.XmlAccessType;
2628
import javax.xml.bind.annotation.XmlAccessorType;
2729
import javax.xml.bind.annotation.XmlType;
@@ -34,6 +36,8 @@
3436
@XmlAccessorType(XmlAccessType.NONE)
3537
public class IdImpl implements Id {
3638

39+
private static final Pattern pattern = Pattern.compile("[\\w-_.:;()]+");
40+
3741
/** The identifier */
3842
@XmlValue
3943
protected String id = null;
@@ -50,7 +54,10 @@ public IdImpl() {
5054
* @param id
5155
* the identifier
5256
*/
53-
public IdImpl(String id) {
57+
public IdImpl(final String id) {
58+
if (!pattern.matcher(id).matches()) {
59+
throw new IllegalArgumentException("Id must match " + pattern);
60+
}
5461
this.id = id;
5562
}
5663

@@ -60,7 +67,7 @@ public IdImpl(String id) {
6067
* @see org.opencastproject.mediapackage.identifier.Id#compact()
6168
*/
6269
public String compact() {
63-
return id.replaceAll("/", "-").replaceAll("\\\\", "-");
70+
return toString();
6471
}
6572

6673
@Override

modules/ingest-service-impl/src/main/java/org/opencastproject/ingest/endpoint/IngestRestService.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -837,11 +837,14 @@ public Response addMediaPackage(@Context HttpServletRequest request, @PathParam(
837837
return Response.serverError().status(Status.BAD_REQUEST).build();
838838
}
839839

840-
WorkflowInstance workflow = (wdID == null) ? ingestService.ingest(mp) : ingestService.ingest(mp, wdID,
841-
workflowProperties);
840+
WorkflowInstance workflow = (wdID == null)
841+
? ingestService.ingest(mp)
842+
: ingestService.ingest(mp, wdID, workflowProperties);
842843
return Response.ok(workflow).build();
843844
}
844845
return Response.serverError().status(Status.BAD_REQUEST).build();
846+
} catch (IllegalArgumentException e) {
847+
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
845848
} catch (Exception e) {
846849
logger.warn(e.getMessage(), e);
847850
return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();

0 commit comments

Comments
 (0)