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

cameri / nostream / 26431959051

26 May 2026 04:17AM UTC coverage: 62.695% (-2.4%) from 65.108%
26431959051

Pull #628

github

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

1755 of 3168 branches covered (55.4%)

Branch coverage included in aggregate %.

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

140 existing lines in 23 files now uncovered.

4038 of 6072 relevant lines covered (66.5%)

7.99 hits per line

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

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

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

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

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

13
  return input
19✔
14
}
15

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

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

23
  const normalizedRemote = normalizeIpAddress(ipAddress)
10✔
24

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

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

34
  if (typeof deprecatedHeader === 'string' && deprecatedHeader.trim() !== '') {
51!
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 => {
1✔
42
  warnIfDeprecatedRemoteIpHeaderIsConfigured(settings)
51✔
43

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

46
  const trustedProxies = settings.network?.trustedProxies
51✔
47
  if (header && (!Array.isArray(trustedProxies) || trustedProxies.length === 0)) {
51✔
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
51✔
52
  const headerAddress = Array.isArray(rawHeaderAddress) ? rawHeaderAddress[0] : rawHeaderAddress
51✔
53
  const socketAddress = request.socket.remoteAddress
51✔
54

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

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

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

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

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

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

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

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

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

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

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

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

107
  return normalizePathPrefix(rawPrefix)
4✔
108
}
109

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

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

118
  return `${normalizedPrefix}${normalizedPath}`
12✔
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