4

I receive a "binary" transfer as base64 from AWS API GW to a Lambda. The payload is a MIME message that contains a application/soap+xml part first and a second part which is application/octet-stream.

The application/octet-stream is really a application/gzip according to the ebMS 3.0/AS4 specification (but that is not important here).

I need to "extract" the "binary" part and the inflate it into the XML message that it really is but I have failed to do this...

I've tried with looping through the message after "de-base64" encode it (Buffer.from(event.body, 'base64');) and "pick out" the binary part by splitting it on the boundary and removing the \r\n to get the "binary" data.

let gz = data.split('--' + boundary)[2];
gz = gz.slice(gz.indexOf('\r\n\r\n') + 4);
gz = gz.slice(-2) === '\r\n' ? gz.slice(0, -2) : gz;

This gives me a gz like:

án Øã¸\µ¶»(tTéAÁ`<½<͵EvS&ñQøV$#Ìç)ó$jm,ÚÚUÎqi"wìtxÜk?ÁllªÎþWÃË eà µYØ'Ùd| Ä × $¸doKÅôÞl#ç@ÁÑlòsÁ´¦ðõýñS¡DÈÂ(+!ÉÛ×üåùaÔ[ Å4jRb/áþc¥pN¶Íy $íÿÅ......

Trying zlib.gunzipSync() on this however does not give me any data, just an error:

{ Error: incorrect header check
    at Zlib.zlibOnError [as onerror] (zlib.js:162:17)
    at processChunkSync (zlib.js:405:12)
    at zlibBufferSync (zlib.js:148:12)
    at Object.syncBufferWrapper [as inflateSync] (zlib.js:736:14)
    at processTicksAndRejections (internal/process/task_queues.js:86:5) errno: -3, code: 'Z_DATA_ERROR' }

The file looks like:

------=_Part_1583_430387329.1575376371683
Content-Type: application/soap+xml; charset=utf-8

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
  <env:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" env:mustUnderstand="true">
      <xenc:EncryptedKey xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="EK-0067fbc8-507c-4a81-bb52-b0c5cc50dfbb">
        <xenc:EncryptionMethod Algorithm="http://www.w3.org/2009/xmlenc11#rsa-oaep">
          <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
          <xenc11:MGF Algorithm="http://www.w3.org/2009/xmlenc11#mgf1sha256" xmlns:xenc11="http://www.w3.org/2009/xmlenc11#"/>
        </xenc:EncryptionMethod>
        <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
          <wsse:SecurityTokenReference>
            <wsse:KeyIdentifier EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIdentifier">PYgjViXQc4kquKVpLqXCvGvz+6A=</wsse:KeyIdentifier>
          </wsse:SecurityTokenReference>
        </ds:KeyInfo>
        <xenc:CipherData>
          <xenc:CipherValue>gtlIaQZJvTQl8ySz+1TmjAAIvtbePCYoDIHxkf3P5yqdDle/Ju6tm70XVShk355fzC3r+GJKVkkqZN0SAXflMi6NDTNKeYQkcuVC02QeU9oRPLc2sofreoXeIENcm57GMs7EMs3PRFd8mLZpvU/RPAFxLlLoJsO3/MZi39erhbgwmwxxzU7QuZnRH7WDjLnjSZ+j6yn4WtO0LgqshBvxW1Td0GdNwDfW1aPQcz9aCaxvGoXDv2LsNvCcjgUdUsWAQ4WzOYGI/nTuxI2dJp4GcF29qfSsLLRo+aIKKX4JUDtr+Zo5UgK3qepp1LObVE51KLwBXFzkG4rHvqI7sS1yeA==</xenc:CipherValue>
        </xenc:CipherData>
        <xenc:ReferenceList>
          <xenc:DataReference URI="#ED-e2fe5f1d-083d-4e12-b7ab-388bf8b3ff35"/>
        </xenc:ReferenceList>
      </xenc:EncryptedKey>
      <xenc:EncryptedData Id="ED-e2fe5f1d-083d-4e12-b7ab-388bf8b3ff35" MimeType="application/octet-stream" Type="http://docs.oasis-open.org/wss/oasis-wss-SwAProfile-1.1#Attachment-Content-Only" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
        <xenc:EncryptionMethod Algorithm="http://www.w3.org/2009/xmlenc11#aes128-gcm"/>
        <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
          <wsse:SecurityTokenReference xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd" wsse11:TokenType="http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#EncryptedKey">
            <wsse:Reference URI="#EK-0067fbc8-507c-4a81-bb52-b0c5cc50dfbb"/>
          </wsse:SecurityTokenReference>
        </ds:KeyInfo>
        <xenc:CipherData>
          <xenc:CipherReference URI="cid:a076a561-f32f-4e34-b62e-7ebb7693aa5f@ip-10-10-187-118.eu-west-1.compute.internal">
            <xenc:Transforms>
              <ds:Transform Algorithm="http://docs.oasis-open.org/wss/oasis-wss-SwAProfile-1.1#Attachment-Ciphertext-Transform" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
            </xenc:Transforms>
          </xenc:CipherReference>
        </xenc:CipherData>
      </xenc:EncryptedData>
      <wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="X509-267eb38a-7380-472e-a046-d4b1772bf54a">MIIFwjCCA6qgAwIBAgIQcSKY1n8PhBuMTIor6U79wTANBgkqhkiG9w0BAQsFADBrMQswCQYDVQQGEwJCRTEZMBcGA1UEChMQT3BlblBFUFBPTCBBSVNCTDEWMBQGA1UECxMNRk9SIFRFU1QgT05MWTEpMCcGA1UEAxMgUEVQUE9MIEFDQ0VTUyBQT0lOVCBURVNUIENBIC0gRzIwHhcNMTgwODE2MDAwMDAwWhcNMjAwODA1MjM1OTU5WjBPMRIwEAYDVQQDDAlQT1AwMDAwNTUxFzAVBgNVBAsMDlBFUF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5ucAUrswG+3WrWC1mCPWMH9+b2mCkgJ4kE2FEUC4mpdh6ccVCduqRwnvdPS3+Pci5TgVwA/k4hI3LXRErReHQsu9PF3ZDqrBlgVSegcG2xmhvvdQNea8MIF8NAT5QKAlyGOjyBaimnhwagsDC3TwgYmd+rmQORf+bHM95RebZ3RZHMP1yawYNZXPVpoJIaFvfjx+ghsJeGL5V03/sgKFOPvdnzdC+9v3+1Q41MNc8BNXStUKRYnv4cm4LritZtl4ZHywQnqigQiIy8S0pXzKwq48eZIOFM6/JTknTLi3vBNGlezqsbiKwi3V0MXQhHUB5eCAXWt9SLozQobwSExCyNiToU+dGOe5Jh165J5nn83e2k2zWItFr+pk+sJgtVYCgKj/AEmR5MDED8TZMXZrgW30Xcr0UNvxz3YneX74PF1EakhaeO+S8yNGoT3M/dZVFijmmNoW9OvVzJD4vu4rvEOd4r2yojTJsdz4Be1RfqqmdJKsW3Turd9mkU28SEwDxS3mjp9Eou0BNnCDllkcHEL/AgzviFvx1vk1B89G8r6ykp7Zk6hml0yIZ2ooHX13xSmkadNHuMRxRgjX3wP6p8dBVVo=</wsse:BinarySecurityToken>
      <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="SIG-ca3e5ab2-157c-44aa-9bbb-222dd5cb4a40">
        <ds:SignedInfo>
          <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
            <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="env"/>
          </ds:CanonicalizationMethod>
          <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
          <ds:Reference URI="#id-e9cbbca8-ac05-4d38-a25c-69a5834cf78b">
            <ds:Transforms>
              <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
            <ds:DigestValue>m9VFYkY2Wn5Lel2V06yHbfnGh6ZIRKIVVaJuG06HCss=</ds:DigestValue>
          </ds:Reference>
          <ds:Reference URI="#id-e561e160-f956-449e-83c7-42f4ee03ba07">
            <ds:Transforms>
              <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
            <ds:DigestValue>M5VNRySoFO3LXQl3KS1aXMJCy1R/B/lIrPS8QEgQW4Q=</ds:DigestValue>
          </ds:Reference>
          <ds:Reference URI="cid:a076a561-f32f-4e34-b62e-7ebb7693aa5f@ip-10-10-187-118.eu-west-1.compute.internal">
            <ds:Transforms>
              <ds:Transform Algorithm="http://docs.oasis-open.org/wss/oasis-wss-SwAProfile-1.1#Attachment-Content-Signature-Transform"/>
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
            <ds:DigestValue>0hcnk8sdtag0nQmeqaAmXQVRImI2S+mQAXVzZ6FykXs=</ds:DigestValue>
          </ds:Reference>
        </ds:SignedInfo>
        <ds:SignatureValue>deq9NXzSV5Es3s6ZcSHwUKLVXYjzOaP+/471tKytDs0HmreAaxNfqNDyJPFE79B2k2yrP0c9gQXSUFY4nwThvoo18dwA1Gh64VkvEmsgx3Xoy1iHufgAkwu5HGFAzz/zvPu+MkEaatw9h/nqHVdmVoPbME8gvaxQzxfNscF5nZNDjLuc/Xcf12pSWc+pW65MunEMjT8oOx06jzfN0dJ3XVmdtlv+G+MebHQBSyCXxtl/UyJDj4grfcTPUQ7wTgQhS8Bo8QNOs9jWQ/fdM/vdai1Gx0Kg4TVMrzAQ2/JyI5T0rL6jWnOqzpTqePA+TfRvuy5vYZsFzrmLXKqxbzEkNw==</ds:SignatureValue>
        <ds:KeyInfo Id="KI-4a3efdb9-ba0f-4924-927d-1bb31f25a390">
          <wsse:SecurityTokenReference wsu:Id="STR-41f9074c-f671-49cf-9f09-1a45f036ad81">
            <wsse:Reference URI="#X509-267eb38a-7380-472e-a046-d4b1772bf54a" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/>
          </wsse:SecurityTokenReference>
        </ds:KeyInfo>
      </ds:Signature>
    </wsse:Security>
    <eb:Messaging xmlns:eb="http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/ns/core/200704/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" env:mustUnderstand="true" wsu:Id="id-e561e160-f956-449e-83c7-42f4ee03ba07">
      <eb:UserMessage xmlns:ns3="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" xmlns:ns5="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns6="http://www.w3.org/2000/09/xmldsig#" xmlns:ns7="http://docs.oasis-open.org/ebxml-bp/ebbp-signals-2.0" xmlns:ns8="http://www.w3.org/1999/xlink">
        <eb:MessageInfo>
          <eb:Timestamp>2019-12-03T12:32:51.659Z</eb:Timestamp>
          <eb:MessageId>24715fbe-748d-43c6-8d26-72aa742f8542@ip-10-10-187-118.eu-west-1.compute.internal</eb:MessageId>
        </eb:MessageInfo>
        <eb:PartyInfo>
          <eb:From>
            <eb:PartyId type="urn:fdc:peppol.eu:2017:identifiers:ap">POP000055</eb:PartyId>
            <eb:Role>http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/ns/core/200704/initiator</eb:Role>
          </eb:From>
          <eb:To>
            <eb:PartyId type="urn:fdc:peppol.eu:2017:identifiers:ap">PSE000094</eb:PartyId>
            <eb:Role>http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/ns/core/200704/responder</eb:Role>
          </eb:To>
        </eb:PartyInfo>
        <eb:CollaborationInfo>
          <eb:AgreementRef>urn:fdc:peppol.eu:2017:agreements:tia:ap_provider</eb:AgreementRef>
          <eb:Service type="cenbii-procid-ubl">urn:www.cenbii.eu:profile:bii05:ver2.0</eb:Service>
          <eb:Action>busdox-docid-qns::urn:oasis:names:specification:ubl:schema:xsd:Invoice-2::Invoice##urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol5a:ver2.0::2.1</eb:Action>
          <eb:ConversationId>3c91d084-ace3-4a7a-9da5-6c68e2d67523@ip-10-10-187-118.eu-west-1.compute.internal</eb:ConversationId>
        </eb:CollaborationInfo>
        <eb:MessageProperties>
          <eb:Property name="originalSender">iso6523-actorid-upis::0088:tbcntrl00002</eb:Property>
          <eb:Property name="finalRecipient">iso6523-actorid-upis::0007:5567321707</eb:Property>
        </eb:MessageProperties>
        <eb:PayloadInfo>
          <eb:PartInfo href="cid:a076a561-f32f-4e34-b62e-7ebb7693aa5f@ip-10-10-187-118.eu-west-1.compute.internal">
            <eb:PartProperties>
              <eb:Property name="CompressionType">application/gzip</eb:Property>
              <eb:Property name="MimeType">application/xml</eb:Property>
            </eb:PartProperties>
          </eb:PartInfo>
        </eb:PayloadInfo>
      </eb:UserMessage>
    </eb:Messaging>
  </env:Header>
  <env:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-e9cbbca8-ac05-4d38-a25c-69a5834cf78b"/>
</env:Envelope>


------=_Part_1583_430387329.1575376371683
Content-Type: application/octet-stream
Content-ID: <a076a561-f32f-4e34-b62e-7ebb7693aa5f@ip-10-10-187-118.eu-west-1.compute.internal>
Content-Transfer-Encoding: binary

án Øã¸\µ¶»(tTéAÁ`<½<͵EvS&ñQøV$#Ìç)ó$jm,ÚÚUÎqi"wìtxÜk?ÁllªÎþWÃË eà       µYØ'Ùd| Ä
× $¸doKÅôÞl#ç@ÁÑlòsÁ´¦ðõýñS¡DÈÂ(+!ÉÛ×üåùaÔ[ Å4jRb/áþc¥pN¶Íy $íÿÅ
-ë9µ%ÎÓ# ²c¾ûèWÄÅuËr}ÚÐóUú8^åHccøy¥¸Ùû×õS«1©?YïPait¡ÇÙRsì-Ý_f÷ßOôü¸HF^Û7ih0ߥRÏ26¼fh¦þ9d;"T}HZyfËI[ÕQ8Ñ߯ZBî|þµêAò¼"uæBÓs*ÙTU¹¬fÙUEcùicÊ»4I20­:¼Z®^D#l º&ÿÕ_Í4¡o}Æn¦®´¿Ü¥½ÎhÉjøhDø%?]PK2÷<dG;åÄåÔ-ñUĵóZáÅh°²K·µjìªD×.Ù1ØÃlXÁçÕ(¾ç×ç°Úÿ/ûHv "Ï8ÖËæc[rDÜ¥¥÷ßÄ    í¸Êu¢eKôönÜ];a´÷þf« -¬õB7é]î«Þz[gx¹úÙ³øÞÉtñjÂøãõ½JÀ;ñyP @$ÏqñøÀPjÌ
gµ ètC?³ojQAmýA]¹sH¬MkN²C«qÿBÛø×ÿâüõ«"!×xeÉ¿A.4}ëTùã_ó&á¢JÑ-ò¡BÃþ9Å¿5³UM(i  L×ËØ·O}^°P%p&}ïsE:ÊjÅ/lÈpmY:&dSºá¢u3B)t|>¥UíêJ£Ù¨°ßæP×m5_aF#åW      v^'æPUÀ äbèÿ%ÒA¤d½/u0i±â×uë>rü4E,'ØT´­ÖÒ{<¯Of8LSão®ä+g(¥Ôô·x2&8§Ãs7Ö&Ëæ

