Skip to content

Commit

Permalink
Fix case sensitivity in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Nov 9, 2024
1 parent 32e3174 commit 4e4b379
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion dependencies/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion examples/cloudformation/custom_hostname.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/sam/customization/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion extras/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions sam/cloudfront/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sam/standalone/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sam/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 10 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down Expand Up @@ -68,6 +74,7 @@ module.exports = {
addCorsHeaders,
eventPath,
fileMissing,
getHeaderValue,
getUri,
isBase64,
isTooLarge,
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/resolvers.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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) };
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions tests/resolvers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit 4e4b379

Please sign in to comment.