From 4e4b3797519b038a303ad410c06311055553d197 Mon Sep 17 00:00:00 2001 From: "Michael B. Klein" Date: Sat, 9 Nov 2024 00:04:09 +0000 Subject: [PATCH] Fix case sensitivity in headers --- dependencies/package.json | 2 +- examples/cloudformation/custom_hostname.yml | 2 +- examples/sam/customization/template.yaml | 2 +- extras/terraform/main.tf | 2 +- package.json | 2 +- sam/cloudfront/template.yml | 4 ++-- sam/standalone/template.yml | 4 ++-- sam/template.yml | 2 +- src/helpers.js | 13 ++++++++++--- src/package.json | 2 +- src/resolvers.js | 5 +++-- tests/resolvers.test.js | 7 +++++++ 12 files changed, 31 insertions(+), 16 deletions(-) diff --git a/dependencies/package.json b/dependencies/package.json index e42a8be..aacaa79 100644 --- a/dependencies/package.json +++ b/dependencies/package.json @@ -1,6 +1,6 @@ { "name": "serverless-iiif-dependencies", - "version": "5.1.0", + "version": "5.1.1", "description": "Dependencies for serverless IIIF", "author": "Michael B. Klein", "license": "Apache-2.0", diff --git a/examples/cloudformation/custom_hostname.yml b/examples/cloudformation/custom_hostname.yml index 9fb532d..ba43b2f 100644 --- a/examples/cloudformation/custom_hostname.yml +++ b/examples/cloudformation/custom_hostname.yml @@ -21,7 +21,7 @@ Resources: Properties: Location: ApplicationId: arn:aws:serverlessrepo:us-east-1:625046682746:applications/serverless-iiif - SemanticVersion: 5.1.0 + SemanticVersion: 5.1.1 Parameters: ForceHost: !Sub '${CacheHostName}.${CacheDomainName}' SourceBucket: !Ref IiifSourceBucket diff --git a/examples/sam/customization/template.yaml b/examples/sam/customization/template.yaml index 29f2fcc..12b8a9f 100644 --- a/examples/sam/customization/template.yaml +++ b/examples/sam/customization/template.yaml @@ -126,7 +126,7 @@ Resources: Properties: Location: ApplicationId: arn:aws:serverlessrepo:us-east-1:625046682746:applications/serverless-iiif - SemanticVersion: 5.1.0 + SemanticVersion: 5.1.1 Parameters: CorsAllowOrigin: REFLECT_ORIGIN ForceHost: !Ref CacheDomainName diff --git a/extras/terraform/main.tf b/extras/terraform/main.tf index 3d9e608..199c96d 100644 --- a/extras/terraform/main.tf +++ b/extras/terraform/main.tf @@ -10,7 +10,7 @@ terraform { locals { serverless_iiif_app_id = "arn:aws:serverlessrepo:us-east-1:625046682746:applications/serverless-iiif" - serverless_iiif_app_version = "5.1.0" + serverless_iiif_app_version = "5.1.1" } resource "aws_serverlessapplicationrepository_cloudformation_stack" "serverless_iiif" { diff --git a/package.json b/package.json index f1454c2..08671a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-iiif", - "version": "5.1.0", + "version": "5.1.1", "description": "Lambda wrapper for iiif-processor", "author": "Michael B. Klein", "license": "Apache-2.0", diff --git a/sam/cloudfront/template.yml b/sam/cloudfront/template.yml index ce6e85e..35a945a 100644 --- a/sam/cloudfront/template.yml +++ b/sam/cloudfront/template.yml @@ -9,7 +9,7 @@ Metadata: ReadmeUrl: ./README.md Labels: ["iiif", "image-processing"] HomePageUrl: https://samvera.github.io/serverless-iiif - SemanticVersion: 5.1.0 + SemanticVersion: 5.1.1 SourceCodeUrl: https://github.com/samvera/serverless-iiif AWS::CloudFormation::Interface: ParameterGroups: @@ -324,7 +324,7 @@ Resources: Properties: Location: ApplicationId: arn:aws:serverlessrepo:us-east-1:625046682746:applications/serverless-iiif - SemanticVersion: 5.1.0 + SemanticVersion: 5.1.1 Parameters: CorsAllowCredentials: !Ref CorsAllowCredentials CorsAllowOrigin: !Ref CorsAllowOrigin diff --git a/sam/standalone/template.yml b/sam/standalone/template.yml index 3a6cd98..813ebc4 100644 --- a/sam/standalone/template.yml +++ b/sam/standalone/template.yml @@ -9,7 +9,7 @@ Metadata: ReadmeUrl: ./README.md Labels: ["iiif", "image-processing"] HomePageUrl: https://samvera.github.io/serverless-iiif - SemanticVersion: 5.1.0 + SemanticVersion: 5.1.1 SourceCodeUrl: https://github.com/samvera/serverless-iiif AWS::CloudFormation::Interface: ParameterGroups: @@ -118,7 +118,7 @@ Resources: Properties: Location: ApplicationId: arn:aws:serverlessrepo:us-east-1:625046682746:applications/serverless-iiif - SemanticVersion: 5.1.0 + SemanticVersion: 5.1.1 Parameters: CorsAllowCredentials: !Ref CorsAllowCredentials CorsAllowHeaders: !Ref CorsAllowHeaders diff --git a/sam/template.yml b/sam/template.yml index 73942ca..dee030d 100644 --- a/sam/template.yml +++ b/sam/template.yml @@ -9,7 +9,7 @@ Metadata: ReadmeUrl: ../README.md Labels: ["iiif", "image-processing"] HomePageUrl: https://samvera.github.io/serverless-iiif - SemanticVersion: 5.1.0 + SemanticVersion: 5.1.1 SourceCodeUrl: https://github.com/samvera/serverless-iiif AWS::CloudFormation::Interface: ParameterGroups: diff --git a/src/helpers.js b/src/helpers.js index b62811f..426f831 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -13,7 +13,7 @@ const corsSetting = (name) => { const allowOriginValue = (corsAllowOrigin, event) => { if (corsAllowOrigin === 'REFLECT_ORIGIN') { - return event.headers.origin || '*'; + return getHeaderValue(event, 'origin') || '*'; } return corsAllowOrigin; }; @@ -38,9 +38,15 @@ const fileMissing = (event) => { return !/\.(jpe?g|tiff?|jp2|gif|png|webp|json)$/.test(eventPath(event)); }; +const getHeaderValue = (event, header) => { + const headerName = Object.keys(event.headers).find((h) => h.toLowerCase() === header); + if (headerName) return event.headers[headerName]; + return null; +}; + const getUri = (event) => { - const scheme = event.headers['x-forwarded-proto'] || 'http'; - const host = process.env.forceHost || event.headers['x-forwarded-host'] || event.headers.host; + const scheme = getHeaderValue(event, 'x-forwarded-proto') || 'http'; + const host = process.env.forceHost || getHeaderValue(event, 'x-forwarded-host') || getHeaderValue(event, 'host'); const uri = `${scheme}://${host}${eventPath(event)}`; return uri; }; @@ -68,6 +74,7 @@ module.exports = { addCorsHeaders, eventPath, fileMissing, + getHeaderValue, getUri, isBase64, isTooLarge, diff --git a/src/package.json b/src/package.json index b2a6eed..e86399a 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "serverless-iiif", - "version": "5.1.0", + "version": "5.1.1", "description": "Lambda wrapper for iiif-processor", "author": "Michael B. Klein", "license": "Apache-2.0", diff --git a/src/resolvers.js b/src/resolvers.js index 940ff39..ab1e0b1 100644 --- a/src/resolvers.js +++ b/src/resolvers.js @@ -1,4 +1,5 @@ const { S3Client, GetObjectCommand, HeadObjectCommand } = require('@aws-sdk/client-s3'); +const { getHeaderValue } = require('./helpers'); const URI = require('uri-js'); const util = require('util'); @@ -71,7 +72,7 @@ const dimensionRetriever = async (location) => { // Preflight resolvers const parseLocationHeader = (event) => { - const locationHeader = event.headers['x-preflight-location']; + const locationHeader = getHeaderValue(event, 'x-preflight-location'); if (locationHeader && locationHeader.match(/^s3:\/\//)) { const parsedURI = URI.parse(locationHeader); return { Bucket: parsedURI.host, Key: parsedURI.path.slice(1) }; @@ -80,7 +81,7 @@ const parseLocationHeader = (event) => { }; const parseDimensionsHeader = (event) => { - const dimensionsHeader = event.headers['x-preflight-dimensions']; + const dimensionsHeader = getHeaderValue(event, 'x-preflight-dimensions'); if (!dimensionsHeader) return null; const result = JSON.parse(dimensionsHeader); diff --git a/tests/resolvers.test.js b/tests/resolvers.test.js index 758f3ef..d13463b 100644 --- a/tests/resolvers.test.js +++ b/tests/resolvers.test.js @@ -100,6 +100,13 @@ describe('resolvers', () => { // eslint-disable-line max-lines-per-function }); describe('dimensionResolver', () => { + it('preflight dimensions (case insensitive)', async () => { + const { dimensionResolver } = resolvers.resolverFactory({ headers: { 'X-Preflight-Dimensions': '{ "width": 640, "height": 480 }' } }, true); + const expected = { width: 640, height: 480 }; + const result = await dimensionResolver({id: 'dimensions'}); + expect(result).toEqual(expected); + }); + it('preflight dimensions (single)', async () => { const { dimensionResolver } = resolvers.resolverFactory({ headers: { 'x-preflight-dimensions': '{ "width": 640, "height": 480 }' } }, true); const expected = { width: 640, height: 480 };