ÑRðþ¸>í<¨MÆæÝÇê¹ÒêdH1@n0þ\~=W`/1{µR#ôì@aÝö},yôrffô`*Õ$¹½l,¸Å;&®8h
y="¿Rò°&ä¡WniEkÅK»/® iFtLi:J*_ø9pBEÁ,bdyÄY¿4&£4&Ú¶kk<de×Mê:jÃäIu2ú}[ÖβE-ÎÄÇ!UæLa;þE?×rOünÝË|w¸;%±Õõ·ãÖÁÊc¯±ö÷ÛÇÆ!a¼¿Sxô2£±ÛõUilj93ÅæJ¡m]WÙO~èdFj3¥¶!^óZ¦YÒØ #§ 5-Ĥ  ÁL¢¬ ¹NãÅZ½¢§'d¶Tb-Â{êxÿt¶Q¼#.Ö}~ÙçßEGåÀÇã3q~³ü:rnênÊèÔ]¾¸`Ç­n©B]¦¬­ë{Kôã|Dý¦®HJåÞB×®þA¹ËÎ¥¥1        aCáî<h/YOk-X½ â¸"!¿iÑyØÛlGÄ0Þï``îÉýïoª¬ÖM[ò|öø+K_Óå Uwָʪ]|`ƽÛDµÖRøÑ+7ì°ÓdXÐïl`C?æ·Ìø\©¹úE\°ê4?Ø=TÑáyrËËå»#]¶ÕÂc(
------=_Part_1583_430387329.1575376371683--
Anders
  • 3,198
  • 1
  • 20
  • 43
  • Adding a header to the base64 data might do something here? For example `data:image/jpeg;base64[rest of the raw data here]` – Kevin Hernandez Dec 05 '19 at 16:46
  • I'm not sure where you mean to add that... The data sample posted above is what it looks after doing the base64 decode. The part I need to get is the `application/octet-stream` data at the end. – Anders Dec 05 '19 at 16:56
  • sorry mate. Try referencing [this answer here](https://stackoverflow.com/a/22310760/5875610) regarding the error you are getting – Kevin Hernandez Dec 05 '19 at 17:01
  • Ah, yeah, thanks! However I think my issue is with how I extract the "gzip" binary part that somehow messes it up... – Anders Dec 05 '19 at 18:33

0 Answers0