Skip to content

Add --record_fname_encode to record calls whose Call-ID contains '/' - #187

Open
mdzesseis wants to merge 4 commits into
sippy:masterfrom
mdzesseis:record-fname-encode
Open

mdzesseis wants to merge 4 commits into
sippy:masterfrom
mdzesseis:record-fname-encode

Conversation

@mdzesseis

Copy link
Copy Markdown

Follow-up to #179.

Since 9c50b4e, rtpp_record_ctor() refuses to record (R, Rs, -a) when the Call-ID or from-tag used in the file name contains /. That closes the path traversal, and I agree that C with a name chosen by the client is the right long-term answer.

In the meantime, clients that still send R lose those recordings completely. / is legal in a Call-ID and some carriers use it routinely. Moving the client to C is not always possible soon, because it needs a config change in the proxy and in everything that later finds the recording by name.

This PR adds an opt-in --record_fname_encode option:

Tests: a new recording/basic sub-test, badpath_enc. With the option it records the same traversal Call-IDs as badpath, plus one with %, and checks they are created under their encoded names inside the recording directory and nowhere else. It also checks that an over-long Call-ID is still refused. Against master the new sub-test fails, because the option does not exist there (unrecognized option '--record_fname_encode'), while badpath passes on both. Local results with the project's own docker/Dockerfile.ci + scripts/do-test.sh are in the comment below.

Docs: usage(), doc/manpage.xml and rtpproxy.8.

If you would rather not have a global option for this, I am happy to rework it, for example as an R/Rs command modifier.

Since 9c50b4e, rtpp_record_ctor() refuses to record (R, Rs, -a) when
the Call-ID or from-tag used in the file name contains '/', which keeps
recordings inside of the recording directory. '/' is a legal Call-ID
character and some carriers use it routinely, so those calls end up not
recorded at all for clients that still rely on R.

Add an opt-in --record_fname_encode option. When it is given, '/' and
'%' in the Call-ID and from-tag are percent-encoded as "%2F" and "%25"
before the name is built, so the file is created inside of the recording
directory, never contains '/', and the original value can be recovered
by decoding the "%XX" sequences. The encoded value is bounded by the same
PATH_MAX buffers and the snprintf() truncation checks are kept, so names
that do not fit are still refused. The default behaviour is unchanged:
without the option '/' is refused as before (the log message now points
at the option). Names given explicitly with C are not affected.

Add a recording/basic sub-test, badpath_enc, that records the traversal
Call-IDs from badpath and a Call-ID with '%' under their encoded names
with the option, and still refuses an over-long Call-ID.
@mdzesseis

Copy link
Copy Markdown
Author

Local test results, reproduced with the project's own CI recipe (docker/Dockerfile.ci, BASE_IMAGE=ubuntu:24.04, then scripts/do-test.sh), on this branch rebased on 11647ea8:

master 11647ea8 (gcc) this PR (gcc) this PR (clang)
Functional suite 44 PASS, 1 SKIP, 1 FAIL (startstop/badargs) same same
recording/basic · badpath OK OK OK
recording/basic · badpath_enc (new) FAILED: unrecognized option '--record_fname_encode' (sub-test copied over and run with SUB_TESTS) OK OK
recording/basic · all other sub-tests OK OK OK
Compiler warnings in src/rtpp_record.c / src/main.c pre-existing -Wmaybe-uninitialized on ulen32 same, no new ones none

startstop/badargs (-L&100500) fails the same way on master here, so it comes from my local Docker environment, not from this change.

I also ran the PR binary as a systemd service (ExecStart=/usr/local/bin/rtpproxy -f ... -u rtpproxy:rtpproxy -r <dir> -S <spool> --record_fname_encode) and drove U/L/R/D plus 100 RTP packets per leg, sending R with both tag orders:

Call-ID with --record_fname_encode without it
[email protected] [email protected]=tagA.{a,o}.rtp same name
ab/[email protected] ab%[email protected]=tagA.{a,o}.rtp E50, refused
a%41b@host a%2541b@host=tagA.{a,o}.rtp a%41b@host=tagA.{a,o}.rtp
../../tmp/escape ..%2F..%2Ftmp%2Fescape=tagA.{a,o}.rtp, inside the recording directory E50, refused
mix/%2F/x@h mix%2F%252F%2Fx@h=tagA.{a,o}.rtp (not run)
4200-character Call-ID E50, "recording file name is too long", service keeps running (not run)

Every recorded leg had all 100 packets, and extractaudio turned each pair into a 2.0 s WAV. Decoding %XX in the file names gives back the original Call-IDs. Nothing was written outside the recording directory.

Instead of percent-encoding '/' and '%' in the Call-ID, select how the
Call-ID is put into the recording file name built for R, Rs and -a:

  raw     the Call-ID as is (the default), '/' is refused as before;
  sha256  SHA-256 digest of the Call-ID in lowercase hex, recommended
          for its security properties; fixed length, so even Call-IDs
          too long to be used as is can be recorded; needs OpenSSL;
  b64url  URL-safe base64 (RFC 4648 sec. 5) without padding, for when
          the Call-ID has to be recovered from the file name.

