OfficeDev / ews-java-api

A java client library to access Exchange web services. The API works against Office 365 Exchange Online as well as on premises Exchange.
MIT License
870 stars 560 forks source link

If email body contains U+FFFF, the API returns 500 Internal Server Error: #490

Open pathikrit opened 8 years ago

pathikrit commented 8 years ago

EwsRequest:

<Trace Tag="EwsRequest" Tid="8777" Time="2016-02-05 00:17:58Z">
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
        <soap:Header>
            <t:RequestServerVersion Version="Exchange2010_SP2"></t:RequestServerVersion>
        </soap:Header>
        <soap:Body>
            <m:CreateItem MessageDisposition="SendAndSaveCopy">
                <m:SavedItemFolderId>
                    <t:DistinguishedFolderId Id="sentitems"></t:DistinguishedFolderId>
                </m:SavedItemFolderId>
                <m:Items>
                    <t:Message>
                        <t:Subject>Test</t:Subject>
                        <t:Body BodyType="HTML">Hello World</t:Body>
                        <t:ToRecipients>
                            <t:Mailbox>
                                <t:EmailAddress>pbhowmick@coatue.com</t:EmailAddress>
                            </t:Mailbox>
                        </t:ToRecipients>
                    </t:Message>
                </m:Items>
            </m:CreateItem>
        </soap:Body>
    </soap:Envelope>
</Trace>

EwsResponse :

<Trace Tag="EwsResponse" Tid="8777" Time="2016-02-05 00:17:58Z">
    <?xml version="1.0" encoding="utf-8"?>
    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
        <s:Body>
            <s:Fault>
                <faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorSchemaValidation</faultcode>
                <faultstring xml:lang="en-US">The request failed schema validation: '&#xFFFF;', hexadecimal value 0xFFFF, is an invalid character. Line 1,
                    position 653.
                </faultstring>
                <detail>
                    <e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">ErrorSchemaValidation</e:ResponseCode>
                    <e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">The request failed schema validation.</e:Message>
                    <t:MessageXml xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
                        <t:LineNumber>1</t:LineNumber>
                        <t:LinePosition>653</t:LinePosition>
                        <t:Violation>'&#xFFFF;', hexadecimal value 0xFFFF, is an invalid character. Line 1, position 653.</t:Violation>
                    </t:MessageXml>
                </detail>
            </s:Fault>
        </s:Body>
    </s:Envelope>
</Trace>

This is what the Java API returns:

2016-02-05 00:17:58.669 UTC [error] application play-akka.actor.default-dispatcher-10 requestId=4
microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. The remote server returned an error: (500)Internal Server Error
 at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:74) ~[ews-java-api-2.0.jar:na]
 at microsoft.exchange.webservices.data.core.request.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:158) ~[ews-java-api-2.0.jar:na]
 at microsoft.exchange.webservices.data.core.ExchangeService.internalCreateItems(ExchangeService.java:598) ~[ews-java-api-2.0.jar:na]
 at microsoft.exchange.webservices.data.core.ExchangeService.createItem(ExchangeService.java:657) ~[ews-java-api-2.0.jar:na]
 at microsoft.exchange.webservices.data.core.service.item.Item.internalCreate(Item.java:245) ~[ews-java-api-2.0.jar:na]
 at microsoft.exchange.webservices.data.core.service.item.EmailMessage.internalSend(EmailMessage.java:147) ~[ews-java-api-2.0.jar:na]
 at microsoft.exchange.webservices.data.core.service.item.EmailMessage.sendAndSaveCopy(EmailMessage.java:300) ~[ews-java-api-2.0.jar:na]

Sanitization is handled by the .NET library and should also be handled by the Java library.

See MSDN docs: https://technet.microsoft.com/en-us/library/gg585107(v=exchg.80).aspx

pathikrit commented 8 years ago

Possibly related to #353 but I think in this case a ErrorSchemaValidation should be mapped to 400 error and not a 500 Internal Server Error. I think the bug is in this line: https://github.com/OfficeDev/ews-java-api/blob/01096703c3ea1b16b436735e82eb1ff47e54d60e/src/main/java/microsoft/exchange/webservices/data/core/request/ServiceRequestBase.java#L530