pbowden-msft / MAUCacheAdmin

Microsoft AutoUpdate Cache Admin
70 stars 15 forks source link

Teams PKG alway reported as corrupt due to CDN results differences #38

Closed MrCoBalt closed 2 years ago

MrCoBalt commented 2 years ago

The "Teams_OSX.pkg" file is always reported as corrupt on our MAUCacheAdmin installations which causes it to redownload each run. I think I figured it out as due to how the GetDownloadSize function returns the online file size.

The Teams CDN download link (https://statics.teams.cdn.office.net/production-osx/1.5.00.11157/Teams_osx.pkg as of today) returns the following to the GetDownloadSize function's CONTENTHTTPLENGTH=$(curl --head -s $URL | grep -i 'Content-Length' | cut -d ' ' -f2) line:

110546899
x-ms-request-id,Server,x-ms-version,Content-Type,Last-Modified,ETag,Content-MD5,x-ms-lease-status,x-ms-lease-state,x-ms-blob-type,Accept-Ranges,Content-Length,Date,Transfer-Encoding

Note the second line which is coming from the access-control-expose-headers line being returned by the Teams CDN and subsequently being included in the CONTENTHTTPLENGTH result due to grep finding Content-Length twice.

If you run the exact same CONTENTHTTPLENGTH command against a standard Office CDN URL (let's say https://officecdnmac.microsoft.com/pr/C1297A47-86C4-4C1F-97FA-950631F94777/MacAutoupdate/Microsoft_Word_16.63.22071301_Updater.pkg) it returns only a single line with the file size, as expected.

What really trips up the GetDownloadSize function is the next part which strips out everything but numbers: CONTENTLENGTH=$(echo ${CONTENTHTTPLENGTH//[!0-9]/})

Normally this would work but because there's that extra line and it includes Content-MD5 within it, all of a sudden our Teams download size becomes 1105468995 and since that's obviously wrong when compared to the actual downloaded file size the pkg is flagged as corrupt.

The quickest fix (not sure about any potential knock-on effects though) appears to be changing the grep command within GetDownloadSize to look for Content-Length: —including the colon because it is present in the expected Content-Length: line of the headers (from both CDNs) but not the access-control-expose-headers line from the Teams CDN.

With the colon in there MAUCacheAdmin retrieves a single line of content-length from the Teams CDN and doesn't add the extra 5 to the reported download size, resulting in no more incorrectly-reported corruption.

pbowden-msft commented 2 years ago

@MrCoBalt thanks so much for the detailed error report and analysis! The world needs more people like you! Fixed in v3.5