Neither sha256 nor b64url can produce '/', so the result always stays
inside of the recording directory. The from-tag is used as is with any
method and '/' in it is still refused. Names given with C/Cs and remote
(udp:) recording are not affected.

An unknown method, or sha256 in a build without OpenSSL, is reported at
startup and rtpproxy exits with a non-zero code; usage() lists only the
methods available in the build. The option parsing, the RTPP_RCID_*
values and rtpp_record_callid_fname(), which builds and checks the
"<call-id>=<from-tag>" part of the file name and reports why it cannot,
live in a new rtpp_record_util.[ch] rather than in main.c and
rtpp_record.c.

configure gets a standalone libcrypto (EVP_Digest) check, independent
of libsrtp, that defines HAVE_LIBCRYPTO in config.h and sets an automake
conditional of the same name, under which ${LIB_CRYPTO} is linked into
rtpproxy_debug and librtpproxy (and so into rtpproxy). Generated files
(configure, Makefile.in, config.h.in) are not regenerated here.

Tests: badpath_enc is replaced with badpath_b64 and badpath_sha256. They
check that traversal Call-IDs are recorded under the expected names
inside of the recording directory, that '/' in the from-tag is still
refused, that an over-long Call-ID is refused with b64url but recorded
with sha256, and that an unknown method is rejected. badpath_sha256 runs
in builds with HAVE_LIBCRYPTO, which tests/Makefile.am now exports to
the tests, so a build with OpenSSL where the method does not work fails
the test.
…hash

badpath_b64 and badpath_sha256 now also:

  - record with a spool directory (-S), with both R and Rs, and check
    that the files end up in the recording directory under the encoded
    name and nothing is left in the spool;
  - record everything (-a) and check the file names, and that a session
    whose from-tag contains '/' is still set up but not recorded;
  - refuse '/' in the from-tag with -S as well.

The Call-IDs used cover all three base64 tail lengths.

badpath now checks that '/' in the from-tag is refused in the default
raw mode too.

In a build without OpenSSL (no HAVE_LIBCRYPTO), recording/basic now
checks that --record_callid_hash sha256 is refused with the explicit
error.
@sobomax

sobomax commented Nov 21, 2026

Copy link
Copy Markdown
Member

Thanks for this, and for the very thorough testing. The problem is real and worth fixing, but I've reworked the approach and pushed the result on top of your commit. Here's why.

Why it was reworked

  • Escaping one character only fixes one symptom. Percent-encoding / and % works for the case you hit, but / is just the first character that breaks a file name. Other awkward bytes and plain length are the same problem. Each would need its own escaping rule, and every consumer of the recordings would have to reimplement those rules exactly.
  • Encoded names can still be too long. Escaping can triple a name's length, so long Call-IDs can still exceed PATH_MAX/NAME_MAX and go unrecorded.
  • The option should choose a method, not toggle one encoding. A boolean --record_fname_encode locks us into a single scheme. A method selector lets us add schemes later without another option.
  • The from-tag doesn't need encoding. RFC 3261 doesn't allow / in a tag, so a from-tag containing / is still refused rather than worked around.

What it does now

--record_callid_hash raw|sha256|b64url sets how the Call-ID part of the recording file name is built for R, Rs and -a:

  • raw: the default and the current behaviour. A Call-ID containing / is refused.
  • sha256: the SHA-256 digest of the Call-ID, as 64 lowercase hex characters. Needs rtpproxy built with OpenSSL; configure now detects libcrypto on its own.
  • b64url: URL-safe base64 (RFC 4648 §5) without padding. Reversible, for when you need to get the Call-ID back from the file name.

In every mode, the from-tag is used as is and still refused if it contains /, and C/Cs names and remote (udp:) recording are unaffected. An unknown method, or sha256 on a build without OpenSSL, is reported at startup and rtpproxy exits with an error.

Recommendation for your setup: use sha256

rtpproxy ... -r <dir> --record_callid_hash sha256
  • It's the recommended method for its security properties.

  • Every legal Call-ID works, including ones containing / and very long ones, because the name is always 64 characters.

  • Finding a recording only needs the Call-ID, which your CDRs or SIP logs already have:

    printf '%s' "$CALLID" | sha256sum | cut -d' ' -f1
    # files: <digest>=<from-tag>.o.rtp / .a.rtp for R,
    #        <digest>=<from-tag> (.pcap with -P) for Rs

Use b64url only if you need to go from a file name back to the Call-ID, for example to index existing recordings. Its file names are case-sensitive.

Tests

Your badpath_enc is replaced by badpath_b64 and badpath_sha256. They cover R/Rs, the -S spool directory, -a record-all, / in the from-tag, over-long Call-IDs, and unknown methods. badpath_sha256 runs whenever configure found libcrypto. Builds without it check instead that sha256 is refused.

It would be great if you could run your carrier-traffic setup against this branch with --record_callid_hash sha256, and confirm that your lookup finds the recordings, especially for the Call-IDs containing /.

@sobomax
sobomax force-pushed the record-fname-encode branch from 7222f82 to a6ce175 Compare November 21, 2026 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants