Skip to content

Commit

Permalink
strip quotes in storage emulator header boundary (#4825)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuangwang authored Aug 3, 2022
1 parent d446fff commit 2c92cc5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Replaces underlying terminal coloring library.
- Make storage emulator multipart parsing handle quotes in boundary header #3953
- Make storage emulator content type case insensitive #3953
4 changes: 3 additions & 1 deletion src/emulator/storage/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ function splitBufferByDelimiter(buffer: Buffer, delimiter: string, maxResults =
* @param body multipart request body as a Buffer
*/
function parseMultipartRequestBody(boundaryId: string, body: Buffer): MultipartRequestBody {
const boundaryString = `--${boundaryId}`;
// strip additional surrounding single and double quotes, cloud sdks have additional quote here
const cleanBoundaryId = boundaryId.replace(/^["'](.+(?=["']$))["']$/, "$1");
const boundaryString = `--${cleanBoundaryId}`;
const bodyParts = splitBufferByDelimiter(body, boundaryString).map((buf) => {
// Remove the \r\n and the beginning of each part left from the boundary line.
return Buffer.from(buf.slice(2));
Expand Down
22 changes: 22 additions & 0 deletions src/test/emulators/storage/multipart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ hello there!
);
});

it("parses an upload object multipart request with additional quotes in the boundary value", () => {
const contentTypeHeaderWithDoubleQuotes = `multipart/related; boundary="b1d5b2e3-1845-4338-9400-6ac07ce53c1e"`;

let { metadataRaw, dataRaw } = parseObjectUploadMultipartRequest(
contentTypeHeaderWithDoubleQuotes,
BODY
);

expect(metadataRaw).to.equal('{"contentType":"text/plain"}');
expect(dataRaw.toString()).to.equal("hello there!\n");

const contentTypeHeaderWithSingleQuotes = `multipart/related; boundary='b1d5b2e3-1845-4338-9400-6ac07ce53c1e'`;

({ metadataRaw, dataRaw } = parseObjectUploadMultipartRequest(
contentTypeHeaderWithSingleQuotes,
BODY
));

expect(metadataRaw).to.equal('{"contentType":"text/plain"}');
expect(dataRaw.toString()).to.equal("hello there!\n");
});

it("fails to parse when body has wrong number of parts", () => {
const invalidBody = Buffer.from(`--b1d5b2e3-1845-4338-9400-6ac07ce53c1e\r
Content-Type: application/json\r
Expand Down

0 comments on commit 2c92cc5

Please sign in to comment.