Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(docker): bound the image and container inspections

Yudhi Armyndharisec09584063c0
diff --git a/src/modules/docker/docker.service.spec.ts b/src/modules/docker/docker.service.spec.ts
index 171c52a55..1a7bb6c8b 100644
--- a/src/modules/docker/docker.service.spec.ts
+++ b/src/modules/docker/docker.service.spec.ts
@@ -588,6 +588,7 @@ describe('DockerService.createService', () => {
it('returns false when the daemon rejects the image pull', async () => {
const docker = {
+ modem: {},
getImage: imageNotCached,
pull: (_image: string, cb: (err: Error | null, stream: null) => void) =>
cb(new Error('pull access denied'), null),
@@ -602,6 +603,7 @@ describe('DockerService.createService', () => {
const start = jest.fn().mockResolvedValue(undefined);
const getImage = jest.fn(() => ({ inspect: jest.fn().mockResolvedValue({ Id: 'sha256:abc' }) }));
const docker = {
+ modem: {},
getImage,
pull: jest.fn(),
createVolume: jest.fn().mockResolvedValue({}),
@@ -1003,3 +1005,125 @@ describe('DockerService availability recovery', () => {
await expect(service.listContainers()).resolves.toEqual([]);
});
});
+
+describe('DockerService image inspection failures', () => {
+ function serviceFor(docker: unknown): DockerService {
+ const service = new DockerService();
+ service.callTimeoutMs = 40;
+ Object.assign(service as unknown as Record<string, unknown>, { docker, isAvailable: true });
+ jest.spyOn(service, 'getContainerByService').mockResolvedValue(null);
+ return service;
+ }
+
+ afterEach(() => {
+ jest.useRealTimers();
+ jest.restoreAllMocks();
+ });
+
+ it('aborts the real Dockerode image request without starting a pull after its deadline', async () => {
+ const RealDocker = jest.requireActual<typeof import('dockerode')>('dockerode');
+ let signal: AbortSignal | undefined;
+ const dial = jest.fn(
+ (options: { abortSignal?: AbortSignal }, callback: (error: Error | null, value?: unknown) => void) => {
+ signal = options.abortSignal;
+ signal?.addEventListener('abort', () => callback(new Error('inspection aborted')), { once: true });
+ },
+ );
+ const modem = { Promise, dial };
+ const pull = jest.fn();
+ const service = serviceFor({ modem, getImage: () => new RealDocker.Image(modem, 'redis:7-alpine'), pull });
+ jest.useFakeTimers();
+ const result = service.createService('redis');
+ await jest.advanceTimersByTimeAsync(80);
+
+ await expect(result).resolves.toBe(false);
+ expect(signal?.aborted).toBe(true);
+ expect(modem.dial).toBe(dial);
+ expect(pull).not.toHaveBeenCalled();
+ });
+
+ it.each([401, 500, undefined])('does not pull after an inspection error with status %s', async statusCode => {
+ const pull = jest.fn();
+ const error = Object.assign(new Error('inspection failed'), { statusCode });
+ const service = serviceFor({
+ modem: {},
+ getImage: () => ({ inspect: () => Promise.reject(error) }),
+ pull,
+ });
+
+ await expect(service.createService('redis')).resolves.toBe(false);
+ expect(pull).not.toHaveBeenCalled();
+ });
+
+ it('lets a missing image finish pulling after the short inspection deadline', async () => {
+ let finishPull: ((error: Error | null) => void) | undefined;
+ const start = jest.fn().mockResolvedValue(undefined);
+ const service = serviceFor({
+ modem: { followProgress: (_stream: unknown, callback: (error: Error | null) => void) => (finishPull = callback) },
+ getImage: imageNotCached,
+ pull: (_image: string, callback: (error: Error | null, stream: unknown) => void) => callback(null, {}),
+ createVolume: jest.fn().mockResolvedValue({}),
+ createContainer: jest.fn().mockResolvedValue({ start }),
+ });
+ jest.useFakeTimers();
+ let finished = false;
+ const result = service.createService('redis').then(value => {
+ finished = true;
+ return value;
+ });
+ await jest.advanceTimersByTimeAsync(160);
+ expect(finished).toBe(false);
+ expect(finishPull).toBeDefined();
+ finishPull!(null);
+ await expect(result).resolves.toBe(true);
+ expect(start).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('DockerService existing container inspection deadlines', () => {
+ afterEach(() => {
+ jest.useRealTimers();
+ jest.restoreAllMocks();
+ });
+
+ it.each(['create', 'start', 'stop'] as const)(
+ 'aborts a stalled inspection before %s mutates a container',
+ async operation => {
+ const service = new DockerService();
+ service.callTimeoutMs = 40;
+ Object.assign(service as unknown as Record<string, unknown>, { docker: {}, isAvailable: true });
+ let signal: AbortSignal | undefined;
+ const container = {
+ inspect: jest.fn(
+ (options?: Docker.ContainerInspectOptions) =>
+ new Promise<never>((_resolve, reject) => {
+ signal = options?.abortSignal;
+ signal?.addEventListener('abort', () => reject(new Error('inspection aborted')), { once: true });
+ }),
+ ),
+ start: jest.fn(),
+ stop: jest.fn(),
+ };
+ jest.spyOn(service, 'getContainerByService').mockResolvedValue(container as never);
+ jest.useFakeTimers();
+ let settled: boolean | undefined;
+ const action =
+ operation === 'create'
+ ? service.createService('redis')
+ : operation === 'start'
+ ? service.startService('cache')
+ : service.stopService('cache');
+ const work = action.then(value => {
+ settled = value;
+ return value;
+ });
+ await jest.advanceTimersByTimeAsync(80);
+ expect(settled).toBe(false);
+ expect(signal?.aborted).toBe(true);
+ expect(container.inspect).toHaveBeenCalledTimes(1);
+ expect(container.start).not.toHaveBeenCalled();
+ expect(container.stop).not.toHaveBeenCalled();
+ await expect(work).resolves.toBe(false);
+ },
+ );
+});
diff --git a/src/modules/docker/docker.service.ts b/src/modules/docker/docker.service.ts
index c3d34b010..78da3149b 100644
--- a/src/modules/docker/docker.service.ts
+++ b/src/modules/docker/docker.service.ts
@@ -446,7 +446,7 @@ export class DockerService implements OnModuleInit {
// Check if container already exists
const existing = await this.getContainerByService(profile);
if (existing) {
- const info = await existing.inspect();
+ const info = await this.answered(abortSignal => existing.inspect({ abortSignal }), 'a container inspection');
this.warnOnImageDrift(profile, spec.name, info);
if (info.State.Running) {
this.logger.log(`Container ${spec.name} already running`);
@@ -461,14 +461,26 @@ export class DockerService implements OnModuleInit {
// Pull only when the image is not already on the host, as compose's default
// `pull_policy: missing` does: a host that has the image keeps working when the registry is
// unreachable or has withdrawn it. As with compose, a cached floating tag is not refreshed
- // here. Any inspect failure falls back to pulling.
- const cached = await this.docker
- .getImage(spec.image)
- .inspect()
- .then(
- () => true,
- () => false,
- );
+ // here. Only an answer that the image is absent pulls: a daemon that fails or does not answer the
+ // inspection could not complete a pull either, and the pull itself is not bounded.
+ const cached = await this.answered(abortSignal => {
+ const image = Object.create(this.docker!.getImage(spec.image)) as Docker.Image;
+ const modem = this.docker!.modem;
+ // Dockerode 5 leaves the signal out of an image inspection; forward it without changing the shared modem.
+ const imageModem = Object.create(modem) as Docker['modem'];
+ image.modem = imageModem;
+ imageModem.dial = (
+ options: Parameters<Docker['modem']['dial']>[0],
+ callback: Parameters<Docker['modem']['dial']>[1],
+ ) => modem.dial({ ...options, abortSignal }, callback);
+ return image.inspect({ abortSignal } as Docker.ImageInspectOptions);
+ }, 'an image inspection').then(
+ () => true,
+ (error: unknown) => {
+ if ((error as { statusCode?: number } | null)?.statusCode !== 404) throw error;
+ return false;
+ },
+ );
if (!cached) {
this.logger.log(`Pulling image: ${spec.image}`);
await new Promise<void>((resolve, reject) => {
@@ -560,7 +572,7 @@ export class DockerService implements OnModuleInit {
}
try {
- const info = await container.inspect();
+ const info = await this.answered(abortSignal => container.inspect({ abortSignal }), 'a container inspection');
this.warnOnImageDrift(profile, info.Name?.replace(/^\//, '') || service, info);
if (info.State.Running) {
this.logger.log(`Service '${service}' is already running`);
@@ -637,7 +649,7 @@ export class DockerService implements OnModuleInit {
}
try {
- const info = await container.inspect();
+ const info = await this.answered(abortSignal => container.inspect({ abortSignal }), 'a container inspection');
if (!info.State.Running) {
this.logger.log(`Service '${service}' is already stopped`);
return true;