use of com.zimbra.common.mime.HeaderUtils.ByteBuilder in project zm-mailbox by Zimbra.
the class FileUploadServletTest method testFileUploadAuthTokenCsrfEnabled.
@Test
public void testFileUploadAuthTokenCsrfEnabled() throws Exception {
URL url = new URL("http://localhost:7070/service/upload?lbfums=");
ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
addFormField(bb, "_charset_", "");
addFormField(bb, "filename1", filename1);
addFormFile(bb, filename1, "text/plain", content1);
endForm(bb);
byte[] form = bb.toByteArray();
HashMap<String, String> headers = new HashMap<String, String>();
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
XMLElement req = new XMLElement(AccountConstants.AUTH_REQUEST);
req.addAttribute(AccountConstants.A_CSRF_SUPPORT, "1");
com.zimbra.common.soap.Element a = req.addUniqueElement(AccountConstants.E_ACCOUNT);
a.addAttribute(AccountConstants.A_BY, "name");
a.setText(acct.getName());
req.addUniqueElement(AccountConstants.E_PASSWORD).setText("secret");
Map<String, Object> context = ServiceTestUtil.getRequestContext(acct);
MockHttpServletRequest authReq = (MockHttpServletRequest) context.get(SoapServlet.SERVLET_REQUEST);
authReq.setAttribute(Provisioning.A_zimbraCsrfTokenCheckEnabled, Boolean.TRUE);
Random nonceGen = new Random();
authReq.setAttribute(CsrfFilter.CSRF_SALT, nonceGen.nextInt() + 1);
Element response = new Auth().handle(req, context);
String authToken = response.getElement(AccountConstants.E_AUTH_TOKEN).getText();
String csrfToken = response.getElement("csrfToken").getText();
headers.put(Constants.CSRF_TOKEN, csrfToken);
MockHttpServletRequest mockreq = new MockHttpServletRequest(form, url, "multipart/form-data; boundary=" + boundary, 7070, "test", headers);
mockreq.setAttribute(CsrfFilter.CSRF_TOKEN_CHECK, Boolean.TRUE);
Cookie cookie = new Cookie("ZM_AUTH_TOKEN", authToken);
mockreq.setCookies(cookie);
MockHttpServletResponse resp = new MockHttpServletResponse();
servlet.doPost(mockreq, resp);
String respStrg = resp.output.toString();
System.out.println(respStrg);
assertTrue(respStrg.contains("200"));
}
use of com.zimbra.common.mime.HeaderUtils.ByteBuilder in project zm-mailbox by Zimbra.
the class FileUploadServletTest method testExtraFilenames.
@Test
public void testExtraFilenames() throws Exception {
ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
addFormField(bb, "_charset_", "");
addFormField(bb, "filename1", filename1 + "\r\nextra\r\ndata.txt");
addFormFile(bb, filename1, "text/plain", content1);
addFormField(bb, "filename2", filename2);
addFormFile(bb, filename2, "text/plain", content2);
addFormField(bb, "filename3", "bar.gif");
addFormFile(bb, "", null, null);
endForm(bb);
List<Upload> uploads = uploadForm(bb.toByteArray());
Assert.assertEquals(2, uploads == null ? 0 : uploads.size());
compareUploads(uploads.get(0), filename1, content1.getBytes(CharsetUtil.UTF_8));
compareUploads(uploads.get(1), filename2, content2.getBytes(CharsetUtil.UTF_8));
}
use of com.zimbra.common.mime.HeaderUtils.ByteBuilder in project zm-mailbox by Zimbra.
the class MimeCompoundHeader method reserialize.
@Override
protected void reserialize() {
if (content != null) {
return;
}
ByteBuilder line = new ByteBuilder();
line.append(name.getBytes()).append(':').append(' ');
this.valueStart = line.length();
if (primaryValue != null) {
line.append(primaryValue);
}
int position = line.length();
for (Map.Entry<String, String> param : params.entrySet()) {
String key = param.getKey(), value = param.getValue();
if (value == null || key == null || key.isEmpty()) {
continue;
}
line.append(';');
position++;
boolean quoted = false, nonascii = false;
for (int i = 0, max = value.length(); i < max; i++) {
char c = value.charAt(i);
if (c >= 0x7F || c < 0x20 && c != '\t') {
nonascii = true;
break;
} else if (TSPECIALS[c]) {
quoted = true;
}
}
String paramCharset = nonascii ? CharsetUtil.checkCharset(value, this.charset) : "us-ascii";
ByteBuilder bb = new ByteBuilder();
if (!nonascii) {
if (quoted || value.isEmpty()) {
bb.append(param.getKey()).append('=').append('"');
for (int i = 0, max = value.length(); i < max; i++) {
char c = value.charAt(i);
if (c == '"' || c == '\\') {
bb.append('\\');
}
bb.append(c);
}
bb.append('"');
} else {
bb.append(param.getKey()).append('=').append(value);
}
} else if (use2231Encoding) {
try {
String encoded = new URLEncoder().encode(value, paramCharset);
bb.append(param.getKey()).append('*').append('=').append(paramCharset).append('\'').append('\'').append(encoded);
} catch (UnsupportedEncodingException e) {
}
} else {
String encoded = null;
Charset mappedCharset = CharsetUtil.toCharset(paramCharset);
if (LC.mime_encode_compound_xwiniso2022jp_as_iso2022jp.booleanValue() && paramCharset.equalsIgnoreCase("ISO-2022-JP") && mappedCharset.name().equalsIgnoreCase("x-windows-iso2022jp")) {
//bug 82851
//usually paramCharset and CharsetUtil.toCharset(paramCharset).name() are equivalent
//however, if -Dsun.nio.cs.map is used then CharsetUtil.toCharset() returns a different charset
//we need to label the MIME with universally understood charset; even though we're mapping that
//internally to a variant that Java understands
encoded = EncodedWord.encode(value, paramCharset);
} else {
encoded = EncodedWord.encode(value, mappedCharset);
}
bb.append(param.getKey()).append('=').append('"').append(encoded).append('"');
}
if (position + bb.length() > LINE_WRAP_LENGTH) {
line.append('\r').append('\n').append(' ');
position = 1;
} else {
line.append(' ');
position++;
}
line.append(bb);
position += bb.length();
}
this.content = line.append('\r').append('\n').toByteArray();
}
use of com.zimbra.common.mime.HeaderUtils.ByteBuilder in project zm-mailbox by Zimbra.
the class FileUploadServletTest method testMissingFilenames.
@Test
public void testMissingFilenames() throws Exception {
ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
addFormField(bb, "_charset_", "");
addFormField(bb, "filename1", "");
addFormFile(bb, "x", "text/plain", content1);
addFormFile(bb, "y", "text/plain", content2);
addFormField(bb, "filename2", "");
addFormFile(bb, "", null, null);
addFormField(bb, "filename3", "");
addFormFile(bb, "", null, null);
endForm(bb);
List<Upload> uploads = uploadForm(bb.toByteArray());
Assert.assertEquals(2, uploads == null ? 0 : uploads.size());
compareUploads(uploads.get(0), "x", content1.getBytes(CharsetUtil.UTF_8));
compareUploads(uploads.get(1), "y", content2.getBytes(CharsetUtil.UTF_8));
}
use of com.zimbra.common.mime.HeaderUtils.ByteBuilder in project zm-mailbox by Zimbra.
the class FileUploadServletTest method testConsecutiveFilenames.
@Test
public void testConsecutiveFilenames() throws Exception {
ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
addFormField(bb, "_charset_", "");
addFormField(bb, "filename1", filename1 + "\r\n" + filename2);
addFormFile(bb, filename1, "text/plain", content1);
addFormFile(bb, filename2, "text/plain", content2);
addFormField(bb, "filename2", "");
addFormFile(bb, "", null, null);
addFormField(bb, "filename3", "");
addFormFile(bb, "", null, null);
endForm(bb);
List<Upload> uploads = uploadForm(bb.toByteArray());
Assert.assertEquals(2, uploads == null ? 0 : uploads.size());
compareUploads(uploads.get(0), filename1, content1.getBytes(CharsetUtil.UTF_8));
compareUploads(uploads.get(1), filename2, content2.getBytes(CharsetUtil.UTF_8));
}
Aggregations