|
| 1 | +import { isUndefined, omit } from 'lodash'; |
| 2 | +import { inject } from 'inversify'; |
| 3 | +import { TYPES as CORE_TYPES } from '@vulcan-sql/core'; |
| 4 | +import { VulcanInternalExtension } from '@vulcan-sql/core'; |
| 5 | +import { BuiltInMiddleware } from '@vulcan-sql/serve/models'; |
| 6 | +import { KoaContext, Next } from '@vulcan-sql/serve/models'; |
| 7 | +import { |
| 8 | + Options as SslOptions, |
| 9 | + httpsResolver, |
| 10 | + xForwardedProtoResolver, |
| 11 | + customProtoHeaderResolver, |
| 12 | + azureResolver, |
| 13 | + forwardedResolver, |
| 14 | +} from 'koa-sslify'; |
| 15 | +import sslify from 'koa-sslify'; |
| 16 | + |
| 17 | +// resolver type for sslify options |
| 18 | +export enum ResolverType { |
| 19 | + /* use local server to run https server, suit for local usage. */ |
| 20 | + LOCAL = 'LOCAL', |
| 21 | + /* |
| 22 | + * RFC standard header (RFC7239) to carry information in a organized way for reverse proxy used. |
| 23 | + * However, currently only little reverse proxies support it. e.g: nginx supported. |
| 24 | + * refer: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded |
| 25 | + * refer: https://www.nginx.com/resources/wiki/start/topics/examples/forwarded/ |
| 26 | + */ |
| 27 | + FORWARDED = 'FORWARDED', |
| 28 | + /* |
| 29 | + * X-Forwarded-Proto header flag is one of the de-facto standard (But not RFC standard) to check and enforce https or not, almost reverse proxies supported. |
| 30 | + * e.g: Heroku, GKE ingress, AWS ELB, nginx. |
| 31 | + */ |
| 32 | + X_FORWARDED_PROTO = 'X_FORWARDED_PROTO', |
| 33 | + /* |
| 34 | + * if use Azure Application Request Routing as reverse proxy, then it use X-ARR-SSL header flag to check and enforce https. |
| 35 | + * refer: https://abhimantiwari.github.io/blog/ARR/ |
| 36 | + */ |
| 37 | + AZURE_ARR = 'AZURE_ARR', |
| 38 | + /* customize the header flag to check and enforce https, when use the type, need to define an custom header flag for checking and enforcing https */ |
| 39 | + CUSTOM = 'CUSTOM', |
| 40 | +} |
| 41 | + |
| 42 | +export type EnforceHttpsOptions = Omit<SslOptions, 'resolver'> & { |
| 43 | + type?: string; |
| 44 | + /* custom proto name when when type is CUSTOM */ |
| 45 | + proto?: string; |
| 46 | +}; |
| 47 | + |
| 48 | +export interface EnforceHttpsConfig { |
| 49 | + enabled: boolean; |
| 50 | + options: EnforceHttpsOptions; |
| 51 | +} |
| 52 | + |
| 53 | +// enforce https middleware |
| 54 | +@VulcanInternalExtension('enforce-https') |
| 55 | +export class EnforceHttpsMiddleware extends BuiltInMiddleware<EnforceHttpsOptions> { |
| 56 | + private koaEnforceHttps = sslify( |
| 57 | + // if not setup "enforce-https", default sslify is LOCAL type |
| 58 | + this.getOptions() ? this.transformOptions(this.getOptions()!) : undefined |
| 59 | + ); |
| 60 | + |
| 61 | + constructor( |
| 62 | + @inject(CORE_TYPES.ExtensionConfig) config: any, |
| 63 | + @inject(CORE_TYPES.ExtensionName) name: string |
| 64 | + ) { |
| 65 | + super(config, name); |
| 66 | + const rawOptions = this.getOptions() as EnforceHttpsOptions; |
| 67 | + |
| 68 | + const options = rawOptions ? this.transformOptions(rawOptions) : undefined; |
| 69 | + this.koaEnforceHttps = sslify(options); |
| 70 | + } |
| 71 | + |
| 72 | + public async handle(context: KoaContext, next: Next) { |
| 73 | + if (!this.enabled) return next(); |
| 74 | + else return this.koaEnforceHttps(context, next); |
| 75 | + } |
| 76 | + |
| 77 | + private transformOptions(rawOptions: EnforceHttpsOptions) { |
| 78 | + // given default value if not exist. |
| 79 | + rawOptions.type = rawOptions.type || ResolverType.LOCAL; |
| 80 | + |
| 81 | + // check incorrect type |
| 82 | + this.checkResolverType(rawOptions.type); |
| 83 | + const type = rawOptions.type.toUpperCase(); |
| 84 | + |
| 85 | + const resolverMapper = { |
| 86 | + [ResolverType.LOCAL.toString()]: () => httpsResolver, |
| 87 | + [ResolverType.FORWARDED.toString()]: () => forwardedResolver, |
| 88 | + [ResolverType.X_FORWARDED_PROTO.toString()]: () => |
| 89 | + xForwardedProtoResolver, |
| 90 | + [ResolverType.AZURE_ARR.toString()]: () => azureResolver, |
| 91 | + }; |
| 92 | + // if type is CUSTOM |
| 93 | + if (type === ResolverType.CUSTOM) { |
| 94 | + if (!rawOptions.proto) |
| 95 | + throw new Error( |
| 96 | + 'The "CUSTOM" type need also provide "proto" in options.' |
| 97 | + ); |
| 98 | + |
| 99 | + return { |
| 100 | + resolver: customProtoHeaderResolver(rawOptions.proto), |
| 101 | + ...omit(rawOptions, ['type', 'proto']), |
| 102 | + } as SslOptions; |
| 103 | + } |
| 104 | + // if not CUSTOM. |
| 105 | + return { |
| 106 | + resolver: resolverMapper[type](), |
| 107 | + ...omit(rawOptions, ['type', 'proto']), |
| 108 | + } as SslOptions; |
| 109 | + } |
| 110 | + |
| 111 | + private checkResolverType(type: string) { |
| 112 | + // check incorrect type |
| 113 | + if (!(type.toUpperCase() in ResolverType)) |
| 114 | + throw new Error( |
| 115 | + `The type is incorrect, only support type in ${JSON.stringify( |
| 116 | + Object.keys(ResolverType) |
| 117 | + )}.` |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Get enforce https options in config |
| 124 | + * @param options EnforceHttpsOptions |
| 125 | + * @returns beside you disabled it, or it return enforce https options when setup "enforce-https"( if not found options, default is LOCAL type ). |
| 126 | + */ |
| 127 | +export const getEnforceHttpsOptions = (options?: { |
| 128 | + enabled: boolean; |
| 129 | + options: EnforceHttpsOptions; |
| 130 | +}): { |
| 131 | + enabled: boolean; |
| 132 | + options: EnforceHttpsOptions; |
| 133 | +} => { |
| 134 | + // if not given "enforce-https" options, return default options |
| 135 | + if (!options) |
| 136 | + return { |
| 137 | + enabled: true, |
| 138 | + options: { type: ResolverType.LOCAL } as EnforceHttpsOptions, |
| 139 | + }; |
| 140 | + |
| 141 | + return { |
| 142 | + enabled: isUndefined(options['enabled']) ? true : false, |
| 143 | + options: |
| 144 | + options['options'] || |
| 145 | + ({ type: ResolverType.LOCAL } as EnforceHttpsOptions), |
| 146 | + }; |
| 147 | +}; |
0 commit comments