Skip to content

Commit

Permalink
URL validator tidyup; affects [discourse dynamic endpoint gerrit jira…
Browse files Browse the repository at this point in the history
… maven nexus osslifecycle python vpm website] securityheaders sonar swagger w3c (#10810)

* add a required url validator

* replace occurrences of optionalUrl.required() with url

* use standard validators in server.js
  • Loading branch information
chris48s authored Jan 18, 2025
1 parent 49bcb52 commit 954147f
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 38 deletions.
9 changes: 6 additions & 3 deletions core/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import { makeSend } from '../base-service/legacy-result-sender.js'
import { handleRequest } from '../base-service/legacy-request-handler.js'
import { clearResourceCache } from '../base-service/resource-cache.js'
import { rasterRedirectUrl } from '../badge-urls/make-badge-url.js'
import { fileSize, nonNegativeInteger } from '../../services/validators.js'
import {
fileSize,
nonNegativeInteger,
optionalUrl,
url as requiredUrl,
} from '../../services/validators.js'
import log from './log.js'
import PrometheusMetrics from './prometheus-metrics.js'
import InfluxMetrics from './influx-metrics.js'
Expand Down Expand Up @@ -54,8 +59,6 @@ const Joi = originalJoi
},
}))

const optionalUrl = Joi.string().uri({ scheme: ['http', 'https'] })
const requiredUrl = optionalUrl.required()
const origins = Joi.arrayFromString().items(Joi.string().origin())
const defaultService = Joi.object({ authorizedOrigins: origins }).default({
authorizedOrigins: [],
Expand Down
4 changes: 2 additions & 2 deletions services/discourse/discourse.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { nonNegativeInteger, optionalUrl } from '../validators.js'
import { nonNegativeInteger, url } from '../validators.js'
import { BaseJsonService, queryParams } from '../index.js'

const schemaSingular = Joi.object({
Expand All @@ -20,7 +20,7 @@ const schemaPlural = Joi.object({
const schema = Joi.alternatives(schemaSingular, schemaPlural)

const queryParamSchema = Joi.object({
server: optionalUrl.required(),
server: url,
}).required()

function singular(variant) {
Expand Down
4 changes: 2 additions & 2 deletions services/dynamic/dynamic-helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'

const queryParamSchema = Joi.object({
url: optionalUrl.required(),
url,
query: Joi.string().required(),
prefix: Joi.alternatives().try(Joi.string(), Joi.number()),
suffix: Joi.alternatives().try(Joi.string(), Joi.number()),
Expand Down
4 changes: 2 additions & 2 deletions services/endpoint/endpoint.service.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { URL } from 'url'
import Joi from 'joi'
import { httpErrors } from '../dynamic-common.js'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { fetchEndpointData } from '../endpoint-common.js'
import { BaseJsonService, InvalidParameter, queryParams } from '../index.js'

const blockedDomains = ['github.com', 'shields.io']

const queryParamSchema = Joi.object({
url: optionalUrl.required(),
url,
}).required()

const description = `
Expand Down
4 changes: 2 additions & 2 deletions services/gerrit/gerrit.service.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { BaseJsonService, pathParam, queryParam } from '../index.js'

const queryParamSchema = Joi.object({
baseUrl: optionalUrl.required(),
baseUrl: url,
}).required()

const schema = Joi.object({
Expand Down
4 changes: 2 additions & 2 deletions services/jira/jira-issue.service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { BaseJsonService, pathParam, queryParam } from '../index.js'
import { authConfig } from './jira-common.js'

const queryParamSchema = Joi.object({
baseUrl: optionalUrl.required(),
baseUrl: url,
}).required()

const schema = Joi.object({
Expand Down
4 changes: 2 additions & 2 deletions services/jira/jira-sprint.service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { BaseJsonService, pathParam, queryParam } from '../index.js'
import { authConfig } from './jira-common.js'

const queryParamSchema = Joi.object({
baseUrl: optionalUrl.required(),
baseUrl: url,
}).required()

const schema = Joi.object({
Expand Down
4 changes: 2 additions & 2 deletions services/maven-metadata/maven-metadata.service.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { renderVersionBadge } from '../version.js'
import { BaseXmlService, NotFound, queryParams } from '../index.js'
import { description } from './maven-metadata.js'

const queryParamSchema = Joi.object({
metadataUrl: optionalUrl.required(),
metadataUrl: url,
versionPrefix: Joi.string().optional(),
versionSuffix: Joi.string().optional(),
}).required()
Expand Down
4 changes: 2 additions & 2 deletions services/nexus/nexus.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Joi from 'joi'
import { renderVersionBadge } from '../version.js'
import {
optionalUrl,
url,
optionalDottedVersionNClausesWithOptionalSuffix,
} from '../validators.js'
import {
Expand Down Expand Up @@ -49,7 +49,7 @@ const nexus2ResolveApiSchema = Joi.object({
}).required()

const queryParamSchema = Joi.object({
server: optionalUrl.required(),
server: url,
queryOpt: Joi.string()
.regex(/(:[\w.]+=[^:]*)+/i)
.optional(),
Expand Down
4 changes: 2 additions & 2 deletions services/osslifecycle/osslifecycle.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { BaseService, InvalidResponse, queryParam } from '../index.js'

const description = `
Expand All @@ -12,7 +12,7 @@ can be viewed on the [OSS Tracker repository](https://github.com/Netflix/osstrac
`

const queryParamSchema = Joi.object({
file_url: optionalUrl.required(),
file_url: url,
}).required()

export default class OssTracker extends BaseService {
Expand Down
4 changes: 2 additions & 2 deletions services/python/python-version-from-toml.service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Joi from 'joi'
import BaseTomlService from '../../core/base-service/base-toml.js'
import { queryParams } from '../index.js'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'

const queryParamSchema = Joi.object({
tomlFilePath: optionalUrl.required(),
tomlFilePath: url,
}).required()

const schema = Joi.object({
Expand Down
4 changes: 2 additions & 2 deletions services/security-headers/security-headers.service.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { BaseService, NotFound, queryParams } from '../index.js'

const queryParamSchema = Joi.object({
url: optionalUrl.required(),
url,
ignoreRedirects: Joi.equal(''),
}).required()

Expand Down
6 changes: 3 additions & 3 deletions services/sonar/sonar-helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Joi from 'joi'
import { queryParams } from '../index.js'
import { colorScale } from '../color-formatters.js'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'

const ratingPercentageScaleSteps = [10, 20, 50, 100]
const ratingScaleColors = [
Expand Down Expand Up @@ -38,7 +38,7 @@ const sonarVersionSchema = Joi.alternatives(

const queryParamSchema = Joi.object({
sonarVersion: sonarVersionSchema,
server: optionalUrl.required(),
server: url,
}).required()

const openApiQueryParams = queryParams(
Expand All @@ -48,7 +48,7 @@ const openApiQueryParams = queryParams(

const queryParamWithFormatSchema = Joi.object({
sonarVersion: sonarVersionSchema,
server: optionalUrl.required(),
server: url,
format: Joi.string().allow('short', 'long').optional(),
}).required()

Expand Down
4 changes: 2 additions & 2 deletions services/swagger/swagger.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { BaseJsonService, NotFound, queryParams } from '../index.js'

const schema = Joi.object()
Expand All @@ -14,7 +14,7 @@ const schema = Joi.object()
.required()

const queryParamSchema = Joi.object({
specUrl: optionalUrl.required(),
specUrl: url,
}).required()

export default class SwaggerValidatorService extends BaseJsonService {
Expand Down
4 changes: 2 additions & 2 deletions services/twitter/twitter.service.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { BaseService, pathParams, queryParams } from '../index.js'

const queryParamSchema = Joi.object({
url: optionalUrl.required(),
url,
}).required()

class TwitterUrl extends BaseService {
Expand Down
7 changes: 7 additions & 0 deletions services/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ export const optionalDottedVersionNClausesWithOptionalSuffix =
*/
export const optionalUrl = Joi.string().uri({ scheme: ['http', 'https'] })

/**
* Joi validator that checks if a value is a URL and the value must be present.
*
* @type {Joi}
*/
export const url = optionalUrl.required()

/**
* Joi validator for a file size we are going to pass to bytes.parse
* see https://github.com/visionmedia/bytes.js#bytesparsestringnumber-value-numbernull
Expand Down
4 changes: 2 additions & 2 deletions services/vpm/vpm-version.service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { latest, renderVersionBadge } from '../version.js'
import { BaseJsonService, NotFound, pathParam, queryParam } from '../index.js'

const queryParamSchema = Joi.object({
repository_url: optionalUrl.required(),
repository_url: url,
include_prereleases: Joi.equal(''),
}).required()

Expand Down
4 changes: 2 additions & 2 deletions services/w3c/w3c-validation.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import { BaseJsonService, NotFound, pathParam, queryParam } from '../index.js'
import {
description,
Expand All @@ -25,7 +25,7 @@ const schema = Joi.object({
}).required()

const queryParamSchema = Joi.object({
targetUrl: optionalUrl.required(),
targetUrl: url,
preset: Joi.string().regex(presetRegex).allow(''),
}).required()

Expand Down
4 changes: 2 additions & 2 deletions services/website/website.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import emojic from 'emojic'
import Joi from 'joi'
import trace from '../../core/base-service/trace.js'
import { BaseService, queryParams } from '../index.js'
import { optionalUrl } from '../validators.js'
import { url } from '../validators.js'
import {
queryParamSchema,
renderWebsiteStatus,
Expand All @@ -20,7 +20,7 @@ A site will be classified as "down" if it fails to respond within 3.5 seconds.
`

const urlQueryParamSchema = Joi.object({
url: optionalUrl.required(),
url,
}).required()

export default class Website extends BaseService {
Expand Down

0 comments on commit 954147f

Please sign in to comment.