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

Fix HTTP 302 redirects via delegate for identifiers with slashes #585

Merged
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 @@ -259,6 +259,34 @@ private void checkFrozen() {
}
}

/**
* <p>Translates the meta-identifier into a URI path component.</p>
*
* <p>Reverses {@link #fromURIPathComponent(String, DelegateProxy)}.</p>
*
* @param delegateProxy Delegate proxy.
*/
public String toURIPathComponent(DelegateProxy delegateProxy) {
// Encode just the identifier part.
final Identifier originalIdentifier = getIdentifier();
final String slashedIdentifier = originalIdentifier.toString();
final String deSlashedIdentifier = StringUtils.encodeSlashes(slashedIdentifier);
final String encodedIdentifier = Reference.encode(deSlashedIdentifier);
final MetaIdentifierTransformer xformer =
new MetaIdentifierTransformerFactory().newInstance(delegateProxy);
final String serializedMetaIdentifier;

setIdentifier(new Identifier(encodedIdentifier));
serializedMetaIdentifier = xformer.serialize(this);
// Now that we've serialized the encoded meta-identifier, put it back to how it was before
setIdentifier(originalIdentifier);

LOGGER.debug("[Slash-substituted identifier: {}] -> [de-slashed identifier: {}] -> " +
"[percent-encoded identifier: {}] -> [raw path component: {}]",
slashedIdentifier, deSlashedIdentifier, encodedIdentifier, serializedMetaIdentifier);
return serializedMetaIdentifier;
}

@Override
public String toString() {
return new StandardMetaIdentifierTransformer().serialize(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import edu.illinois.library.cantaloupe.image.Format;
import edu.illinois.library.cantaloupe.image.Identifier;
import edu.illinois.library.cantaloupe.image.MetaIdentifier;
import edu.illinois.library.cantaloupe.image.MetaIdentifierTransformer;
import edu.illinois.library.cantaloupe.image.MetaIdentifierTransformerFactory;
import edu.illinois.library.cantaloupe.delegate.DelegateProxy;
import edu.illinois.library.cantaloupe.delegate.DelegateProxyService;
import edu.illinois.library.cantaloupe.delegate.UnavailableException;
Expand Down Expand Up @@ -594,9 +592,8 @@ protected Reference getPublicReference(MetaIdentifier newMetaIdentifier) {
final int identifierIndex = pathComponents.indexOf(
getIdentifierPathComponent());

final MetaIdentifierTransformer xformer =
new MetaIdentifierTransformerFactory().newInstance(getDelegateProxy());
final String newMetaIdentifierString = xformer.serialize(newMetaIdentifier);
final String newMetaIdentifierString =
newMetaIdentifier.toURIPathComponent(getDelegateProxy());
publicRef.setPathComponent(identifierIndex, newMetaIdentifierString);
return publicRef;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ public static String decodeSlashes(final String uriPathComponent) {
return uriPathComponent;
}

/**
* Reverses {@link #decodeSlashes(String)}.
*
* @param slashedIdentifier Identifier with slashes to be substituted.
* @return Identifier with slashes substituted.
*/
public static String encodeSlashes(final String slashedIdentifier) {
final String substitute = Configuration.getInstance().
getString(Key.SLASH_SUBSTITUTE, "");
if (!substitute.isEmpty()) {
return org.apache.commons.lang3.StringUtils.replace(
slashedIdentifier, "/", substitute);
}
return slashedIdentifier;
}

public static String escapeHTML(String html) {
StringBuilder out = new StringBuilder(Math.max(16, html.length()));
for (int i = 0, length = html.length(); i < length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,29 @@ void testSetScaleConstraintWithFrozenInstance() {
() -> instance.setScaleConstraint(scaleConstraint));
}

/* toURIPathComponent() */

@Test
void testToURIPathComponent() {
final Configuration config = Configuration.getInstance();
config.setProperty(Key.SLASH_SUBSTITUTE, "BUG");
config.setProperty(Key.META_IDENTIFIER_TRANSFORMER,
StandardMetaIdentifierTransformer.class.getSimpleName());

DelegateProxy delegateProxy = TestUtil.newDelegateProxy();
MetaIdentifier metaIdentifier = MetaIdentifier.builder()
.withIdentifier("cats/:dogs")
.withPageNumber(2)
.withScaleConstraint(2, 3)
.build();
MetaIdentifier beforeMethodCall = new MetaIdentifier(metaIdentifier);
String actual = metaIdentifier.toURIPathComponent(delegateProxy);
String expected = "catsBUG%3Adogs;2;2:3";
assertEquals(expected, actual);
// Make sure the call to toURIPathComponent didn't change the meta-identifier.
assertEquals(beforeMethodCall, metaIdentifier);
}

/* toString() */

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ void testDecodeSlashes() {
assertEquals("ca/ts", StringUtils.decodeSlashes("ca$$ts"));
}

@Test
void testEncodeSlashes() {
Configuration.getInstance().setProperty(Key.SLASH_SUBSTITUTE, "$$");
assertEquals("cats", StringUtils.encodeSlashes("cats"));
assertEquals("ca$$ts", StringUtils.encodeSlashes("ca/ts"));
}

@Test
void testEscapeHTML() {
String html = "the quick brown <script type=\"text/javascript\">alert('hi');</script> fox";
Expand Down