Skip to content

Commit 80a88bf

Browse files
committed
fix: add defensive checks in storage resolvers instead of non-null assertions
Replace cdn! and credential! non-null assertions with explicit error messages in both bucket-provisioner-resolver.ts and presigned-url-resolver.ts. If CDN env vars are missing, the resolvers now throw clear errors explaining which variables to set, instead of silently passing undefined to S3 clients.
1 parent fe60f7b commit 80a88bf

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

graphile/graphile-settings/src/bucket-provisioner-resolver.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,22 @@ export function getBucketProvisionerConnection(): StorageConnectionConfig {
2929

3030
const { cdn } = getEnvOptions();
3131

32-
// cdn is guaranteed populated — pgpmDefaults provides all CDN fields
33-
const { provider, awsRegion, awsAccessKey, awsSecretKey, endpoint } = cdn!;
32+
if (!cdn) {
33+
throw new Error(
34+
'[bucket-provisioner-resolver] CDN config not found. ' +
35+
'Ensure CDN environment variables (AWS_ACCESS_KEY, AWS_SECRET_KEY, etc.) ' +
36+
'are set or that pgpmDefaults provides CDN fields.',
37+
);
38+
}
39+
40+
const { provider, awsRegion, awsAccessKey, awsSecretKey, endpoint } = cdn;
41+
42+
if (!awsAccessKey || !awsSecretKey) {
43+
throw new Error(
44+
'[bucket-provisioner-resolver] Missing S3 credentials. ' +
45+
'Set AWS_ACCESS_KEY and AWS_SECRET_KEY environment variables.',
46+
);
47+
}
3448

3549
log.info(
3650
`[bucket-provisioner-resolver] Initializing: provider=${provider} endpoint=${endpoint}`,
@@ -39,8 +53,8 @@ export function getBucketProvisionerConnection(): StorageConnectionConfig {
3953
connectionConfig = {
4054
provider: (provider as StorageConnectionConfig['provider']) || 'minio',
4155
region: awsRegion || 'us-east-1',
42-
accessKeyId: awsAccessKey!,
43-
secretAccessKey: awsSecretKey!,
56+
accessKeyId: awsAccessKey,
57+
secretAccessKey: awsSecretKey,
4458
...(endpoint ? { endpoint, forcePathStyle: true } : {}),
4559
};
4660

graphile/graphile-settings/src/presigned-url-resolver.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,43 @@ export function getPresignedUrlS3Config(): S3Config {
2929

3030
const { cdn } = getEnvOptions();
3131

32-
// cdn is guaranteed populated — pgpmDefaults provides all CDN fields
33-
const { bucketName, awsRegion, awsAccessKey, awsSecretKey, endpoint, publicUrlPrefix } = cdn!;
32+
if (!cdn) {
33+
throw new Error(
34+
'[presigned-url-resolver] CDN config not found. ' +
35+
'Ensure CDN environment variables (AWS_ACCESS_KEY, AWS_SECRET_KEY, etc.) ' +
36+
'are set or that pgpmDefaults provides CDN fields.',
37+
);
38+
}
39+
40+
const { bucketName, awsRegion, awsAccessKey, awsSecretKey, endpoint, publicUrlPrefix } = cdn;
41+
42+
if (!awsAccessKey || !awsSecretKey) {
43+
throw new Error(
44+
'[presigned-url-resolver] Missing S3 credentials. ' +
45+
'Set AWS_ACCESS_KEY and AWS_SECRET_KEY environment variables.',
46+
);
47+
}
48+
49+
if (!bucketName) {
50+
throw new Error(
51+
'[presigned-url-resolver] Missing CDN bucket name. ' +
52+
'Set CDN_BUCKET_NAME environment variable.',
53+
);
54+
}
3455

3556
log.info(
3657
`[presigned-url-resolver] Initializing: bucket=${bucketName} endpoint=${endpoint}`,
3758
);
3859

3960
const client = new S3Client({
4061
region: awsRegion,
41-
credentials: { accessKeyId: awsAccessKey!, secretAccessKey: awsSecretKey! },
62+
credentials: { accessKeyId: awsAccessKey, secretAccessKey: awsSecretKey },
4263
...(endpoint ? { endpoint, forcePathStyle: true } : {}),
4364
});
4465

4566
s3Config = {
4667
client,
47-
bucket: bucketName!,
68+
bucket: bucketName,
4869
region: awsRegion,
4970
publicUrlPrefix,
5071
...(endpoint ? { endpoint, forcePathStyle: true } : {}),

0 commit comments

Comments
 (0)