Skip to content

Commit

Permalink
fix validation of endpoint spec
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ke committed Nov 14, 2023
1 parent 6ae889b commit fb87b50
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions swarmgate/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ function doesVolumeExist(volumeName: string): Promise<boolean> {
});
}

async function isValidEndpoint(
res: express.Response,
endpoint: Docker.Endpoint): Promise<boolean> {
if (endpoint.Spec?.Ports) {
for (const port of endpoint.Spec?.Ports) {
if (!ALLOW_PORT_EXPOSE) {
res.status(403).send(`Access denied: Exposing ports is not allowed.`);
return false;
}
}
}
return true;
}

type TaskTemplate = {
ContainerSpec?: {
Secrets?: { SecretName: string }[],
Expand All @@ -208,7 +222,6 @@ type TaskTemplate = {
},
Runtime?: string,
Networks?: { Target: string }[],
EndpointSpec?: { Ports?: { TargetPort: number, Protocol: string }[] }
}
// returns true if we should continue
async function isValidTaskTemplate(
Expand All @@ -235,15 +248,6 @@ async function isValidTaskTemplate(
}
}

if (taskTemplate.EndpointSpec?.Ports) {
for (const port of taskTemplate.EndpointSpec.Ports) {
if (!ALLOW_PORT_EXPOSE) {
res.status(403).send(`Access denied: Exposing ports is not allowed.`);
return false;
}
}
}

if (containerSpec) {
if (containerSpec.Secrets) {
for (const secret of containerSpec.Secrets) {
Expand Down Expand Up @@ -311,6 +315,10 @@ app.post('/:version?/services/create', async (req, res) => {
return;
}

if(serviceSpec.EndpointSpec && !await isValidEndpoint(res, serviceSpec.EndpointSpec)) {
return;
}

serviceSpec.Labels = { ...serviceSpec.Labels, [label]: labelValue };
if (taskTemplate.ContainerSpec) {
taskTemplate.ContainerSpec.Labels = { ...taskTemplate.ContainerSpec.Labels || {}, [label]: labelValue };
Expand Down Expand Up @@ -353,6 +361,10 @@ app.post('/:version?/services/:id/update', async (req, res) => {
}
}

if(updateSpec.EndpointSpec && !await isValidEndpoint(res, updateSpec.EndpointSpec)) {
return;
}

const service = docker.getService(serviceId);

updateSpec.version = req.query.version;
Expand Down

0 comments on commit fb87b50

Please sign in to comment.