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

escape non-breaking space as \20, as required in RFC 7613, section 7.3 #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion jxmpp-core/src/main/java/org/jxmpp/util/XmppStringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.jxmpp.util.cache.LruCache;

import static java.lang.Character.isWhitespace;

/**
* Utility class for handling Strings in XMPP.
*/
Expand Down Expand Up @@ -230,7 +232,7 @@ public static String escapeLocalpart(String localpart) {
buf.append("\\5c");
break;
default: {
if (Character.isWhitespace(c)) {
if (isWhitespace(c) || isNonBreakingSpace(c)) {
buf.append("\\20");
} else {
buf.append(c);
Expand All @@ -243,6 +245,10 @@ public static String escapeLocalpart(String localpart) {
return res;
}

private static boolean isNonBreakingSpace(char chr) {
return chr == '\u00A0' || chr == '\u2007' || chr == '\u202F';
}

/**
* Un-escapes the localpart of a JID according to "JID Escaping" (XEP-0106).
* Escaping replaces characters prohibited by Nodeprep with escape sequences,
Expand Down
50 changes: 47 additions & 3 deletions jxmpp-core/src/test/java/org/jxmpp/util/XmppStringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package org.jxmpp.util;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.jxmpp.util.XmppStringUtils.parseDomain;

import org.junit.Test;

public class XmppStringUtilsTest {

@Test
Expand Down Expand Up @@ -69,4 +69,48 @@ public void parseResource() {
// Some more advanced parsing cases
assertEquals(error, "resOne@resTwo", XmppStringUtils.parseResource("[email protected]/resOne@resTwo"));
}

@Test
public void escapeLocalpart() {
String specialChars = " \"&'/:<>@\\_string";
assertEquals("\\20\\22\\26\\27\\2f\\3a\\3c\\3e\\40\\5c_string", XmppStringUtils.escapeLocalpart(specialChars));

// is null-safe
assertNull(XmppStringUtils.escapeLocalpart(null));
}

@Test
public void escapeLocalpart_whitespaces() {
// non ascii spaces, according to RFC 7613, Section 7.3, have to be mapped to '\20' as well
char[] whiteSpaces = {
'\u00A0', // NO-BREAK SPACE
'\u2007', // FIGURE SPACE
'\u202F', // NARROW NO-BREAK SPACE
'\t', // CHARACTER TABULATION
'\n', // LINE FEED (LF)
'\u000B', // LINE TABULATION
'\f', // FORM FEED (FF)
'\r', // CARRIAGE RETURN (CR)
'\u001C', // INFORMATION SEPARATOR FOUR
'\u001D', // INFORMATION SEPARATOR THREE
'\u001E', // INFORMATION SEPARATOR TWO
'\u001F' // INFORMATION SEPARATOR ONE
};
for (int i = 0; i < whiteSpaces.length; i++) {
char whiteSpace = whiteSpaces[i];
String whiteSpaceStr = new String(new char[]{whiteSpace});
String message = "character: index:" + i + ", name:" + Character.getName(whiteSpace);
assertEquals(message,"\\20", XmppStringUtils.escapeLocalpart(whiteSpaceStr));
}
}

@Test
public void unescapeLocalpart() {
String escapedString = "\\20\\22\\26\\27\\2f\\3a\\3c\\3e\\40\\5c";
String specialChars = " \"&'/:<>@\\";
assertEquals(specialChars, XmppStringUtils.unescapeLocalpart(escapedString));

// is null-safe
assertNull(XmppStringUtils.unescapeLocalpart(null));
}
}