Skip to content

Commit 239acb4

Browse files
committed
graph/db: validate edge policy TLV data before persisting
In this commit, we start validating the extra opaque data of a channel edge policy before persisting it. We just check that the data is valid TLV. NOTE: we recently [started validating](1410a09) this at the lnwire level. So really, no new update will reach the DB layer without this already being checked. But we check it again here so that the DB API behaves correctly as its own unit.
1 parent c6d6d4c commit 239acb4

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

graph/db/errors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ var (
1212
ErrEdgePolicyOptionalFieldNotFound = fmt.Errorf("optional field not " +
1313
"present")
1414

15+
// ErrParsingExtraTLVBytes is returned when we attempt to parse
16+
// extra opaque bytes as a TLV stream, but the parsing fails.
17+
ErrParsingExtraTLVBytes = fmt.Errorf("error parsing extra TLV bytes")
18+
1519
// ErrGraphNotFound is returned when at least one of the components of
1620
// graph doesn't exist.
1721
ErrGraphNotFound = fmt.Errorf("graph bucket not initialized")

graph/db/graph_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,19 +4195,25 @@ func TestGraphCacheForEachNodeChannel(t *testing.T) {
41954195

41964196
directedChan := getSingleChannel()
41974197
require.NotNil(t, directedChan)
4198-
require.Equal(t, directedChan.InboundFee, lnwire.Fee{
4198+
expectedInbound := lnwire.Fee{
41994199
BaseFee: 10,
42004200
FeeRate: 20,
4201-
})
4201+
}
4202+
require.Equal(t, expectedInbound, directedChan.InboundFee)
42024203

4203-
// Set an invalid inbound fee and check that the edge is no longer
4204-
// returned.
4204+
// Set an invalid inbound fee and check that persistence fails.
42054205
edge1.ExtraOpaqueData = []byte{
42064206
253, 217, 3, 8, 0,
42074207
}
4208-
require.NoError(t, graph.UpdateEdgePolicy(edge1))
4208+
require.ErrorIs(
4209+
t, graph.UpdateEdgePolicy(edge1), ErrParsingExtraTLVBytes,
4210+
)
42094211

4210-
require.Nil(t, getSingleChannel())
4212+
// Since persistence of the last update failed, we should still bet
4213+
// the previous result when we query the channel again.
4214+
directedChan = getSingleChannel()
4215+
require.NotNil(t, directedChan)
4216+
require.Equal(t, expectedInbound, directedChan.InboundFee)
42114217
}
42124218

42134219
// TestGraphLoading asserts that the cache is properly reconstructed after a

graph/db/kv_store.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,6 +4546,12 @@ func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
45464546
}
45474547
}
45484548

4549+
// Validate that the ExtraOpaqueData is in fact a valid TLV stream.
4550+
err = edge.ExtraOpaqueData.ValidateTLV()
4551+
if err != nil {
4552+
return fmt.Errorf("%w: %w", ErrParsingExtraTLVBytes, err)
4553+
}
4554+
45494555
if len(edge.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
45504556
return ErrTooManyExtraOpaqueBytes(len(edge.ExtraOpaqueData))
45514557
}

0 commit comments

Comments
 (0)