mssql lane decodes Microsoft SQL Server’s TDS protocol. Envoy ships no TDS filter of any kind, so an Envoy+OPA layer on its own polices none of this traffic. Postgres at least gets table-and-verb metadata from Envoy’s contrib filter; SQL Server gets nothing.
config.yaml
What the codec reads
A TDS message may span several packets, and the codec reassembles them before parsing: a statement classified from a fragment is the exact failure a policy cannot tolerate. Three message types carry something policy reads:
The codec hands a batch to the classifier whole and splits it with T-SQL’s own lexical rules (
[ opens a quoted identifier there and is an array subscript in PostgreSQL, so one dialect cannot serve both).
Stored procedures
An RPCRequest is how a driver calls a procedure by name (callproc, EXEC from an ORM) and how .NET and JDBC send every parameterized query: sp_executesql with the statement as a parameter. The codec reads every call in the message:
Parameter values never enter the statement text: they are the data a mask rule exists to keep out of an audit trail. A call the client flagged not to run (TDS’s NoExecFlag, which SqlClient sets on the prepare half of a prepare-and-execute pair) is still judged, because its text reached the server and a later call may run it; its row carries
mssql.noexec: true.
The system procedure is recognized the way SQL Server resolves it: an sp_ name under dbo or sys in any database runs master’s procedure, so those spellings carry the client’s statement and are judged as such. custom.sp_executesql runs a user procedure of that name and stays an opaque EXEC.
A procedure’s body is in the catalog, so no rule on verbs or tables reaches it. Name the procedure instead, with the procedure rule:
get_secrets matches dbo.get_secrets, [dbo].[get_secrets] and appdb.dbo.get_secrets, from a batch EXEC (with or without a ; before it), an RPC call by name, or an EXEC wrapped in sp_executesql/sp_prepexec the way .NET and JDBC send it. Any procedure not named runs, is recorded as EXEC <name> with mssql.proc, and has its result set masked like a query’s. Rego reads the same name as input.metadata["mssql.proc"].
Prepared statements
JDBC, ODBC and SqlClient prepare withsp_prepexec (or sp_prepare, sp_cursoropen and the cursor family) and run with sp_execute, which carries an integer handle and nothing else. The handle is the server’s: it comes back in the reply to the prepare. The codec drives both directions of the connection, reads the handle out of that reply and files the text under it, so every sp_execute is judged and recorded as the SQL it runs, with mssql.proc: sp_execute and mssql.handle. A delete rule reaches a prepared DELETE on its tenth execution as on its first.
A handle prepared before the connection was observed, or whose reply the codec could not read, is unknown with a reason naming the handle. That is the fail-closed direction: reporting it as harmless because the bytes on the wire are one integer is how an execution of a prepared DELETE passes as nothing.
Who the session is
A LOGIN7 that crosses in the clear is a statement:LOGIN <user>, operation other, with the client’s host, application, database, driver and TDS version under mssql.login.*. The password, an SSPI ticket and a federated token are never reported.
The user name of a SQL login names the session. Every audit row from there carries principal: <user>, which Rego reads as input.context.principal, and the LOGIN row loses mssql.login.user. The session opens as the connection and turns into the named one on the login frame, so the trail shows a 0-statement anonymous session before it, as an http lane does. No lane option governs this.
Readable means the login was not the one encrypted packet: a TDS 8.0 session something in front terminated (the Envoy shape below), a client sending
ENCRYPT_NOT_SUP (go-mssqldb’s encrypt=disable), or a relay terminating the login itself. A login the codec cannot read is never refused over it; a lane that wants no unnamed sessions says so in Rego:
Integrated authentication passes through untouched
TDS gives the SSPI exchange its own packet type (0x11) and the login its own (0x10), so Kerberos works through this codec with no Kerberos code: the AP-REQ the client’s OS minted is opaque bytes in a packet type the codec forwards verbatim, and inspection begins at the first SQLBatch after it. The protocol’s own message typing marks the boundary, so the codec never guesses where login ended. Kerberos and SQL Server covers the full deployment.
The one server reply it refuses
A routingENVCHANGE, the server redirecting the client to another host, would take the connection to a socket the relay does not hold. The codec refuses it and the lane closes the connection rather than forwarding a working bypass.
TLS on each leg
The TDS version the client speaks decides who terminates its TLS, and with it the whole deployment shape:
The hop from the lane to SQL Server stays plaintext: the Linux build accepts no TLS shape the Sidecar can originate, so leave
upstream_tls unset and keep that hop inside the pod or host boundary. The reasoning and every negotiation detail live in Kerberos and SQL Server.
Masking
The codec re-frames TDS result sets: it reads each row against theCOLMETADATA the server declared, rewrites matching values, and rebuilds the token stream around them. Column rules match those declared names.
An OUTPUT parameter comes back as a RETURNVALUE token and is masked the same way, under the parameter’s name without its @: a rule on column email reaches @email OUTPUT, not @e. A PII rule reaches either.
A driver that negotiates Always Encrypted gets a key table in front of every result set’s columns. mssql-jdbc does on every connection, whether or not a column uses the feature. The codec reads the acknowledgement in the login response and parses accordingly, so a JDBC result set masks like any other.
One limit: if a result set carries a column type whose wire length the codec cannot compute (sql_variant, XML, a UDT, an encrypted column), masking stops for that connection rather than guessing at byte boundaries, because a wrong guess desynchronizes the client. Statements, policy and audit keep running; the rewrite alone stands down. The stop is recorded once, as an activity audit row named masking_declined with the reason: values keep travelling after it, and neither end of the connection can tell.
Denials
A denied statement returns a synthesized TDS reply, anERROR token followed by DONE(error), the same shape a real SQL Server error takes. The developer reads the rule’s message in sqlcmd instead of watching the socket drop:
The Envoy lane
TDS 8.0 is TCP, then TLS, then the protocol, so Envoy terminates it with an ordinaryDownstreamTlsContext in front of tcp_proxy:
envoy.yaml
Encrypt=strict. A TDS 7.x client skips Envoy and reaches the lane directly, because its TLS handshake travels inside 0x12 packets Envoy cannot speak.
Two compose stacks run this end to end, split by who terminates: deploy/docker-compose/envoy-stack/mssql/ (SQL Server 2022, TDS 8.0, Envoy terminating, a Samba AD DC and a Kerberos client) and mssql2019/ (TDS 7.4, no Envoy, the encrypted login covered). See local testing.
Next
Kerberos and SQL Server
Integrated auth end to end, TDS version negotiation, and the two local stacks.
Config File Reference
Every listener field, inheritance between lanes, and what startup refuses.