Search in sources :

Example 6 with ByteBuilder

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"));
}
Also used : Cookie(javax.servlet.http.Cookie) Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) XMLElement(com.zimbra.common.soap.Element.XMLElement) URL(java.net.URL) ByteBuilder(com.zimbra.common.mime.HeaderUtils.ByteBuilder) Random(java.util.Random) Auth(com.zimbra.cs.service.account.Auth) Element(com.zimbra.common.soap.Element) Test(org.junit.Test)

Example 7 with ByteBuilder

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));
}
Also used : ByteBuilder(com.zimbra.common.mime.HeaderUtils.ByteBuilder) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) Test(org.junit.Test)

Example 8 with ByteBuilder

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();
}
Also used : ByteBuilder(com.zimbra.common.mime.HeaderUtils.ByteBuilder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Charset(java.nio.charset.Charset) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 9 with ByteBuilder

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));
}
Also used : ByteBuilder(com.zimbra.common.mime.HeaderUtils.ByteBuilder) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) Test(org.junit.Test)

Example 10 with ByteBuilder

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));
}
Also used : ByteBuilder(com.zimbra.common.mime.HeaderUtils.ByteBuilder) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) Test(org.junit.Test)

Aggregations

ByteBuilder (com.zimbra.common.mime.HeaderUtils.ByteBuilder)10 Test (org.junit.Test)8 HashMap (java.util.HashMap)5 Element (com.zimbra.common.soap.Element)4 XMLElement (com.zimbra.common.soap.Element.XMLElement)4 Account (com.zimbra.cs.account.Account)4 Upload (com.zimbra.cs.service.FileUploadServlet.Upload)4 Auth (com.zimbra.cs.service.account.Auth)4 URL (java.net.URL)4 Cookie (javax.servlet.http.Cookie)4 Random (java.util.Random)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Charset (java.nio.charset.Charset)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1