Skip to content

Commit

Permalink
Merge pull request #585 from UCLALibrary/bugfix/auth-http-302-ids-wit…
Browse files Browse the repository at this point in the history
…h-slashes

Fix HTTP 302 redirects via delegate for identifiers with slashes
  • Loading branch information
glenrobson authored Nov 27, 2024
2 parents 0dccf29 + a9f1253 commit ccd668c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
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 @@ -597,9 +595,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

0 comments on commit ccd668c

Please sign in to comment.