Skip to content

Commit d6741ee

Browse files
Security: can set array of auth tokens (#417)
* Security: can set array of auth tokens * Update src/config.ts Co-authored-by: Selene <[email protected]> --------- Co-authored-by: Selene <[email protected]>
1 parent d49faae commit d6741ee

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

scripts/clean_target.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
ARCH="${1:-}"
4+
OUT="${2:-}"
5+
6+
if [ -z "$ARCH" ]; then
7+
echo "ARCH (arg 1) has to be set"
8+
exit 1
9+
fi
10+
11+
PLUGIN_NAME=plugin-${ARCH}
12+
13+
if [ ! -z "$OUT" ]; then
14+
PLUGIN_NAME=${OUT}
15+
fi
16+
17+
rm -rf .dist/${PLUGIN_NAME}
18+
rm -f ./artifacts/${PLUGIN_NAME}.zip

src/app.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ function populateServiceConfigFromEnv(config: ServiceConfig, env: NodeJS.Process
109109
}
110110

111111
if (env['AUTH_TOKEN']) {
112-
config.service.security.authToken = env['AUTH_TOKEN'];
112+
const authToken = env['AUTH_TOKEN'] as string;
113+
config.service.security.authToken = authToken.includes(' ') ? authToken.split(' ') : authToken;
113114
}
114115

115116
if (env['LOG_LEVEL']) {

src/config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface LoggingConfig {
5656
}
5757

5858
export interface SecurityConfig {
59-
authToken: string;
59+
authToken: string | string[];
6060
}
6161

6262
export interface ServiceConfig {
@@ -148,3 +148,12 @@ export const readJSONFileSync = (filePath: string): any => {
148148
const rawdata = fs.readFileSync(filePath, 'utf8');
149149
return JSON.parse(rawdata);
150150
};
151+
152+
export const isAuthTokenValid = (config: SecurityConfig, reqAuthToken: string): boolean => {
153+
let configToken = config.authToken || [''];
154+
if (typeof configToken === "string") {
155+
configToken = [configToken]
156+
}
157+
158+
return reqAuthToken !== "" && configToken.includes(reqAuthToken)
159+
}

src/plugin/v2/grpc_plugin.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as protoLoader from '@grpc/proto-loader';
33
import * as promClient from 'prom-client';
44
import { GrpcPlugin } from '../../node-plugin';
55
import { Logger } from '../../logger';
6-
import { PluginConfig, SecurityConfig } from '../../config';
6+
import { PluginConfig, SecurityConfig, isAuthTokenValid } from '../../config';
77
import { createBrowser, Browser } from '../../browser';
88
import { HTTPHeaders, ImageRenderOptions, RenderOptions } from '../../types';
99
import {
@@ -93,7 +93,7 @@ export class RenderGRPCPluginV2 implements GrpcPlugin {
9393
class PluginGRPCServer {
9494
private browserVersion: string | undefined;
9595

96-
constructor(private browser: Browser, private log: Logger, private sanitizer: Sanitizer, private securityCfg: SecurityConfig) {}
96+
constructor(private browser: Browser, private log: Logger, private sanitizer: Sanitizer, private securityCfg: SecurityConfig) { }
9797

9898
async start(browserVersion?: string) {
9999
this.browserVersion = browserVersion;
@@ -108,8 +108,7 @@ class PluginGRPCServer {
108108
return callback({ code: Status.INVALID_ARGUMENT, details: 'Request cannot be null' });
109109
}
110110

111-
const configToken = this.securityCfg.authToken || '';
112-
if (!req.authToken || req.authToken !== configToken) {
111+
if (!isAuthTokenValid(this.securityCfg, req.authToken)) {
113112
return callback({ code: Status.UNAUTHENTICATED, details: 'Unauthorized request' });
114113
}
115114

@@ -158,8 +157,7 @@ class PluginGRPCServer {
158157
return callback({ code: Status.INVALID_ARGUMENT, details: 'Request cannot be null' });
159158
}
160159

161-
const configToken = this.securityCfg.authToken || '';
162-
if (!req.authToken || req.authToken !== configToken) {
160+
if (!isAuthTokenValid(this.securityCfg, req.authToken)) {
163161
return callback({ code: Status.UNAUTHENTICATED, details: 'Unauthorized request' });
164162
}
165163

@@ -217,8 +215,7 @@ class PluginGRPCServer {
217215
async sanitize(call: grpc.ServerUnaryCall<GRPCSanitizeRequest, any>, callback: grpc.sendUnaryData<GRPCSanitizeResponse>) {
218216
const grpcReq = call.request;
219217

220-
const configToken = this.securityCfg.authToken || '';
221-
if (!grpcReq.authToken || grpcReq.authToken !== configToken) {
218+
if (!isAuthTokenValid(this.securityCfg, grpcReq.authToken)) {
222219
return callback({ code: Status.UNAUTHENTICATED, details: 'Unauthorized request' });
223220
}
224221

@@ -324,7 +321,8 @@ const populateConfigFromEnv = (config: PluginConfig) => {
324321
}
325322

326323
if (env['GF_PLUGIN_AUTH_TOKEN']) {
327-
config.plugin.security.authToken = env['GF_PLUGIN_AUTH_TOKEN'];
324+
const authToken = env['GF_PLUGIN_AUTH_TOKEN'] as string;
325+
config.plugin.security.authToken = authToken.includes(' ') ? authToken.split(' ') : authToken;
328326
}
329327
};
330328

src/service/middlewares.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import express = require('express');
22
import * as boom from '@hapi/boom';
33
import { ImageRenderOptions } from '../types';
4-
import { SecurityConfig } from '../config';
4+
import { SecurityConfig, isAuthTokenValid } from '../config';
55

66
export const asyncMiddleware = (fn) => (req, res, next) => {
77
Promise.resolve(fn(req, res, next)).catch((err) => {
@@ -28,10 +28,8 @@ export const trustedUrlMiddleware = (
2828

2929
export const authTokenMiddleware = (config: SecurityConfig) => {
3030
return (req: express.Request<any, any, any, ImageRenderOptions, any>, res: express.Response, next: express.NextFunction) => {
31-
const cfgToken = config.authToken || '';
3231
const headerToken = req.header('X-Auth-Token');
33-
34-
if (headerToken === undefined || headerToken !== cfgToken) {
32+
if (headerToken === undefined || !isAuthTokenValid(config, headerToken)) {
3533
return next(boom.unauthorized('Unauthorized request'));
3634
}
3735

0 commit comments

Comments
 (0)