• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cameri / nostream / 26462901868

26 May 2026 05:01PM UTC coverage: 65.065% (-0.04%) from 65.108%
26462901868

Pull #628

github

web-flow
Merge 2c158e839 into aee894068
Pull Request #628: refactor(http): remove deprecated remote_ip_header fallback

1835 of 3168 branches covered (57.92%)

Branch coverage included in aggregate %.

7 of 8 new or added lines in 1 file covered. (87.5%)

1 existing line in 1 file now uncovered.

4177 of 6072 relevant lines covered (68.79%)

19.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.38
/src/utils/http.ts
1
import { IncomingMessage } from 'http'
2

3
import { createLogger } from '../factories/logger-factory'
2✔
4
import { Settings } from '../@types/settings'
5

6
const logger = createLogger('http-utils')
2✔
7

8
const normalizeIpAddress = (input: string): string => {
2✔
9
  if (input.startsWith('::ffff:')) {
994✔
10
    return input.slice(7)
245✔
11
  }
12

13
  return input
749✔
14
}
15

16
const isTrustedProxy = (ipAddress: string, settings: Settings): boolean => {
2✔
17
  const trustedProxies = settings.network?.trustedProxies
302✔
18

19
  if (!Array.isArray(trustedProxies) || trustedProxies.length === 0) {
302✔
20
    return false
49✔
21
  }
22

23
  const normalizedRemote = normalizeIpAddress(ipAddress)
253✔
24

25
  return trustedProxies.some((trustedProxy) => {
253✔
26
    return normalizeIpAddress(trustedProxy) === normalizedRemote
741✔
27
  })
28
}
29

30
const warnIfDeprecatedRemoteIpHeaderIsConfigured = (settings: Settings): void => {
2✔
31
  const networkSettings = settings.network as Record<string, unknown> | undefined
285✔
32
  const deprecatedHeader = networkSettings?.remote_ip_header
285✔
33

34
  if (typeof deprecatedHeader === 'string' && deprecatedHeader.trim() !== '') {
285!
NEW
35
    logger.warn(
×
36
      'WARNING: network.remote_ip_header is deprecated and no longer used. Rename it to network.remoteIpHeader to restore forwarded header handling.',
37
    )
38
  }
39
}
40

41
export const getRemoteAddress = (request: IncomingMessage, settings: Settings): string => {
2✔
42
  warnIfDeprecatedRemoteIpHeaderIsConfigured(settings)
285✔
43

44
  const header = settings.network?.remoteIpHeader as string
285✔
45

46
  const trustedProxies = settings.network?.trustedProxies
285✔
47
  if (header && (!Array.isArray(trustedProxies) || trustedProxies.length === 0)) {
285✔
48
    logger.warn('WARNING: network.remoteIpHeader is set but network.trustedProxies is empty. Forwarded headers will be ignored. Add your proxy IP to network.trustedProxies.')
2✔
49
  }
50

51
  const rawHeaderAddress = header ? request.headers[header] : undefined
285✔
52
  const headerAddress = Array.isArray(rawHeaderAddress) ? rawHeaderAddress[0] : rawHeaderAddress
285✔
53
  const socketAddress = request.socket.remoteAddress
285✔
54

55
  const trustedProxy = typeof socketAddress === 'string'
285✔
56
    && isTrustedProxy(socketAddress, settings)
57

58
  const result = trustedProxy && typeof headerAddress === 'string'
285✔
59
    ? headerAddress
60
    : socketAddress
61

62
  return (result as string).split(',')[0].trim()
285✔
63
}
64

65
const normalizePathPrefix = (pathPrefix: string | undefined): string => {
2✔
66
  if (typeof pathPrefix !== 'string') {
65✔
67
    return ''
9✔
68
  }
69

70
  const prefix = pathPrefix.split(',')[0].trim()
56✔
71

72
  if (!prefix.startsWith('/') || prefix.startsWith('//')) {
56✔
73
    return ''
2✔
74
  }
75

76
  try {
54✔
77
    const { pathname } = new URL(prefix, 'http://nostream.local')
54✔
78
    const normalized = pathname.replace(/\/+$/, '')
54✔
79

80
    return normalized === '/' ? '' : normalized
54!
81
  } catch {
82
    return ''
×
83
  }
84
}
85

86
const getRelayUrlPathPrefix = (relayUrl: string | undefined): string => {
2✔
87
  if (typeof relayUrl !== 'string') {
61✔
88
    return ''
9✔
89
  }
90

91
  try {
52✔
92
    return normalizePathPrefix(new URL(relayUrl).pathname)
52✔
93
  } catch {
94
    return ''
×
95
  }
96
}
97

98
const getTrustedForwardedPathPrefix = (request: IncomingMessage, settings: Settings): string => {
2✔
99
  const socketAddress = request.socket?.remoteAddress
63✔
100
  if (typeof socketAddress !== 'string' || !isTrustedProxy(socketAddress, settings)) {
63✔
101
    return ''
50✔
102
  }
103

104
  const rawHeader = request.headers?.['x-forwarded-prefix']
13✔
105
  const rawPrefix = Array.isArray(rawHeader) ? rawHeader[0] : rawHeader
13!
106

107
  return normalizePathPrefix(rawPrefix)
13✔
108
}
109

110
export const getPublicPathPrefix = (request: IncomingMessage, settings: Settings): string => {
2✔
111
  return getTrustedForwardedPathPrefix(request, settings) || getRelayUrlPathPrefix(settings.info?.relay_url)
63✔
112
}
113

114
export const joinPathPrefix = (prefix: string, path: string): string => {
2✔
115
  const normalizedPrefix = prefix.replace(/\/+$/, '')
17✔
116
  const normalizedPath = path.startsWith('/') ? path : `/${path}`
17!
117

118
  return `${normalizedPrefix}${normalizedPath}`
17✔
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc